tags:

views:

435

answers:

2

I am trying to figure out a simple way of making a text box equal all letters to the left of a "-" in another text box. Basically, if an end-user types blah-test in textbox1, I would like textbox2 to equal blah. I have tried if statements and substrings based off of letter position count (i.e. substring(0, 5); however, this got very lengthy and impractical, since the words entered into textbox1 can be any length.

Thank you,

DFM

+3  A: 

Try this:

if(textbox2.text.Contains("-"))
{
  textbox1.text = textbox2.text.Split("-")[0];
}

Here we first check if textbox2 contains the - character and if it does we split the text in two parts and set the text of textbox1 to the part that is left of the first - character.

Rune Grimstad
Thank you for your reply - I should have included this in the original question, end-users will be able to enter a string with multiple "-"; hence, the string could be test-test1-test2-test3 etc... It actually corresponds with a file directory (i.e. C:/Test/Test1/Test2 etc... ). Now that I see how to do one "-", I should be able to enhance the code to read more "-"; although, your additional suggestions are well appreciated.Thank you,DFM
+1  A: 

it's very simple

select and double click on the OnTextChange event on textbox1

Write this code inside textbox1_OnTextChange:

string text = textbox1.Text;
textbox2.text = text.Substring(0, text.indexOf("-"));

and you're done!

Makach
That code will throw an exception if there is no '-' character present in textbox1.
Fredrik Mörk
Thank you - this will put me in the right direction. Now, I need to enhance this code so that textbox3 will get all letters in between "-" and another "-", textbox4 will get all letters between the 2nd "-" and the third "-". Essentially, textbox1 could have the string "Test-Test1-Test2-Test3 etc...". Textbox2 and up will each contain a part of textbox1's string (in between each "-"). Also, I will put in an "if" to handle no "-". Any additional suggestions would be appreciated.Thank you,DFM
After reading your comments, it seems like its String.Split that you actually needed. the array returned by split will have the parsed strings for each textbox that you need to populate.
Gishu
Thanks Gishu - I just Google'd C# String.Split and this looks like what I need for multiple "-". I cannot believe I couldn't find this method earlier today.Thanks,