在C#WinForm中使用以编程方式创建的Panel?

时间:2014-07-15 12:24:05

标签: c# winforms drawing mouseevent

我的数据库中有一些问题与某些答案类型有关。我目前在Android应用程序上使用它们进行问卷调查,但我也想在桌面应用程序上查看它们,这样您就可以大致了解它的外观,并更容易添加问题和东西。 Buuuuuuut!以C#形式以编程方式添加内容似乎比在android中更省力。我已经完成了所有基本功能(texbox,复选框,下拉菜单),但我现在正尝试绘制一个。

我认为我可以使用Panel,因为我可以使用它,但它似乎并没有起作用。我一直在浏览和搜索该怎么做,因为我对C#Forms并不十分熟悉。我设置了一个MouseDownMouseMove事件处理程序,在MouseMove事件处理程序中,我尝试绘制它并调用代码并执行该行,但没有任何反应。它或者是一些非常小而且愚蠢的东西,或者我以错误的方式处理它。如果我不能这样做,我不会介意其他任何关于我如何做的建议。它全部动态完成,因此它会向TableLayoutPanel添加问题+答案类型,因此需要将其添加到该switch (questionnaireAnswerType.questionnaireAnswerTypeId) { case 1: ComboBox dropDown = new ComboBox(); dropDown.Anchor = AnchorStyles.Left; String[] items = question.dropDownContents.Split(';'); dropDown.Items.AddRange(items); tablePanel.Controls.Add(dropDown, 1, index); break; case 2: TextBox textBox = new TextBox(); textBox.Size = new System.Drawing.Size(70, 20); tablePanel.Controls.Add(textBox, 1, index); break; case 3: CheckBox checkBox = new CheckBox(); checkBox.Anchor = AnchorStyles.Left; tablePanel.Controls.Add(checkBox, 1, index); break; case 4: Panel drawPanel = new Panel(); System.Drawing.Size size = new System.Drawing.Size(); drawPanel.MouseDown += new MouseEventHandler(drawPanel_MouseDown); drawPanel.MouseMove += new MouseEventHandler(drawPanel_MouseMove); size.Height = 100; size.Width = 500; drawPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; drawPanel.Size = size; AddTableRow(); tablePanel.Controls.Add(drawPanel, 0, index +1); tablePanel.SetColumnSpan(drawPanel, 2); break; } 。据我所知,Canvas不是一个控件,所以我无法添加它。

这是相关代码:

 void drawPanel_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            Line line = new Line();

            line.Stroke = System.Windows.SystemColors.WindowFrameBrush;
            line.X1 = currentPoint.X;
            line.Y1 = currentPoint.Y;
            line.X2 = e.Location.X;
            line.Y2 = e.Location.Y;

            currentPoint = e.Location;

            using (Graphics g = ((Panel) sender).CreateGraphics())
            {
                g.DrawLine(new Pen(Color.Black, 3), new System.Drawing.Point(currentPoint.X, currentPoint.Y), new System.Drawing.Point(e.Location.X, e.Location.Y));
            }
        }
    }

    void drawPanel_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
            currentPoint = e.Location;
    }

这些是mousedown事件:

{{1}}

0 个答案:

没有答案