Before writing this article, I would like to sincerely thank Microsoft for its Jeffrey Tan Sir, he could have understood my unreasonable questions in English and spent precious time researching them. He helped me solve the problem that I was puzzled for a week. My problem at the time was that I wrote some statements to implement standard menu commands, but I couldn't get the correct results during debugging, in fact, the two examples I have studied use methods similar to mine, but they can all be executed normally. I have not found the reason and have to ask for help.
In theWriteFormDesigner Attempt(Thu)Modify controls on a form"I raised the question of how to delete controls. Shan Wei, the author of my role, also proposed how to achieve control alignment. The answer to all these questions is to useMenucommandservice,In. NET 1.0/1.1The solution is to manually write an implementationImenucommandserviceInterface Class, add its instance to the service container, and. NET 2.0We have providedMenucommandservice.
Open main formCodeInterface,Form1Add a private variable:
Private Menucommandservice;
Modify Load The Event code has been modified several times, so I will post it all here: Private Void Form1_load ( Object Sender, eventargs E)
{
Designsurface Surface = New Designsurface ();
Toolboxservice = New Demotoolboxservice ();
Toolboxservice. toolbox = New ListBox ();
Toolboxservice. toolbox. Items. Add ( " Point " );
Toolboxservice. toolbox. Items. Add ( New Toolboxitem ( Typeof (Button )));
Toolboxservice. toolbox. Items. Add ( New Toolboxitem ( Typeof (Textbox )));
Toolboxservice. toolbox. Items. Add ( New Toolboxitem ( Typeof (Label )));
Toolboxservice. toolbox. Items. Add ( New Toolboxitem ( Typeof (Tabcontrol )));
Toolboxservice. toolbox. Items. Add ( New Toolboxitem ( Typeof (Statusbar )));
// Assembly a1 = assembly. loadfrom (@ "D: \ DOTNET \ mycontrol. dll ");
// Toolboxservice. toolbox. Items. Add (New toolboxitem (a1.gettype ("mycontrol. comboboxfield ")));
Toolboxservice. toolbox. Dock = Dockstyle. Fill;
This . Panel1.controls. Add (toolboxservice. Toolbox );
Iservicecontainer container = Surface. getservice ( Typeof (Iservicecontainer )) As Iservicecontainer;
Menucommandservice = New Menucommandservice (surface );
If (Container ! = Null )
{
Container. addservice (Typeof(Itoolboxservice), toolboxservice );
Container. addservice (Typeof(Imenucommandservice), menucommandservice );
}
Surface. beginload ( Typeof (Form ));
Control View = (Control) surface. view;
View. Dock = Dockstyle. Fill;
This . Splitcontainer1.panel1. Controls. Add (View );
This . Propertygrid1.selectedobject = Surface. componentcontainer. components [ 0 ];
Selectionservice = Surface. getservice ( Typeof (Iselectionservice )) As Iselectionservice;
Selectionservice. selectionchanged + = New Eventhandler (selectionservice_selectionchanged );
}
We will test the deletion function and addMenustrip, InMenustripAddEditMenu item, inEditAdd a sub-menu under a menu itemDelete,Set the shortcut keyDel,IsDeleteWrite Event code for menu items:
Private Void Deletetoolstripmenuitem_click ( Object Sender, eventargs E)
{
Menucommandservice. globalinvoke (standardcommands. delete );
}
Run the program, add several controls to the designer, select one or more controls, and then pressDelKey or click the menuEdit-> Delete,The selected control is deleted.
Other menu commands such as select all, such as alignment, can be implemented in this way.StandardcommandsToo many commands are included.
In addition to the serialization of resources, code generation, and event processing functions, our designer has achieved success.
I will be very busy recently, and the rest of the content will be written later. Thank you for your support and encouragement!
Related chapters:
WriteFormDesigner Attempt(I) Start
WriteFormDesigner Attempt(II) Propertygrid
WriteFormDesigner Attempt(3.)Add controls to a form
Try to write the Form Designer (4) modify the control attributes on the form
Try to write the Form Designer (5) to make the designer use custom controls