Tuesday, July 14, 2009

How do I get user input like text and numbres from windows forms controls using c# and visual studio 8?

I've been searching MSDN for days now but I can't find a definite answer. I know how to handle events and set item propreties at runtime but I have no clue as to how I might get a character string from lets say a rich text box control.

How do I get user input like text and numbres from windows forms controls using c# and visual studio 8?
If it's a textbox,


string strVal = txtControl.Text; // you can aldo do .Text.Trim();


If it's a dropdown


string strVal = (string) ddlControl.SelectedItem.Value;





Some objects have string properties (.Text) you can access directly and store into string variables; others have "object" types (.Value) that you'll need to cast. Then you can also convert them to other types by writing


int iVal = Convert.ToInt32(strVal);


for free-hand input, beware of conversion exceptions.





You can collect the user input either upon clicking of a button (set up an event handler for the button's Clicked event) or by handling the TextChanged or ValueChanged events of controls.


Creating event handlers is best and most accurately done via the designer.


No comments:

Post a Comment