Updating the state of a control

  • Thread starter Thread starter RL Stevenson
  • Start date Start date
R

RL Stevenson

Often I want to change the Enable or Visibility on a control when some other
control on the form changes. Or maybe I need to make a change to the
DataSource on a combo box when the user makes a selection elsewhere on the
form.

In Visual FoxPro, you would call Refresh on the form causing Refresh to be
called on all the controls on the form, causing the Refresh event code to be
called and allowing the controls to update themselves to the new state. Or
sometime you might just call Refresh on a single control if two controls are
tightly coupled.

Basically, I would like to have a method to call on a form or control that
will execute some event code that I have provided.

I can't find anything in Windows Forms so far that even reminds me of this
capability, yet I expect that this kind of functionality is widely used.
Can someone help me find the right tree in this forest?
 
System.Windows.Forms.Control has an event called "Refresh", is this the
tree you look for.

WinFroms expose a lot of even handler you can hook your code in,are you
looking for how to hook an event handler to call your own code?
Generally, you can override some event handler methods to do the stuff
you are interested.

JW
 
Thank you for pointing out the refresh method. I was not looking in the
right place.

Now, how to find out what events, if any, are caused by calling Refresh.

I did a simple experiment with a textbox to see if I could invoke my event
code by calling Refresh. The help file says Refresh causes the control to
redraw itself, but doesn't mention any events that occur.

Hoped to find a Refresh event for a textbox, but no.

Paint event? No such thing for a text box.

Maybe the Layout event?

That one doesn't seem to be triggered by invoking Refresh on the textbox.

Seems like there should be some event that is triggered, but I haven't
discovered it yet.

Any hints?
 
Hi,

In c# you don't "Refresh" a control, because in c# their is no distinction
between the control, and it's value. It looks to me as if Foxpro is a bit
like C++/MFC, which kept the controls and values seperate, and the programmer
had to keep them in synch. The way it works in C# is that you just assign a
value to control's property, and that's it, you are done.

eg.
TextBox name = new TextBox();
name.Text = "Jerry Jones";

if (name.Text == "Jerry Jones)...

That's it...no other data to maintain or synchronise.

There is also the problem of changing several controls when one control
changes. I don't know if there is a neat c# solution to that problem. I write
a method called "UpdateControls" which changes the states of the controls
which are dependant on other controls. I invoke "UpdateControls" whenever one
of the controls which is depended on changes value.

Regards,

Stephen
 
Thanks.

Your approach is very similar to the one I used.

I liked the FoxPro approach because it seemed more object-oriented to me.
The code to update the state of the control is part of the control. You can
consider the control to be an abstract state machine in some sense because
when you tell it to update itself, the control finds the state variables in
its environment that affect it, and then the control changes its state to
conform to the current environment.
 
Back
Top