1

I have 5 textboxes that gets editable on double click.

Below is method i have written for one textbox.

private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            txtFirstLctrTime.IsReadOnly = false;
            txtFirstLctrTime.Background = new SolidColorBrush(Colors.White);
            txtFirstLctrTime.Foreground = new SolidColorBrush(Colors.Black);
        }

Is there any way i can use same method for all text box instead of writing different method for all?? I am fairly new to programming

1
  • 1
    Just a design question: Why? Does the user expect to have to double-click a textbox to be able to edit? Usually the user knows: textbox gray => disabled, textbox white => editable. Why confuse the user? Commented Mar 31, 2015 at 12:42

4 Answers 4

3

You can atach this handler to all textboxes. Then you check the sender, because that's the textbox you actually clicked:

    private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        var textBox = sender as TextBox;

        textBox.IsReadOnly = false;
        textBox.Background = new SolidColorBrush(Colors.White);
        textBox.Foreground = new SolidColorBrush(Colors.Black);
    }

You should look into MVVM and data binding thought, having click-handlers and code-behind has it's limits.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot. Worked perfectly.
Please note that this may throw a NullReferenceException in case you attach the event handler to anything but a textbox. Shouldn't be a problem, just to point it out.
@ThorstenDittmar I have just attached it to textbox, so i guess there should not be any issue :)
1

Attach same handler to all textboxes and use sender argument to get textbox instance which raised event:

private void  MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
     TextBox textBox = (TextBox)sender;
    textBox.IsReadOnly = false;
    textBox.Background = new SolidColorBrush(Colors.White);
    textBox.Foreground = new SolidColorBrush(Colors.Black);
}

Comments

0

Yes, there is a way. Sender is a parameter which can tell You which controll fired this event. Look at my example below:

private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
  TextBox tbWhichFiredThisEvent = sender as TextBox;
  if(tbWhichFiredThisEvent != null)
  {
    tbWhichFiredThisEvent.IsReadOnly = false;
    // ... etc.
  }
}

Comments

0

Another option is to inherit from TextBox and implement your specific behavior on the OnDoubleClick method.

This way you can have this control on different views without repeated code.

2 Comments

Any way i can select combobox only in particular panel and use above method?
Sorry, I don't get what you mean

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.