Attach external variable to control?

  • Thread starter Thread starter Julie J.
  • Start date Start date
J

Julie J.

When working w/ forms and controls, in 95% of the cases, the value of the
control is used somewhere in the program (duh!).

However, there doesn't seem to be an easy way to attach a value variable to
that control the automatically handles synchronization of the value w/ the
contol.

Here is an example:

// some form
System.Windows.Forms.TextBox inputSearchString;

// some variable that holds the value of the input
string searchString;

Now, if I want to synchronize the value of the input w/ the string, I have to
go and create a couple of event handlers that copy the text box value to the
string if I want to ensure that the searchString is always current.

It seems that a better solution would be for something like an attach operation
that would couple and synchronize the values:

searchString.AttachTo(inputSearchString);

Now, whenever the value of inputSearchString changed, it would automatically
update the value in searchString.

Is there something in the language/framework that I've missed that would allow
this type of functionality, or must I just create a mess of event handlers?
 
Julie J. said:
When working w/ forms and controls, in 95% of the cases, the value of the
control is used somewhere in the program (duh!).

However, there doesn't seem to be an easy way to attach a value variable
to
that control the automatically handles synchronization of the value w/ the
contol.

Here is an example:

// some form
System.Windows.Forms.TextBox inputSearchString;

// some variable that holds the value of the input
string searchString;

Now, if I want to synchronize the value of the input w/ the string, I have
to
go and create a couple of event handlers that copy the text box value to
the
string if I want to ensure that the searchString is always current.

It seems that a better solution would be for something like an attach
operation
that would couple and synchronize the values:

searchString.AttachTo(inputSearchString);

Now, whenever the value of inputSearchString changed, it would
automatically
update the value in searchString.

Is there something in the language/framework that I've missed that would
allow
this type of functionality, or must I just create a mess of event
handlers?

To achieve this with actual linking would require alot of work(it'd be based
on refelction and just not something I'd want to do). It'd have to be based
on fields, not variables. Such a mechanism would also start to tie variables
into other bits they shouldn't.

However, couldn't you simply use a property here? something like:

TextBox inputSearchString;
string searchString
{
get
{
return inputSearchString.Text;
}
set
{
inputSearchString.Text = value;
}
}
 
Daniel, thanks for the reply.

The motivation behind my quest is that a lot of the work that I do is based on
entities that exist beyond just their UI representation.

For example, defining a set of search parameters (search text, and 15-20 other
filtering parameters). I've encapsulated all of the parameters into a class,
and instances of that class are created, defined, archived, etc. and eventually
may display an interface for the user to make changes to the parametric
values. So, for that reason, it doesn't make sense to define the parameter
class in terms of UI elements. Further, since C# doesn't support MI, I can
define the UI in terms of the underlying parameter class.

At least w/ VS6 and prior, there was the ClassWizard for MFC that would
facilitate the hooking up of variables w/ a UI representation. I'm finding it
tedious (and error prone) to have to resort to event handling to perform the
required level of value synchronization.

Too bad there isn't an easier way in the language/framework...
 
Julie J. said:
Daniel, thanks for the reply.

The motivation behind my quest is that a lot of the work that I do is based on
entities that exist beyond just their UI representation.

For example, defining a set of search parameters (search text, and 15-20 other
filtering parameters). I've encapsulated all of the parameters into a class,
and instances of that class are created, defined, archived, etc. and eventually
may display an interface for the user to make changes to the parametric
values. So, for that reason, it doesn't make sense to define the parameter
class in terms of UI elements. Further, since C# doesn't support MI, I can
define the UI in terms of the underlying parameter class.

At least w/ VS6 and prior, there was the ClassWizard for MFC that would
facilitate the hooking up of variables w/ a UI representation. I'm finding it
tedious (and error prone) to have to resort to event handling to perform the
required level of value synchronization.

Too bad there isn't an easier way in the language/framework...

Julie,

Since you've encapsulated those parameters into a class, why can't you use
databinding to bind control values to your class members?

You might have some issues with synchronizing the BindingManagerBase and
figuring out the proper time to use EndCurrentEdit, but in general, this
seems to be the solution that .NET prescribes to your problem.

Erik
 
Julie J. said:
Daniel, thanks for the reply.

The motivation behind my quest is that a lot of the work that I do is
based on
entities that exist beyond just their UI representation.

For example, defining a set of search parameters (search text, and 15-20
other
filtering parameters). I've encapsulated all of the parameters into a
class,
and instances of that class are created, defined, archived, etc. and
eventually
may display an interface for the user to make changes to the parametric
values. So, for that reason, it doesn't make sense to define the
parameter
class in terms of UI elements. Further, since C# doesn't support MI, I
can
define the UI in terms of the underlying parameter class.

At least w/ VS6 and prior, there was the ClassWizard for MFC that would
facilitate the hooking up of variables w/ a UI representation. I'm
finding it
tedious (and error prone) to have to resort to event handling to perform
the
required level of value synchronization.

Too bad there isn't an easier way in the language/framework...

Frankly, you will *always* be handling events in some way, all a wizard
would do is hide that from you. The framework level code required to do what
you want in a generic way would be complicated to say the least.

To achieve this sort of behaviour across *alot* of UI work, you may want to
look into your own code generator that achieves your target. This may be
possible in an easier way with C++ style templates, but they don't exist in
C#.

You may also want to look into databinding, it could well do what you want.
It is outside of my experiance however.
 
Back
Top