Generic event procedure for mutiple controls.
-
I want to be able to create generic event procedures that can be used for a number of controls (the same type) on a form. For example (assume the object sender would always be a text box);
private void tbCallBlockPct_Click(object sender, EventArgs e) { sender.SelectAll(); }
However when I try somehting like this I get a syntax error that the “object sender does not contain a definition for SelectAll”. If the function is being passed a textbox as the variable sender should not all of its methods and properties be accessable?David Wilkes
-
I want to be able to create generic event procedures that can be used for a number of controls (the same type) on a form. For example (assume the object sender would always be a text box);
private void tbCallBlockPct_Click(object sender, EventArgs e) { sender.SelectAll(); }
However when I try somehting like this I get a syntax error that the “object sender does not contain a definition for SelectAll”. If the function is being passed a textbox as the variable sender should not all of its methods and properties be accessable?David Wilkes
sender
is of typeobject
, which doesn't have aSelectAll()
method in its interface. You should castsender
to an object variable of typeTextBox
in order to invoke properties/methods particular to this class.TextBox tb = (TextBox)sender;
tb.SelectAll();Paul Marfleet
-
sender
is of typeobject
, which doesn't have aSelectAll()
method in its interface. You should castsender
to an object variable of typeTextBox
in order to invoke properties/methods particular to this class.TextBox tb = (TextBox)sender;
tb.SelectAll();Paul Marfleet
Thanks. That is alomost exactly what I ended comming up with not too long after posting the message. It seemed a bit round about and redundant, so i was not sure about it. Would it be correct to assume that since the tb.SelectAll works (selects the text in the original control) means that the tb is a pointer to the actual text box and not a copy of it? It would seem that you should be able to do it more directly, like
(TextBox)sender.SelectAll();
or something similar.David Wilkes
-
Thanks. That is alomost exactly what I ended comming up with not too long after posting the message. It seemed a bit round about and redundant, so i was not sure about it. Would it be correct to assume that since the tb.SelectAll works (selects the text in the original control) means that the tb is a pointer to the actual text box and not a copy of it? It would seem that you should be able to do it more directly, like
(TextBox)sender.SelectAll();
or something similar.David Wilkes
amatbrewer wrote:
Would it be correct to assume that since the tb.SelectAll works (selects the text in the original control) means that the tb is a pointer to the actual text box and not a copy of it?
Yes.
amatbrewer wrote:
It would seem that you should be able to do it more directly, like (TextBox)sender.SelectAll(); or something similar.
Try this one:
((TextBox) sender).SelectAll();
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Thanks. That is alomost exactly what I ended comming up with not too long after posting the message. It seemed a bit round about and redundant, so i was not sure about it. Would it be correct to assume that since the tb.SelectAll works (selects the text in the original control) means that the tb is a pointer to the actual text box and not a copy of it? It would seem that you should be able to do it more directly, like
(TextBox)sender.SelectAll();
or something similar.David Wilkes
It would be safer not to assume that the sender is a TextBox (one day it won't be). Code like this:
TextBox tb = sender as TextBox;
if (tb != null)
{
tb.SelectAll();
}
else
{
// handle the error. Maybe just assert - it's a programming error
}Phil
The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.