如何从文本框中获取输入按代码创建 [英] How Can I Get Input From Textbox Create By Code

查看:73
本文介绍了如何从文本框中获取输入按代码创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单和文本框的代码和一个按钮现在我想从该文本框获取输入到我的基本形式的变量

i have code for a form and textbox and one button now i want to take inputs from that text box to a variable of my base form

推荐答案

当你通过VS设计器添加一个文本框,它创建一个同名的变量来指向文本框 - 同样的东西适用于一个按钮。

如果你想手动创建文本框和按钮通过你的代码,你需要在你的代码中做一些相同的事情来跟踪控件。

这很容易做到,如果只有一个文本框和一个按钮你正在创建:

When you add a textbox via the VS designer, it creates a variable of the same name to "point" to the textbox - the same thing applies to a button.
If you want to create the textbox and button manually via your code, then you need to do something equivalent in your code to "track" the controls.
It's easy to do, if it's only one textbox and one button you are creating:
private TextBox myTextBox = new TextBox();
private myButton = new Button();
...
   Controls.Add(myTextBox);
   Controls.Add(myButton);

如果你把它们中的一些作为成对添加并希望按钮引用它匹配的Tetxbox它会变得更复杂,但即使这样,框架也会让它变得简单:

It gets more complex if you are adding a number of them as pairs and want the button to reference it's matching Tetxbox, but even then the framework makes it easy:

   TextBox tb = new TextBox();
   b = new Button();
...
   b.Tag = tb;
   Controls.Add(tb);
   Controls.Add(b);
...
private void MyButton_Click(object sender, EventArgs e)
   {
   Button b = sender as Button;
   if (b != null)
      {
      TextBox tb = b.Tag as TextBox;
      if (tb != null)
         {
         myStringVariable = tb.Text;
         ...
         }
      }
   }


这篇关于如何从文本框中获取输入按代码创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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