need advice on how to best do this

  • Thread starter Thread starter Peted
  • Start date Start date
P

Peted

Hi

Looking for advice on best way to implement this idea

I have a form with 10 text boxes, and one button. when user clicks
button i want to transmit info in text boxes to a device, but i only
want to transmit data from textboxes that user has changed data in
since last button click

How can i check if the text box contents have been changed by user, so
i only transmit those ones and dont have to transmit data from all of
them.
 
This would be a two-step process. First you need to have a Dictionary
that is keyed on the textbox references.

You should have a routine which will cycle through the textbox, and set
the values in the dictionary to false based on the references as keys.

Then, have a TextChanged event handler set up for all of the textboxes
so they all point to the same textbox.

In the event handler, use the sender parameter to find the key in the
dictionary, and then set the value to true.

When you click the button, cycle through the keys (which are the
references to the textboxes) and for the keys that have values of true, send
your values. As you cycle through the keys and process the text, set the
value in the dictionary back to false.

Hope this helps.
 
I have a form with 10 text boxes, and one button. when user clicks
button i want to transmit info in text boxes to a device, but i only
want to transmit data from textboxes that user has changed data in
since last button click

How can i check if the text box contents have been changed by user, so
i only transmit those ones and dont have to transmit data from all of
them.

In addition to what Nicholas wrote, keep in mind that the exact
implementation depends on your definition of "changed by the user". His
suggestion handles the definition of the user actually editing the text
box, whether the net results of the edit result in the same value as the
previous one or not. You could alternatively save all the original values
and compare them after the button is pushed. Or if you go to the other
extreme in defining "changed by the user", you could instead handle
KeyPress or KeyDown events in the textbox, considering a textbox as
"changed" any time the user does any input to the textbox, even if that
input doesn't result in the text changing.

Pete
 
[...]
Then, have a TextChanged event handler set up for all of the
textboxes so they all point to the same textbox.

I presume you meant to write "all point to the same method". :)

Also, assuming the Tag property of each textbox isn't already in use for
some other purpose, an alternative to using the Dictionary might be to
just keep an array of Booleans, with an index into the array stored in the
Tag property. Not that performance would be a concern here (though doing
it this way does optimize the speed of the tracking when things change),
but one might consider it a simpler implementation to do it that way.
When the button is pushed, then one just enumerates all of the textboxes,
handling just those that have the flag set in the array.

Pete
 
How about you use DataBindings to bind the Text of the TextBox to an
object that tracks state? i.e.

public sealed class DeviceOption { // ? name
public string Text {...} // optionally with change notification
public string OldText {...}
public DeviceOption(string text) {Text=OldText=text;}
public bool IsDirty {get{return Text!=OldText;}}
}
....
DeviceOption opt = new DeviceOption("abc");
textbox1.DataBindings.Add("Text",opt,"Text");
.....
foreach(DeviceOption opt in options) {
if(opt.IsDirty) {...}
}

You can do similar just toggling a flag on change, but personally I
like it when I change a value from A to B and back to A, then it
treats it as "clean".

Marc
 
Looking for advice on best way to implement this idea

I have a form with 10 text boxes, and one button. when user clicks
button i want to transmit info in text boxes to a device, but i only
want to transmit data from textboxes that user has changed data in
since last button click

How can i check if the text box contents have been changed by user, so
i only transmit those ones and dont have to transmit data from all of
them.

Hi Peted, everyone's given you some various ideas on the implementation of
this. First I would look at how you want to solve the problem. Basically you
need to store what *you* consider to be the previous value *somewhere*. It
doesn't matter if that's in a dictionary, array, tag property or whatever.
Somewhere that previous value has to be stored. You have to decide what to
set it to initially (do you read from the device), when to update it etc.
Once you've worked that out then decide how you'll store it.

A couple of solutions not already presented is to just store it in an array
and the other option is to inherit the textbox and make it a property of the
textbox.

Michael
 
[...]
Basically you
need to store what *you* consider to be the previous value *somewhere*.

Well, no. That was the point of the response from Nicholas. His
suggestion doesn't rely on tracking the previous value; it just keys off
of whether the user does something to cause the TextChanged event to occur.

The difference between that and what you suggest was the point of my post.
A couple of solutions not already presented is to just store it in an
array

I offered this option.
and the other option is to inherit the textbox and make it a property of
the textbox.

Yes, that's yet another way to track the changes. :)

Pete
 
Peter Duniho said:
Well, no. That was the point of the response from Nicholas. His
suggestion doesn't rely on tracking the previous value; it just keys off
of whether the user does something to cause the TextChanged event to
occur.

That is possible although a poorer solution in most cases. Either way he
still needs to decide what he actually wants to do and then decide how to
store the data as *seperate* issues.
The difference between that and what you suggest was the point of my post.

I didn't read everything too thoroughly.
I offered this option.

I missed that.
 
thanks to everyone for all the good advice, i will have a look and see
what works best

cheers
 
That is possible although a poorer solution in most cases. Either way he
still needs to decide what he actually wants to do and then decide how to
store the data as *seperate* issues.

Yes, I agree (and said as much :) )
 
Back
Top