动态创建的文本框在Usercontrol中不可见 - winforms,C#

时间:2015-04-16 10:52:20

标签: c# .net winforms

我正在尝试创建文本框并在用户控件中动态绘制圆圈。圆圈是可见的,但运行我的应用程序时文本框不可见。我在代码中遗漏了什么吗?

请找到以下代码,

public partial class uscCircle : UserControl
{
    public uscCircle()
    {
        InitializeComponent();
    }

    public void DrawCircle(PaintEventArgs args, int x, int y, int width, int height)
    {
        Pen pen = new Pen(Color.Red, 3);
        Brush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
        args.Graphics.FillEllipse(myBrush, x - width / 2, y - height / 2, width, height);
    }

    public void AddTextBox(string text, int x, int y, int width, int height)
    {
        markerlabel.Size = new Size(40, 15);
        markerlabel.Text = text;
        markerlabel.TextAlign = HorizontalAlignment.Center;
        markerlabel.BorderStyle = BorderStyle.FixedSingle;
        markerlabel.ForeColor = Color.White;
        markerlabel.BackColor = Color.Red;
        markerlabel.Location = new Point(x - (width + 14), y + height / 2);
        markerlabel.Visible = true;
        this.Controls.Add(markerlabel);
    }
}

public partial class CalibrationForm : Form
{
    private CalibrationForm_Click(object sender, EventArgs e)
    {
        int x = e.X;
        int y = e.Y;
        DrawTextBox(X, Y, 25, 25, "1234", "abcd");
    }

    private void DrawCircle(int x, int y, int width, int height, string MarkerID, string type)
    {
        PaintEventArgs arg = new PaintEventArgs(this.CreateGraphics(), new Rectangle());
        uscCircle circle = new uscCircle();
        circle.DrawCircle(arg, x, y, width, height);
        circle.AddTextBox(ID, x, y, width, height);
        circle.AddTextBox(type, x + 40, y, width, height);
    }
}

2 个答案:

答案 0 :(得分:0)

我看不到您在表单中添加uscCircle的位置。如果没有显示,则文本框也不会显示。

如:

private void DrawCircle(int x, int y, int width, int height, string MarkerID, string type)
{
    uscCircle circle = new uscCircle();
    circle.AddTextBox(ID, x, y, width, height);
    circle.AddTextBox(type, x+40, y, width, height);
    this.Controls.Add(circle);
}

答案 1 :(得分:0)

您正在将TextBox添加到新的用户控件中,然后不再使用。

uscCircle circle = new uscCircle();
circle.AddTextBox(ID, x, y, width, height);
circle.AddTextBox(type, x+40, y, width, height);

您需要将'uscCircle'添加到Form

的控件中
this.Controls.Add(uscCircle); // Must  be in your Form file

或者将TextBox-Generation代码移动到表单