Results 1 to 11 of 11

Thread: .NET visual form designer, only 50 lines of code!!!

  1. #1

    Thread Starter
    Taking a Break
    Join Date
    Jan 2020
    Posts
    1,340

    .NET visual form designer, only 50 lines of code!!!

    Copyright statement: free commercial use, but the source URL link needs to be kept
    CODE IS C#,I CHANGE TO VB.NET BY SHARPDEVELOP 4.4
    The article is reproduced in.
    https://www.cnblogs.com/panjiwen/arc...10/272980.html
    http://www.cnblogs.com/Files/panjiwen/FormDesigner.zip

    Name:  .NET_FormDesigner.jpg
Views: 194
Size:  30.8 KB

    Try to write FORM designer (1) START
    Eddie Sheffield, as the first person outside of Microsoft to discover the enablement of Form Designer, is truly remarkable.
    Today, a few years later, you can already find some information about Form Designer, although not many and not comprehensive, there are still some after all. The information I have is:
    1. The Chinese version of "Dissecting a C# Application Inside SharpDevelop", Chapter 16 is devoted to the Form designer;
    2. A small example downloaded on windowsforms.net is obviously an idea obtained from SharpDevelop;
    3. WinRes is a resource localization tool that comes with VS2005/.net FrameWork2.0. You can decompile and see its source code.

    Comparing 1, 2 and 3, I found that although both have implemented Form Designer, the design ideas are quite different. Obviously, the method used by WinRes should be concise. The reason is that .net 2.0 enhances the function of the designer and simplifies the calling method of the designer. Due to time constraints, it may be difficult to write a full-featured Form designer in the short term. What I want to do is to do some tests and come up with a basic method for writing a Form designer based on .net 2.0.
    I want to write a small example without any function but can see the designer style to test. Later, you will slowly increase the design functions, such as adding and deleting controls, setting properties, and saving files.
    Our steps are as follows:
    Create a new C# WindowsForm scheme in VS2005 (the version I use is RC), add System. Design in the project reference, and add it first in the code of Form1

    using System.ComponentModel.Design;
    Then double-click the Form, and write the following code in the Load event of the Form:

    DesignSurface surface = new DesignSurface();
    surface.BeginLoad(typeof(Form));
    Control view = (Control)surface.View;
    view.Dock = DockStyle.Fill;
    this.Controls.Add(view);
    Then run the program. This is the simplest and useless form designer, but at least we can adjust the size of the designed Form

    Code:
    'VB.NET CODE example
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Data
    Imports System.Drawing
    Imports System.Text
    Imports System.Windows.Forms
    
    Imports System.Drawing.Design
    Imports System.ComponentModel.Design
    
    Imports System.Reflection
    
    Public Partial Class Form1
    	Inherits Form
    	Public Sub New()
    		InitializeComponent()
    	End Sub
    
    
    	Private Sub Form1_Load(sender As Object, e As EventArgs)
    		Dim surface As New DesignSurface()
    
    		toolBoxService = New DemoToolboxService()
    		toolBoxService.ToolBox = New ListBox()
    
    		toolBoxService.ToolBox.Items.Add("Point")
    
    		toolBoxService.ToolBox.Items.Add(New ToolboxItem(GetType(Button)))
    		toolBoxService.ToolBox.Items.Add(New ToolboxItem(GetType(TextBox)))
    		toolBoxService.ToolBox.Items.Add(New ToolboxItem(GetType(Label)))
    		toolBoxService.ToolBox.Items.Add(New ToolboxItem(GetType(TabControl)))
    		toolBoxService.ToolBox.Items.Add(New ToolboxItem(GetType(StatusBar)))
    
    		' Assembly a1=Assembly.LoadFrom(@"D:\Dotnet\MyControl.dll");
    		' toolBoxService.ToolBox.Items.Add(new ToolboxItem(a1.GetType("MyControl.ComboBoxField")));
    
    		toolBoxService.ToolBox.Dock = DockStyle.Fill
    		Me.panel1.Controls.Add(toolBoxService.ToolBox)
    
    
    		Dim container As IServiceContainer = TryCast(surface.GetService(GetType(IServiceContainer)), IServiceContainer)
    
    		menuCommandService = New MenuCommandService(surface)
    
    		If container IsNot Nothing Then
    			container.AddService(GetType(IToolboxService), toolBoxService)
    			container.AddService(GetType(IMenuCommandService), menuCommandService)
    		End If
    
    		surface.BeginLoad(GetType(Form))
    		Dim view As Control = DirectCast(surface.View, Control)
    		view.Dock = DockStyle.Fill
    		Me.splitContainer1.Panel1.Controls.Add(view)
    		Me.propertyGrid1.SelectedObject = surface.ComponentContainer.Components(0)
    
    
    		selectionService = TryCast(surface.GetService(GetType(ISelectionService)), ISelectionService)
    		AddHandler selectionService.SelectionChanged, New EventHandler(AddressOf selectionService_SelectionChanged)
    	End Sub
    
    	Private Sub selectionService_SelectionChanged(sender As Object, e As EventArgs)
    		Dim selection As Object()
    		If selectionService.SelectionCount = 0 Then
    			propertyGrid1.SelectedObject = Nothing
    		Else
    			selection = New Object(selectionService.SelectionCount - 1) {}
    			selectionService.GetSelectedComponents().CopyTo(selection, 0)
    			propertyGrid1.SelectedObjects = selection
    		End If
    
    	End Sub
    
    	Private toolBoxService As DemoToolboxService
    	Private selectionService As ISelectionService
    	Private menuCommandService As MenuCommandService
    
    
    	Private Sub selectAllToolStripMenuItem_Click(sender As Object, e As EventArgs)
    		menuCommandService.GlobalInvoke(StandardCommands.SelectAll)
    	End Sub
    
    	Private Sub deleteToolStripMenuItem_Click(sender As Object, e As EventArgs)
    		menuCommandService.GlobalInvoke(StandardCommands.Delete)
    	End Sub
    
    End Class
    Last edited by xiaoyao; May 7th, 2021 at 02:46 AM.

  2. #2
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    461

    Re: .NET visual form designer, only 50 lines of code!!!

    First time you post something intresting.

    It works very well, and I even added in 1 lin, listbox, and other controls.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,970

    Re: .NET visual form designer, only 50 lines of code!!!

    This is not a question. The place for your code example is the C# codebank, not the vbgeneral forum...

  4. #4

    Thread Starter
    Taking a Break
    Join Date
    Jan 2020
    Posts
    1,340

    Re: .NET visual form designer, only 50 lines of code!!!

    Quote Originally Posted by .paul. View Post
    This is not a question. The place for your code example is the C# codebank, not the vbgeneral forum...
    Because the original code is C#, I also posted the VB.NET code. I don't have any website space to save, you just need to do it yourself.

  5. #5

    Thread Starter
    Taking a Break
    Join Date
    Jan 2020
    Posts
    1,340

    Re: .NET visual form designer, only 50 lines of code!!!

    Quote Originally Posted by Thierry69 View Post
    First time you post something intresting.

    It works very well, and I even added in 1 lin, listbox, and other controls.
    Some people think itís good, and Iím very happy to praise me, I also like to be praised

  6. #6

    Thread Starter
    Taking a Break
    Join Date
    Jan 2020
    Posts
    1,340

    Re: .NET visual form designer, only 50 lines of code!!!

    October 2005 Essay Archives-Canghai Yueming-Blog Garden
    Those who are interested can use the translation tool to view the history of the author's development process and it took 2 months.

    https://www.cnblogs.com/panjiwen/archive/2005/10.html
    https://www.cnblogs.com/panjiwen/archive/2005/11.html

  7. #7

    Thread Starter
    Taking a Break
    Join Date
    Jan 2020
    Posts
    1,340

    Re: .NET visual form designer, only 50 lines of code!!!

    Try to write a FORM designer (5) Let the designer use custom controls
    In the comment of "Trying to write a Form designer (3) Adding controls to the form", the enthusiastic follower Leejee raised the question of custom controls. So I made a small test to realize the use of custom controls in the designer.
    First prepare a custom control. Create a new Windows control scheme, name it MyControl, add a user control named ComboBoxField, put a Label and a ComboBox on the user control, and generate a solution. Copy the generated dll file to the test directory D:\Dotnet.

    Open the designer program, add a reference to MyControl.Dll in the project, add Using MyControl; in the code of the main form, and add a line of code at the end of the form toolBoxService.ToolBox.Items.Add(Ö.);:

    toolBoxService.ToolBox.Items.Add(new ToolboxItem(typeof(MyControl.ComboBoxField)));
    Run the program, you can indeed use this custom control like other standard controls. Hey, there is no accident, it is as simple as the previous experiment.
    But think about it carefully, something went wrong, we need to add a reference to the file where the control is located in the project, and we need to write the class name of the control in the code. In other words, when we write the designer, we must know which custom controls we want to use. When we add custom controls to VS, VS doesn't know in advance what we want to add. What should I do to realize this function? Of course "reflection" should be used.

    Delete the reference we just added in the project, and delete the two statements we just wrote in the main form code.
    Write in the place where the second sentence was written:

    Assembly a1=Assembly.LoadFrom(@"D:\Dotnet\MyControl.dll");
    toolBoxService.ToolBox.Items.Add(new ToolboxItem(a1.GetType("MyControl.ComboBoxField")));
    Run the program, the effect is the same as the previous one. It seems that the dynamic library file name and type name are also hard-coded into the code here, but it is easy to read these two strings from the configuration file, so that you can use customization when you use the designer. Controls.

    Related chapters:
    Try to write a Form designer (1) Start
    Try to write Form designer (2) PropertyGrid
    Try to write Form designer (3) Add controls on the form
    Try to write the Form designer (4) Modify the properties of the controls on the form

    Postscript: This article was written last night. I later felt that something was wrong when I was sleeping. That is, my method is to use reflection before loading the toolbox. I guess VS's approach is to use this control when you need to use it. reflection. The solution to this problem is to modify the ToolboxService. I will implement this feature in a more complete version in the future. 2005/11/07

    Good article to like, follow me to bookmark this article

  8. #8

    Thread Starter
    Taking a Break
    Join Date
    Jan 2020
    Posts
    1,340

    Re: .NET visual form designer, only 50 lines of code!!!

    Repost:The goal of my topic is to write a usable form designer, what's the use? For example, after your program is released, users feel that the position of a certain control needs to be adjusted, and the font color of a certain control needs to be modified. It doesn't matter, the user just needs to modify it. A more complicated application is that the user wants to add a field to a certain document. It doesn't matter, the user or the implementer adds it by themselves, don't change the code, don't recompile. Even if the user wants to add some processing, the implementation staff can write the code in the designer on the spot, and the system can compile it and call it at runtime.
    Last edited by xiaoyao; May 7th, 2021 at 02:47 AM.

  9. #9
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    926

    Re: .NET visual form designer, only 50 lines of code!!!

    Impressed.

  10. #10

    Thread Starter
    Taking a Break
    Join Date
    Jan 2020
    Posts
    1,340

    Re: .NET visual form designer, only 50 lines of code!!!

    Quote Originally Posted by .paul. View Post
    This is not a question. The place for your code example is the C# codebank, not the vbgeneral forum...
    In fact, I rarely use C#, in fact it belongs to C#.
    But I posted this mainly for the users of VB.NET, and I also provided the source code of VB.NET. , Under normal circumstances, the subject should be automatically displayed in multiple sections, but he has only one record in the database, which is equivalent to an article, which is displayed in different channel columns virtually, and accepts simultaneous comments from users of these different programming languages.

    Or should I post 2 topics? It's just a bit repetitive.

  11. #11
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,163

    Re: .NET visual form designer, only 50 lines of code!!!

    Hi xioyao,

    I searched the Web for DemoToolboxService, but apart from the present thread and a handful of Chinese-language web pages, the only English-language occurrence I found was in a 2009 CodeBank article. It seems to be well coded but it doesn't define a Property or Field called ToolBox. So the code
    Code:
            toolBoxService = New DemoToolboxService()
            toolBoxService.ToolBox = New ListBox()
    is marked as an error. Is there a later version of DemoToolBoxService that does implement the ToolBox property or field?

    Excuse me for not understanding that beautiful Chinese lettering.

    BB

    EDIT: I found the code of your own DemoToolBoxService -- fortunately (for me) in VB English apart from comments -- embedded in one of the "Chinese" links I found earlier: https://www.csdn.net/tags/MtTacg4sMT...0O0OO0O0O.html. Obviously, I should have looked there first. It took a certain amount of tinkering but in the end it works well as described.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width