简体   繁体   中英

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. 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.

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

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

or you move the TextBox-Generation code into your form

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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