如何在运行时命名ShapeContainer

时间:2014-02-09 18:38:24

标签: c# winforms canvas shape

我尝试制作一个绘制网络地图的应用程序 我在表单中使用一个面板,并在此面板中动态创建ShapeContainer作为画布。

在这个画布中我动态创建一些形状和线条(为了创建这些形状,我使用两种方法)

for lineshape:

private void CreateLine(int StartX,int StartY,int EndX,int EndY,Color lineColor,String ControlName)
    {

        LineShape newline = new LineShape();
        canvas.Parent = panMap;
        newline.Parent = canvas;
        newline.StartPoint = new Point(StartX+ZoomScale , StartY+ZoomScale );
        newline.EndPoint = new Point(EndX + ZoomScale, EndY + ZoomScale);
        newline.BorderColor = lineColor;
        newline.BorderWidth = 2;
        newline.BorderStyle = System.Drawing.Drawing2D.DashStyle.Solid;
        newline.Name = "Link_" + ControlName;
        newline.Tag = "Link_" + ControlName;
        newline.BringToFront();
        canvas.Shapes.Add(newline);


    }

并且对于创建矩形我制作此方法:

private void CreateBox(int X, int Y, int ObjectType)
    {
        ShapeContainer canvas = new ShapeContainer();
        RectangleShape box = new RectangleShape();
        box.Parent = canvas;
        box.Size = new System.Drawing.Size(100, 90);
        box.Location = new System.Drawing.Point(X, Y);
        box.Name = "Box" + ObjectType.ToString();
        box.BackColor = Color.Transparent;
        box.BorderColor = Color.Transparent;
        box.BackgroundImage = img.Images[ObjectType];
        box.BackgroundImageLayout = ImageLayout.Stretch;
        box.BorderWidth = 0;


    }

我将这些方法称为:

        CreateBox(600, 160, 4);
        CreateBox(600, 200, 3);

        CreateLine(75, 83, 227, 176, Color.Green, "1");
        CreateLine(227, 176, 367, 95, Color.Green, "2");

----现在,我有一些问题: 1.如何设置此形状的名称(用于其他方法)?     例如在文本框控件中我使用:

TextBox txtbx = (TextBox)Controls["txtCityName"];

我能为形状做些什么?

  1. 如何为这个形状创建方法? 例如:

    newline.Click + = newliclick(对象发件人,EventArgs事件,Color linecolor)!?

  2. 我希望向方法

    发送多个对象

    对不起,我很抱歉,如果我不能描述我的问题(我的英语不太好),请原谅。

1 个答案:

答案 0 :(得分:1)

可能但不是那样。您的方法签名应该与事件处理程序匹配。您可以通过继承EventArgs来创建一个类,但在这种情况下似乎没有必要。

无论如何,只需为事件处理程序定义一个方法,另一个方法就是执行Color参数的工作。在click事件的内部调用另一个方法并传递Color参数。例如:< / p>

newline.Click += newlineClick;

private void newlineClick(object sender, EventArgs e)
{
    newlineClickImpl(sender,e, Color.Blue);
}

private void  newlineClickImpl(object sender, EventArgs e,Color color)
{
    ...
}