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

时间:2021-08-07 20:53:59

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

2 个解决方案

#1


0  

I don't see where you add uscCircle to the form. If that isn't displayed, neither will the textbox be.

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

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

#2


0  

You are adding your TextBox into a new User control which is then not used.

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

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”添加到Form的控件中

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

or you move the TextBox-Generation code into your form

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

#1


0  

I don't see where you add uscCircle to the form. If that isn't displayed, neither will the textbox be.

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

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

#2


0  

You are adding your TextBox into a new User control which is then not used.

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

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”添加到Form的控件中

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

or you move the TextBox-Generation code into your form

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