I am new to using C# and trying to create a Windows Form App project.

Form1.Designer.cs is here and below is the Form1.cs (the form that I intend to create)

using System;
using System.Windows.Forms;

namespace LearnCSharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            /* --Upper bound of Block-- A */
            System.Windows.Forms.Form form = new System.Windows.Forms.Form();
            //create a viewer object 
            Microsoft.Msagl.GraphViewerGdi.GViewer viewer = new Microsoft.Msagl.GraphViewerGdi.GViewer();
            //create a graph object 
            Microsoft.Msagl.Drawing.Graph graph = new Microsoft.Msagl.Drawing.Graph("graph");
            //create the graph content 
            graph.AddEdge("A", "B");
            graph.AddEdge("B", "C");
            graph.AddEdge("A", "C").Attr.Color = Microsoft.Msagl.Drawing.Color.Green;
            graph.FindNode("A").Attr.FillColor = Microsoft.Msagl.Drawing.Color.Magenta;
            graph.FindNode("B").Attr.FillColor = Microsoft.Msagl.Drawing.Color.MistyRose;
            Microsoft.Msagl.Drawing.Node c = graph.FindNode("C");
            c.Attr.FillColor = Microsoft.Msagl.Drawing.Color.PaleGreen;
            c.Attr.Shape = Microsoft.Msagl.Drawing.Shape.Diamond;
            //bind the graph to the viewer 
            viewer.Graph = graph;
            //associate the viewer with the form 
            form.SuspendLayout();
            viewer.Dock = System.Windows.Forms.DockStyle.Fill;
            form.Controls.Add(viewer);
            form.ResumeLayout();
            //show the form 
            form.ShowDialog();
            /* --Lower bound of Block-- A */
        }
    }
}

Block A is a template that I get from MSAGL code sample

When I build the solution and run the .exe, the Form1 and the MSAGL graph show consecutively/separately (The MSAGL graph is not an element in Form1).


My question is: Here is the windows form that I want to create so that the MSAGL graph can be the element of Form1 that I want to create in this green area. (and if possible can be dragged/modified the properties as it is a button or any element in the form). How to implement that? Form

1

There are 1 answers

2
Adam White On BEST ANSWER

Well that's because inside the constructor for Form1, you create a totally new and separate form: System.Windows.Forms.Form form = new System.Windows.Forms.Form(); and display it: form.ShowDialog();

Instead, just add the graph to Form1: this.Controls.Add(viewer); and since your building all this and adding it in the constructor, no need to SuspendLayout/ResumeLayout.

So once again, to recap what you should do, delete this line:

System.Windows.Forms.Form form = new System.Windows.Forms.Form();

and any lines that reference form, and then add this line:

this.Controls.Add(viewer);

and leave all other lines as is.