如何在运行时更改 TextBox 位置 [英] How to change TextBox location at runtime

查看:47
本文介绍了如何在运行时更改 TextBox 位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时创建 TextBoxes 并向 eac 添加一个 EventHandler,但是当我尝试移动前一个时,我只能移动创建的最后一个,它消失了.

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.

这是我的代码:

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

推荐答案

正如 @LarsTech 所说,你不能做一个TextBox 对象指向将被创建的所有 TextBoxes,一个简单有效的解决方案是使用 sender 对象.

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.

EventHandler 为您提供了一个将传递给方法的参数,它将指向导致事件被触发的控件.

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.

既然我们知道所有的 TextBoxes 都共享同一个事件并且它们都是 TextBoxes,我们可以将发送者对象类型转换为 TextBox 类然后使用它.

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.

方法如下:

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.

这篇关于如何在运行时更改 TextBox 位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆