Getting Information from the User Using Controls

Getting Information from User and Showing Results with C# Controls

1. Textbox control

It is used to get information from the user. The type of information in it is considered a string. Therefore, if numerical operations are to be performed with the received information, type conversion must be performed.

TextBox1.Text It is used to access the Text property of the TextBox1 control, thus getting or changing the text inside the TextBox1 as a string.

TextBox1_TextChanged  means "When the text in Textbox1 changes". If we double-click on a text box, a subroutine with that name is created automatically.

Sample:

string a = textBox1.Text;
int b = int.Parse(textBox2.Text);
float c = float.Parse(textBox3.Text);

2. Label control 

It is used to inform the user. Since the type is considered string, it is necessary to convert different types of variables to string in order to print them on the label.

Label1.Text It is used to get or change the text in label1 as string.

Sample:

string name;

float average=50f;

label1.Text = "Welcome.";

label2.Text = name;  /* The type of name variable is string, so no conversion is needed. */

label3.Text = average.toString(); /* We can use the toString() method to print a numeric variable to the label. */

 

3. ComboBox

It is a drop-down list, allowing the user to choose. Some features:

ComboBox1.Text  Returns the text of the selected element as a string.

ComboBox1.SelectedItem.toString() Returns the   selected element as a string.

ComboBox1.SelectedIndex  Returns the sequence number of the selected element. The first element is 0. If no element is selected, this value will be -1.

Index number: In controls containing more than one element, such as ComboBox, ListBox, each element is given an index number. The index number of the first element is 0, and the other elements are numbered sequentially. 

With the SelectedIndex property, we can learn the number of the currently selected element in a list. If no selection is made, this value will be -1, if selected, it will be the number of the selected element.

ComboBox2_SelectedIndexChanged   means "When the selected element in the dropdown named ComboBox2 changes". If we double-click on a combobox element, a subroutine with a similar name is automatically created.

Sample:

label1.Text = ComboBox1.Text;

label2.Text = ComboBox2.SelectedItem.toString();

label3.Text = ComboBox3.SelectedIndex.toString(); /* Returns -1 if no selection is made. */

 

4. ListBox

It works like ComboBox. It has the same features. The difference in appearance is that it is always clear. If allowed, more than one selection (via CTRL key) can be made.

ListBox1.Text  Returns the selected element as a string.

ListBox1.SelectedItem.toString()  Returns the selected element as a string.

ListBox1.SelectedIndex  Returns the sequence number of the selected element. The first element is 0. If no element is selected, this value will be -1.

 

ListBox1_SelectedIndexChanged   is the name of the program that will run when the selected item in the ListBox changes. We can create a subroutine with a similar name by double-clicking the ListBox.

Sample:

label1.Text = ListBox1.Text;

label2.Text = ListBox2.SelectedItem.toString();

label3.Text = ListBox3.SelectedIndex.toString();

 

5. RadioButton

It allows the user to choose only one of the options. If there will be two separate radiobutton groups on the form, we can group them by enclosing them in a separate groupBox.

RadioButton1.Text is the   Text property. In other words, it determines the text that will appear next to it.

Checked property = Indicates whether it is checked. Checked=true is checked, checked=false is unsigned.

 

6. CheckBox

It has the same features as RadioButton. But more than one selection can be made or it can be left without any selection.

Checked property = Indicates whether it is checked. Checked=true is checked, checked=false is unsigned.

 

7. dateTimePicker

This control can be used when date information is requested from the user. It allows the user to select the desired date with the mouse.

dateTimePicker1.Text returns the selected date in text type.

dateTimePicker1_ValueChanged  is the name of the subroutine that will run when the selected date changes. It can be created by double-clicking on the object.

 

Getting information from c# controls, writing information, getting the number in the textbox to the variable, printing the numeric variable to the label, what is the selectedindex

EXERCISES

There are no examples related to this subject.



COMMENTS




Read 549 times.

Online Users: 250



getting-information-from-user-using-controls