How to Create a Graph in Visual Studio

May 11, 2011 at 9:26 AM

Hey,

i am new to .NET development. I tried to create a simple example of NODEXL with C# and Windows Forms. I used the source code from the help file:

 

using System;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.NodeXL.Core;
using Microsoft.NodeXL.Visualization.Wpf;


namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        private NodeXLControl nodeXLControl1;
        public Form1()
        {
            InitializeComponent();

            nodeXLControl1 = new NodeXLControl();
            elementHost1.Child = nodeXLControl1;


           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            PopulateAndDrawGraph();
        }
        protected void PopulateAndDrawGraph()
                {
                    // Get the graph's vertex collection.

                    Console.Write("bla");       

                    IVertexCollection oVertices = nodeXLControl1.Graph.Vertices;
           
                    // Add three vertices.
           
                    IVertex oVertexA = oVertices.Add();
                    IVertex oVertexB = oVertices.Add();
                    IVertex oVertexC = oVertices.Add();
           
                    // Change the color, radius, and shape of vertex A.
           
                    oVertexA.SetValue(ReservedMetadataKeys.PerColor,
                        Color.FromArgb(255, 255, 0, 255));
           
                    oVertexA.SetValue(ReservedMetadataKeys.PerVertexRadius, 20F);
           
                    oVertexA.SetValue(ReservedMetadataKeys.PerVertexShape,
                        VertexShape.Sphere);
           
                    // Draw vertex B as a Label, which is a rectangle containing text.
           
                    oVertexB.SetValue(ReservedMetadataKeys.PerVertexShape,
                        VertexShape.Label);
           
                    oVertexB.SetValue(ReservedMetadataKeys.PerVertexLabel, "Label");
           
                    // Set the label's text and fill colors.
           
                    oVertexB.SetValue(ReservedMetadataKeys.PerColor,
                        Color.FromArgb(255, 220, 220, 220));
           
                    oVertexB.SetValue(ReservedMetadataKeys.PerVertexLabelFillColor,
                        Color.FromArgb(255, 0, 0, 0));
           
                    // Annotate vertex C with text that is drawn outside the vertex.  All
                    // shapes except Label can be annotated.
           
                    oVertexC.SetValue(ReservedMetadataKeys.PerVertexLabel, "Annotation");
           
                    // Get the graph's edge collection.
           
                    IEdgeCollection oEdges = nodeXLControl1.Graph.Edges;
           
                    // Connect the vertices with directed edges.
           
                    IEdge oEdge1 = oEdges.Add(oVertexA, oVertexB, true);
                    IEdge oEdge2 = oEdges.Add(oVertexB, oVertexC, true);
                    IEdge oEdge3 = oEdges.Add(oVertexC, oVertexA, true);
           
                    // Customize their appearance.
           
                    oEdge1.SetValue(ReservedMetadataKeys.PerColor,
                        Color.FromArgb(255, 55, 125, 98));
           
                    oEdge1.SetValue(ReservedMetadataKeys.PerEdgeWidth, 3F);
                    oEdge1.SetValue(ReservedMetadataKeys.PerEdgeLabel, "This is edge 1");
           
                    oEdge2.SetValue(ReservedMetadataKeys.PerEdgeWidth, 5F);
                    oEdge2.SetValue(ReservedMetadataKeys.PerEdgeLabel, "This is edge 2");
           
                    oEdge3.SetValue(ReservedMetadataKeys.PerColor,
                        Color.FromArgb(255, 0, 255, 0));
           
                    nodeXLControl1.DrawGraph(true);
                }
           

    }
}

I have an error here:

elementHost1.Child = nodeXLControl1; (the name elementhost1 is not available)

 

What is missing here? Do I have to design something with the Designer first?

 

Thanks

Peter

May 11, 2011 at 11:40 AM

Sorry, you can close this topic.

I found the solution here:

http://nodexl.codeplex.com/discussions/75822