How do I associate one control with others>

G

Guest

I'm using VB.NET, and my basic problem is that I've got a textbox and an
subprocedure that handles the textchanged event...the textbox is supposed to
have a number entered into it, and then two other controls, labels, change to
reflect what that number represents.

So, you enter a number into the textbox, and the performance rating ("Good",
"Poor", etc.) appears in one label, and the score (8, 9, 10) appears in
another. My problem is how to reference the two label controls just by using
the sender parameter that exists in my textchanged event handler. All I've
got is the textbox, but I want to be able to refer to the two labels that
belong to that textbox, no matter which textbox was the one that just had
data entered into it.

I've tried entering the two labels into the controls collection of a given
textbox and then trying it that way, but it doesn't seem to do anything. Is
there an easy way to do this? Thanks.
 
W

William F. Robertson, Jr.

You could always "cheat" and use similiar names.

TextBox txtGrade1;
Label lblGrade1;
Label lblGrade1Description;

TextChangedEvent( object sender, EventArgs e )
{
TextBox t = ( TextBox ) sender;

//now play with the ID property to find the other controls.
Label l = ( Label ) Page.FindControl( t.ID.Replace( "txt", "lbl" ) );
if ( l != null )
{
l.Text = "good";
}
}

That way you could use the same handler for manipulating many different
groups of TextBox label.s

bill
 
J

John Saunders

JeTmAn said:
I'm using VB.NET, and my basic problem is that I've got a textbox and an
subprocedure that handles the textchanged event...the textbox is supposed
to
have a number entered into it, and then two other controls, labels, change
to
reflect what that number represents.

So, you enter a number into the textbox, and the performance rating
("Good",
"Poor", etc.) appears in one label, and the score (8, 9, 10) appears in
another. My problem is how to reference the two label controls just by
using
the sender parameter that exists in my textchanged event handler. All
I've
got is the textbox, but I want to be able to refer to the two labels that
belong to that textbox, no matter which textbox was the one that just had
data entered into it.

I've tried entering the two labels into the controls collection of a given
textbox and then trying it that way, but it doesn't seem to do anything.
Is
there an easy way to do this? Thanks.

There is no way to "associate" controls with each other - how many different
kinds of association would .NET need to handle?

You need to do this in your own code. For instance, you could use a
Hashtable. In Page_Init, you could do something like:

private Hashtable ht = new Hashtable();

private void Page_Init(object sender, EventArgs e)
{
....
ht.Add(txtTextBox1, new Pair(label1_1, label1_2);
ht.Add(txtTextBox2, new Pair(label2_1, label2_2);
....
}

In the TextChanged event, you could do:

private void TextBox_TextChanged(object sender, EventArgs e)
{
Pair labels = (Pair) ht[sender];
TextBox text = (TextBox) sender;
Label label_1 = (Label) labels.First;
Label label_2 = (Label) labels.Second;

label_1.Text = Rating(text.Text);
label_2.Text = Score(text.Text);
}

If you wind up using several of these three-control sets, you might want to
create a web custom control, specifically a composite control composed of
the three controls). You could then use it
whenever you like.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top