Reputation: 197
I am using these following code. I want to when I select all text in the textbox
it tells me but I don't know why it's not working. please give me some help. Please give some code so that I can use this when I select all text in the textbox
text
it tells me
if (textBox1.SelectAll() == true)
{
MessageBox.Show("You have selected all text in the textbox");
}
It will tell me: Operator ==
cannot be applied to operands of type void
and bool
Upvotes: 1
Views: 1735
Reputation:
I think you should use the GotMouseCapture Event Here the code it works fine for me.
private void textBox_GotMouseCapture(object sender, MouseEventArgs e)
{
textBox.SelectAll();
textBox.Focus();
}
Upvotes: 0
Reputation: 7309
I think you want to add an event handler for the TextBox.SelectionChanged event and in it, compare the TextBox.SelectedText to the TextBox.Text
Upvotes: 0
Reputation: 48558
Because
textBox1.SelectAll()
returns nothing
or return type is void
just use
textBox1.SelectAll();
If you want to check if all text is selected or not check
if(textBox1.SelectedText == textBox1.Text)
{
MessageBox.Show("You have selected all text in the textbox");
}
OR
if(TextBox.SelectionLength == TextBox.Text.Length)
{
MessageBox.Show("You have selected all text in the textbox");
}
Upvotes: 3
Reputation: 460028
Couldn't you simply check whether or not TextBox.SelectionLength == TextBox.Text.Length
?
http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.selectionlength.aspx
You comparison fails because you're comparing the text with the return value of the SelectAll
method which is void(it returns nothing since it just applies the selection).
Upvotes: 4
Reputation: 3439
Only use
textBox1.SelectAll();
When this method is executed all the text in text box will be selected. You don't need to compare it with true
. TextBox.SelectAll() return type is void
. Comparing a boolean with void will give error off course.
Upvotes: 0