Sign in to follow this  
Quat

[.net] properties and UI controls

Recommended Posts

I am making a simple editor where via a UI, the user can change parameters of my 3D display. The UI is basically a mirror of some backend data. So when a UI value changes (I listen for the event), I update the backend variable and repaint. I come across one issue. In my backend class, I initialize the data to default values. Then in the UI Form constructor, I want to initialize the UI controls based on the default values. So something like this:
public MainForm()
        {
            InitializeComponent();

            numIntensityRed.Value   = Convert.ToDecimal(d3dControl.IntensityRed.X);
            numIntensityGreen.Value = Convert.ToDecimal(d3dControl.IntensityGreen.Y);
            numIntensityBlue.Value  = Convert.ToDecimal(d3dControl.IntensityBlue.Z);
        }
where "num" are numeric up/down controls. The problem is that this assignment raises the ValueChanged event of the numeric up/down controls, which then goes and sets the d3DControl properties again. This isn't really a problem, although it seems strange because the d3dControl properties don't need to be set here. It seems like this is a common issue with UI frontends for backends. What is the proper pattern to do this? Should I hook up the event handlers after I initalize the control values?

Share this post


Link to post
Share on other sites
There's not really a problem here. Setting the UI properties is going to invoke the Changed events. As long as setting the properties in your d3d control to their existing values while inside the form constructor isn't going to cause an issue, then there's nothing to worry about.

Share this post


Link to post
Share on other sites
Yeah, it doesn't hurt anything, but it seems like a waste of effort. The backend is already initialized, but then gets set again to the same value.

Share this post


Link to post
Share on other sites
It would be a waste of effort to change it so that this didn't happen. The only issue here is added overhead during load but the fraction of a millisecond it would take to set the "new" values is entirely negligible.

If you were really bothered by this you could define the default values in another place and have the UI code read from that (which in turn would set the D3D control)

But again, the effort is basically wasted as keeping it as is does not adversely affect anything.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this