简体   繁体   中英

Determining A Selected Button In a Modeless Form In the Main From

I'm making a toolbox type thing. The modeless dialog should sit on top of the screen and provide selection options then the main form should carry out options based on which button was pushed. How do I determine what button is selected from the modeless dialog in the main form??

You have an event handler on the modeless dialog wired up to each button. When a button is clicked/toggled, this event handler will fire, thus telling you that the button was clicked/toggled.

You will then probably want to forward this notification on to your parent window. You can do this by calling an event handler on your main form, passing the identifier of the currently selected button as part of the event arguments. The only tricky part here is that, in order to call an event handler on the main form, the floating palette form will have to maintain a reference to the main form. You can do this manually, but there is no need to do so, since the floating palette will always need to be owned by the main dialog (to ensure that it actually floats on top of it in the Z order), so you can simply retrieve a reference to the main dialog by using the floating palette's Owner property. Cast it to the type of the main form, and raise the event. Process the event as desired in an event handler function defined for the main form class.

If you don't need to receive a notification on your main form, you can just track the state in the floating palette form and read it from the main form when you need to know what it is. This will require that the main form keep a reference to the floating palette. The easiest way of doing this is to have a member variable for the main form class that contains an instance of the floating palette form. This is the best design anyway , and will facilitate your ability to access/set data on the floating palette from the main form. It does slightly increase coupling, which some would say is an object-oriented design smell, but these two objects are , in reality, very tightly coupled, so this is really not a problem.

You'll notice that several places above I refer to your "modeless form" as a "floating palette". That's because the design you're describing is actually a rather common scenario in complex applications like Photoshop that have floating palette windows from which you can choose a tool. Paint.NET (written in C#) does exactly this, too, and probably implements it much as I've described.

The biggest thing that trips up new C# programmers is understanding the difference between a class and an instance of a class. The class is an abstract object—it contains all the necessary information for creating an object. The instance is the actual object itself. There is only one class definition for each class type, but there can be multiple instances/objects of each class type. For example, consider that you have a main form class named MainForm . This contains all of the code (events, properties, methods, etc.) for your main form. This is a class. In order to actually display or interact with your main form, you will need to create an instance of that MainForm class. The problem that beginning programmers have is trying to access properties or call functions on the class itself , rather than an instance (object) of the class. This is why I am careful to say that you need to maintain a reference to the floating palette—by this I mean your specific instance of the floating palette form class. Make sure that you understand this distinction; consult your favorite text on programming in C# (or any other object-oriented language) for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM