Hello again everyone,
I’m using a container control in my app and I want to do something on the main window when a button on the control is pressed. RIght now, I have something like this in the button that is on the container controls button action event:
MainWindowName.ControlName.Text = “blah”
Is there a more elegant or correct way to do that or is this pretty much it?
I tend to make an event for the container. Then add the event to the container instance on the window. That way the container doesn’t need to know anything about its container. It also makes it easier to implement in multiple locations.
- Add to “Container Control” -> Event Definition (name it for example: ButtonPressed)
- in the Container Control’s PushButton.Action: ButtonPressed (this causes your Event to fire)
- put the Container Control on the MainWindow
- add an Event Handler to the ContainerControl (ButtonPressed)
- the code in the EventHandler then is: ControlName.Text = “the button has been pressed”
The idea here:
The ContainerControl just sends this “ButtonPressed” event. It doesn’t care what it’s parent wants to do with it.
You can also add Parameters to your Event. Then the ContainerControl can send for example a String to the Event. And the implementation of the Event in your MainWindow can do whatever it wants when the event fires.
What Bob and Jürg said are right on target.
Sometimes I use a container control in one instance only where I might want an area to be scrollable. In that case, I’ll just reference the page directly from the container like: myPage.doSomething