Sign in to follow this  
ArchG

[.net] Textboxs and (sender e)

Recommended Posts

Hello, I have a windows form with a bunch of textboxs that are all set to have the same KeyUp event handler... For an example we'll pretend these are all web browser address bars (they're not but it's easy to explain like this) so when a user types in a web address and pushes the enter key..I was hoping all I had to do was cast the object sender argument to a textbox and I could just grab the text property and see what the user entered... like this

protected void UniversalEventHandler(object sender, KeyEventArgs e)
{

    TextBox Box = (TextBox)sender;
    //Box.Text should be what I entered into it
}
....BUT...it turns out that that will only give me the state in which it was initialized and doesn't keep track of what's entered into it... Is there a way to have it remember what I type into it? (if this doesn't makes sense i'll try to reiterate) Thanks, ArchG

Share this post


Link to post
Share on other sites
Here's what I did to make it work. First off, I would recommend against trying to make your own event handler for it. It's a lot easier to like this:

1. For one textbox, give it the focus in your form editor.
2. Go to properties and click the little lightning bolt to bring up the events list.
3. Double click the OnKeyUp event (or something to that effect)
4. Write in whatever it is you want it to do.
5. Then for all the other textboxes, just repeat steps 1 and 2, then proceed to step 6.
6. Instead of double clicking the OnKeyUp event, just single click and a you'll have a combo box with a list of functions in it. Pick the one that corresponds to the one you wrote for the first textbox.


That may or may not be a reiteration of what you did, but since it works for me, I'm fairly confident it will work for you. Hope that helps.

-AJ

Share this post


Link to post
Share on other sites
Hrmm...

I'm not on my comp with the visual studio but...

In your method...how would you distinguish between the different text boxes that use the same even handler...

For an Example,if I entered text in the textbox on the upper left of the screen...how would the event handler know it wasn't that textbox in the lower right corner? (since they both are using the same event handler)


Does that make sense?

Share this post


Link to post
Share on other sites
Perhaps you can check it's tab index and switch on that for deciding which tb sent the message??

Also, you may want to try the 'as' cast in your handler when casting the sender object just to make sure it was a text box sending the message -

TextBox tb = sender as TextBox;

if(tb != null)//it is a textbox, let's do it!
....

This is a very handy casting technique for testing if you actually have a certain object type and initialises it at the same time, if it's null (the result of a bad cast) you can move on or try again.

Cheers,
Mick.

Share this post


Link to post
Share on other sites
Quote:
Original post by ArchG
Hello,

I have a windows form with a bunch of textboxs that are all set to have the same KeyUp event handler...

For an example we'll pretend these are all web browser address bars (they're not but it's easy to explain like this)

so when a user types in a web address and pushes the enter key..I was hoping all I had to do was cast the object sender argument to a textbox and I could just grab the text property and see what the user entered...
like this

....BUT...it turns out that that will only give me the state in which it was initialized and doesn't keep track of what's entered into it...

Is there a way to have it remember what I type into it?

(if this doesn't makes sense i'll try to reiterate)

Thanks, ArchG


Weird. I just tried the same thing and it works perfectly:


//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 24);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.On_KeyUp);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(8, 48);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 1;
this.textBox2.Text = "textBox2";
this.textBox2.KeyUp += new System.Windows.Forms.KeyEventHandler(this.On_KeyUp);
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(8, 96);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 3;
this.textBox3.Text = "textBox3";
this.textBox3.KeyUp += new System.Windows.Forms.KeyEventHandler(this.On_KeyUp);
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(8, 72);
this.textBox4.Name = "textBox4";
this.textBox4.TabIndex = 2;
this.textBox4.Text = "textBox4";
this.textBox4.KeyUp += new System.Windows.Forms.KeyEventHandler(this.On_KeyUp);


private void On_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
TextBox box = (TextBox)sender;

MessageBox.Show(box.Text);
}
}




Perhaps I'm not understanding what you're trying to do, but you should be able to assign the same event to multiple objects.

Quote:
In your method...how would you distinguish between the different text boxes that use the same even handler...


The sender object contains the reference of an instance to the textbox that called the event. You can do a quick check (i.e. if (sender is TextBox)) and tell if it's a text box, or if you need further detail, you can do a comparison to the instance of the checkbox on the form (i.e. if (sender == MyTextBox)).

Many ways to skin that cat.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this