How to link two TextBoxes?

M

Marcus Kwok

I have a form that has several pairs of TextBoxes: one editable and one
read-only. The text in the read-only TextBox should always show the
same text as the editable TextBox, except it does not have to be the
same while the user is actually typing something if that would add
unnecessary complication. The values in the TextBoxes need to be
doubles, so I plan on checking this with a Validating event.

I am thinking that I could sync the TextBoxes during the editable
TextBox's "Focus - Leave" event, but I would rather not have to code a
separate one for each pair of TextBoxes, and also it seems that the
"Leave" event occurs before Validation. The "Lost Focus" event occurs
after Validation if the keyboard is used, but if it the mouse is used,
it occurs before Validation:

http://msdn.microsoft.com/library/d...emwindowsformscontrolclassvalidatingtopic.asp

so I do not think I can use the "Lost Focus" event for this.

So, my question is: Is there a way to link the two TextBoxes using the
Designer, or if not, what is the best way to approach this? I am using
Managed Extensions to C++ (VS .NET 2003).
 
K

Ken Halter

Marcus Kwok said:
So, my question is: Is there a way to link the two TextBoxes using the
Designer, or if not, what is the best way to approach this? I am using
Managed Extensions to C++ (VS .NET 2003).

If the only requirement is that the textboxes contain the same data, valid
or not, I'd probably throw something in the Change event of the 'read-write'
box that sets the Text property of the read-only box.... but that's the "VB
Classic" way so ymmv.....
 
M

Marcus Kwok

Ken Halter said:
If the only requirement is that the textboxes contain the same data, valid
or not, I'd probably throw something in the Change event of the 'read-write'
box that sets the Text property of the read-only box.... but that's the "VB
Classic" way so ymmv.....

OK, that's not a bad idea. However, ideally I would like to set it up
so that the user can only leave the TextBox if they've entered a valid
number, at which point the read-only TextBox would get sync'ed.

Or, maybe I'll just avoid all this complication and do what you
suggested, and leave the validation checking until the user dismisses
the dialog with an OK (and ignore invalid entries if they Cancel).

There is still the issue of how to do this without having to write a
separate method for each pair of TextBoxes. I think it is do-able by
grouping each pair of TextBoxes in an array or something, but then my
worry is that I will lose these structures when I change something else
in the Designer and it rewrites InitializeComponent().

Thanks for your response.
 
T

tribbles

Use the 'leave' event of the editable textbox. If the value is nto
valid, set focus back to itself. If it is valid, update your read onyl
textbox.
 
B

Bruce Wood

I would do the following.

Create a UserControl that consists of two text boxes. (This assumes
that the two text boxes are always physically next to each other...
won't work if you want to place them at arbitrary locations on the
form.)

In your UserControl, handle the Validated event of the editable text
box. When the editable text box is Validated, update the read-only text
box.

Ideally, you would then expose through the user control only properties
that you want your caller to see, for example the Text property of the
UserControl could always return the contents of the read-only text box,
but setting it would set both text boxes. You should also pass on to
the "outside" the Validating event of the editable text box, so that
you can write event handlers that will validate different text box
contents.

Or, you could just cheat and expose both text boxes as public members.
(Yes, that's nasty, but in a pinch....)

Then you could place the UserControl at various places around your
form, and you know that it will always provide the user the same
experience in every case.

The only downside is that it's a bit of boring grunt work exposing
properties of the text boxes through the public interface of the
UserControl, but once it's done, you never have to do it again. :)
 
G

Guest

You could use data binding and bind the two TextBoxes to the same data
source. That would keep their values in sync, without having to write any
extra methods.
 
M

Marcus Kwok

Bruce Wood said:
I would do the following.

Create a UserControl that consists of two text boxes. (This assumes
that the two text boxes are always physically next to each other...
won't work if you want to place them at arbitrary locations on the
form.)

Thanks for the idea. However, the boxes will be separated, but not
necessarily in arbitrary locations.

I am designing a method for choosing contour levels and colors for
creating a contour plot. My setup is like:

Lower Upper
Bound Color Bound
------------------------
Lower5 Color5
Lower4 Color4 Upper4
Lower3 Color3 Upper3
Lower2 Color2 Upper2
Lower1 Color1 Upper1

where Lower[1-5] and Upper[1-4] are TextBoxes, and Color[1-5] are
buttons. All of Upper[1-4] are Read-Only, as is Lower1.

Lower1 is hard-coded to 0. For the rest of the contours, the way it
works is that values between Lower_n and Upper_n will get colored with
Color_n (except for the top one: any value greater than Lower5 will be
colored as Color5, with no upper bound).

The range must be continuous, so Upper1 must equal Lower2; Upper2 must
equal Lower3; etc. We decided that it would be the most intuitive for
our target audience to supply the lower bounds for each contour, so that
is why all the Upper[1-4] are the Read-Only ones.

The complications I see with your method is that the TextBoxes that must
be sync'ed are staggered, and the first and last levels are handled
differently as well.

Maybe I'm over-thinking and I should just bite the bullet and sync them
manually.
 
M

Marcus Kwok

Zoodor said:
You could use data binding and bind the two TextBoxes to the same data
source. That would keep their values in sync, without having to write any
extra methods.

This is a good idea too, I will look into it.
 
M

Marcus Kwok

Ken Halter said:
If the only requirement is that the textboxes contain the same data, valid
or not, I'd probably throw something in the Change event of the 'read-write'
box that sets the Text property of the read-only box.... but that's the "VB
Classic" way so ymmv.....

Well, this turned out to be pretty simple to implement. It is all
contained within one function, and I made this one function the handler
for all the Read/Write TextBoxes. KISS, right?

System::Void textbox_TextChanged(System::Object * sender, System::EventArgs * e)
{
TextBox* source = __try_cast<TextBox*>(sender);
TextBox* destination;

if (source == textbox_2_low) {
destination = textbox_1_high;
}
else if (source == textbox_3_low) {
destination = textbox_2_high;
}
else if (source == textbox_4_low) {
destination = textbox_3_high;
}
// etc. for the rest of the cases

destination->Text = source->Text;
}

and then I'll save the validation for when the user dismisses the box.

Thanks for the ideas.
 
B

Bruce Wood

Hmm... now that I understand the problem you're trying to solve...
wouldn't this update the read-only box as the user types? I thought you
wanted to populate it only if what the user typed was valid.

As well, shouldn't the upper-bound, read-only text box be set to the
value in the lower-bound, editable text box minus 1, or something?

I'd handle this in the Validated event, not the Changed event if you
want the upper bound box populated only with valid contents, or want to
do some calculation on the lower bound box value to get the value for
the upper bound of the previous contour.
 

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

Top