Sender, how to code it as one line code.

  • Thread starter Thread starter Boki
  • Start date Start date
B

Boki

Hi All,

We can assign multi object ( ex: textbox )'s double click point to a
same function. ex:


private void all_boki_textbox_point_here_DoubleClick(object sender,
EventArgs e)
{
...
...

}

If I have many objects, they are totally doing the same thing, ex:
copy textboxN's text to a specify textbox.

my code become:
private void all_boki_textbox_point_here_DoubleClick(object sender,
EventArgs e)
{
if (sender == textbox1)
final_text_box.text=textbox1.text;

if (sender == textbox2)
final_text_box.text=textbox2.text;

....
}

How to write code as only one line ?

Thanks!



Best regards,
Boki.
 
* Boki wrote, On 9-7-2007 11:12:
Hi All,

We can assign multi object ( ex: textbox )'s double click point to a
same function. ex:


private void all_boki_textbox_point_here_DoubleClick(object sender,
EventArgs e)
{
..
..

}

If I have many objects, they are totally doing the same thing, ex:
copy textboxN's text to a specify textbox.

my code become:
private void all_boki_textbox_point_here_DoubleClick(object sender,
EventArgs e)
{
if (sender == textbox1)
final_text_box.text=textbox1.text;

if (sender == textbox2)
final_text_box.text=textbox2.text;

...
}

How to write code as only one line ?

Thanks!



Best regards,
Boki.


TextBox tb = sender as TextBox;
if (tb != null)
{
finalTextBox.Text = tb.Text;
}

would do the trick.

Jesse
 
* Boki wrote, On 9-7-2007 11:12:








TextBox tb = sender as TextBox;
if (tb != null)
{
finalTextBox.Text = tb.Text;

}

would do the trick.

Jesse

Great, thanks!
 
Back
Top