Click here to Skip to main content
15,905,874 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
what is the difference between these two lines :
C#
comboBox3.Text = "";
textBox5.Clear();
Posted

Hi,

When I decompiled the TextBoxBase.Clear method, I got this:
C#
/// <summary>
/// Clears all text from the text box control.
/// </summary>
public void Clear()
{
  this.Text = (string) null;
}

So, the Clear method sets the Text property to null.
 
Share this answer
 
v2
The 'Clear method of a TextBox simply removes all text content from the TextBox; it is exactly equivalent to someTextBox.Text = ""; ... or ... someTextBox.Text = null;

Manipulating the 'Text field of a ComboBox is a very different process because it interacts with the current 'SelectedIndex property of the ComboBox:

1. comboBox3.Text = ""; ... or comboBox3.Text = null; ... will set the current SelectedIndex to -1. What you will see displayed in the ComboBox text-area at run-time is: nothing.

2. comboBox3.Text = "Choice ABC"; will possibly have two effects:

a. if "Choice ABC" is an existing item in the ComboBox item collection, then the current SelectedIndex will be set to match the index of "Choice ABC." And, "Choice ABC" will be displayed in the ComboBox text-area.

b. if "Choice ABC" is not an existing item in the ComboBox item collection, then the current SelectedIndex will not be changed, but "Choice ABC" will be displayed in the ComboBox text-area.
 
Share this answer
 
Hello Mohi,

Both methods are similar in their action, they clear the contents of a textbox, You can use TextBox.Clear method to clear the contents of the control instead of assigning the Text property an empty string.

Regards,
 
Share this answer
 
Comments
Sumon562 29-Jun-13 5:01am    
Thank You. I am posting another question. Please help
I think no difference but
1. Clear is method provided
2. text=""; is old tradition way to clear the text
i recommend to use clear
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900