简体   繁体   English

动态创建的文本框在用户控件中不可见-Winforms,C#

[英]Dynamically created textbox not visible in Usercontrol - winforms, C#

I am trying to create textboxes and draw circles dynamically within a user control. 我正在尝试创建文本框并在用户控件内动态绘制圆。 The circle is visible but the textboxes are not visible when I run my application. 运行应用程序时,圆圈可见,但文本框不可见。 Am I missing something in the code? 我在代码中缺少什么吗?

Please find the code below, 请在下面找到代码,

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);
    }
}

I don't see where you add uscCircle to the form. 我看不到将uscCircle添加到表单的位置。 If that isn't displayed, neither will the textbox be. 如果未显示,则文本框也不会显示。

Such as: 如:

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);
}

You are adding your TextBox into a new User control which is then not used. 您正在将TextBox添加到新的User控件中,然后不再使用它。

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

You either need to add your 'uscCircle' into the controls of the Form 您要么需要将“ uscCircle”添加到表单的控件中

this.Controls.Add(uscCircle); // Must  be in your Form file

or you move the TextBox-Generation code into your form 或者您将TextBox-Generation代码移到表单中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM