BPSK
BPSK

Reputation: 15

How to change TextBox location at runtime

I'm creating TextBoxes at runtime and add an EventHandler to eac, but I can only move the last one created, when I try to move a previous one, it disappears.

This is my code:

int Naslov_rnd;
TextBox tb;

private void Naslov_p_Click(object sender, EventArgs e)
{
    Naslov_rnd++;

    tb = new TextBox();
    VizitKartica.SuspendLayout();

    tb.Location = new Point(0, 0);
    tb.Multiline = true;
    tb.Size = new Size(200, 20);
    tb.BorderStyle = BorderStyle.None;
    tb.BackColor = Color.DodgerBlue;
    tb.ForeColor = Color.White;
    tb.Name = "Naslov_" + Naslov_rnd.ToString(); ;
    tb.Text = "Dodajte Vaš naslov";
    tb.Font = new Font("Microsoft Sans Serif", 12);

    VizitKartica.Controls.Add(tb);
    elementi_lista.AddItem(tb.Name);

    VizitKartica.ResumeLayout(true); Controls collection

      tb.MouseMove += new MouseEventHandler(tb_MouseMove);
    tb.MouseDown += new MouseEventHandler(tb_MouseDown);
}

protected void tb_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        tb.Left = e.X + tb.Left;
        tb.Top = e.Y + tb.Top;
    }
}

protected void tb_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        Point MouseDownLocation = e.Location;
    }
}

Upvotes: 0

Views: 885

Answers (1)

Mousa Alfhaily
Mousa Alfhaily

Reputation: 1290

As @LarsTech said, you cannot make one TextBox object point to all of the TextBoxes that will be created, a simple and effective solution to this is to use the sender object.

The EventHandler provides you with an argument wich will get passed to the method, and it will point to the control that caused the event to be fired.

Since we know that all the TextBoxes are sharing the same event and they are all TextBoxes, we can type-cast the sender object to the TextBox class and then use it.

Here is how :

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    int Naslov_rnd;

    private void button1_Click(object sender, EventArgs e)
    {
        Naslov_rnd++;

        TextBox tb = new TextBox();
        VizitKartica.SuspendLayout();

        tb.Location = new Point(0, 0);
        tb.Multiline = true;
        tb.Size = new Size(200, 20);
        tb.BorderStyle = BorderStyle.None;
        tb.BackColor = Color.DodgerBlue;
        tb.ForeColor = Color.White;
        tb.Name = "Naslov_" + Naslov_rnd.ToString();
        tb.Text = "Dodajte Vaš naslov";
        tb.Font = new Font("Microsoft Sans Serif", 12);

        VizitKartica.Controls.Add(tb);

        VizitKartica.ResumeLayout(true); 

        tb.MouseMove += new MouseEventHandler(tb_MouseMove);
        tb.MouseDown += new MouseEventHandler(tb_MouseDown);
    }

    protected void tb_MouseMove(object sender, MouseEventArgs e)
    {
        TextBox tb2 = (TextBox) sender;
        if (e.Button == MouseButtons.Left)
        {
            tb2.Left = e.X + tb2.Left;
            tb2.Top = e.Y + tb2.Top;
        }
    }

    protected void tb_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            Point MouseDownLocation = e.Location;
        }
    }
}

Hope that helped you and what you are looking for.

Upvotes: 1

Related Questions