移动动态添加的文本框 [英] moving dynamically added textbox

查看:57
本文介绍了移动动态添加的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序启动后,如何移动动态添加的文本框?
我希望使用该程序的人能够移动它.
这是我用来在文本框中放入的代码.

How would i move a dynamically added text box after the program has started?
I would like the person that uses the program to be able to move it.
and here is the code that i''m using to put in a text box.

private void button1_Click(object sender, System.EventArgs e)
{
    TextBox textbox1 = new TextBox();
    textbox1.Location = new Point(25, 25);
    this.Controls.Add(textbox1);
}



p.s.上面的代码会为我提供无限数量的文本框,因为那是我想要的.



p.s. will the code above give me infinite amounts of text boxes because that''s kinda what i want.

sorry to SA for not wording the question right.

推荐答案

您以完全相同的方式移动它:myTextBox.Location = new Point(/* .. */);或myTextBox.Left = /* ... */; myTextBox.Top = /* ... */.不需要无效,因为所有无效都在属性设置器中实现.

您的理解或设计时间是错误的:所有控件都是在运行时创建的.设计人员是在Visual Studio设计期间工作的人.它仅有助于编写在应用程序运行时实际执行的代码.为了确保这一点,只需看一下自动生成的C#代码即可.顺便说一句,如果您只知道如何与设计人员一起学习代码,这也是学习如何编写代码的简单方法:查看自动生成的文件,然后查看.

—SA
You move it in exact same way: myTextBox.Location = new Point(/* .. */); or myTextBox.Left = /* ... */; myTextBox.Top = /* ... */. No invalidation is required, as it is all implemented in the property setters.

You understanding or design time is wrong: all controls are created during run-time. The designer is something working in the Visual Studio design time; it only helps to write code which actually is executed during your application''s run-time. To make sure, just take a look at the auto-generated C# code. By the way, this is also a simple way of learning how to write some code if you only know how to do it with the designer: look at the auto-generated file and see.

—SA


修改您的按钮,单击事件,如下所示

Modify you button click Event as below

TextBox textbox1 = new TextBox();
textbox1.Name = "Txt1";// this is useful to identify the object
textbox1.Location = new Point(25, 25);
this.Controls.Add(textbox1);



然后将此代码添加到您要移动文本框的位置



Then add this code to where you want to move the text box

foreach (Control item in this.Controls)
{
    if (item is TextBox && item.Name == "Txt1")
        item.Location = new Point(90, 90);//this is the x,y coordination to new location
}


这篇关于移动动态添加的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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