简体   繁体   English

如何在运行时创建文本框并从这些文本框中获取值? 在C#中。 净

[英]How to create textbox at run time and take values from these text box ? in C# . Net

I need your help in making text box at run time and taking values from these text boxes that user enter. 在运行时创建文本框并从用户输入的这些文本框中获取值时,我需要您的帮助。 i have two button and one rich_text_box , when user click on one button it creates 3 text boxes and then user click on other button it should take value from text boxes and how in rich text box . 我有两个按钮和一个rich_text_box,当用户单击一个按钮时,它将创建3个文本框,然后用户单击其他按钮,它应从文本框中获取值以及如何在富文本框中显示值。

this is code i am using to create dynamic textbox 这是我用来创建动态文本框的代码

private void create_textbox_Click(object sender, EventArgs e)
    {
       flowLayoutPanel1.Controls.Clear();
        for(i=1;i<=3;i++)
        {
            TextBox text = new TextBox();
            text.Name = "Text Box" + i.ToString();
            //text.Text = "Text Box " + i.ToString();
            flowLayoutPanel1.Controls.Add(text);


        }
    }

and this code i am using to take values from new created text boxes and displaying in rich text box . 我正在使用此代码从新创建的文本框中获取值并在富文本框中显示。

private void get_value_Click(object sender, EventArgs e)
    {
     TextBox text = new TextBox();
        for (i = 1; i <= 3; i++)
        {
            string value = text.Text + i.ToString();
            richTextBox1.SelectedText  = "\r\n" + value;
        }

    }

This should solve your problem: 这应该可以解决您的问题:

private void get_value_Click(object sender, EventArgs e)
{
  for (var c in flowLayoutPanel1.Controls)
  {
    var t = c as TextBox;
    if (t != null)
    {
      richTextBox1.SelectedText  = "\r\n" + t.Text;
    }
  } 
}

In your method get_value_Click you aren't using any of the text boxes that were added to the flow layout panel. 在您的方法get_value_Click中,您没有使用添加到流布局面板中的任何文本框。 Something similar to Wolfgang Ziegler's answer should work but you will need to check the Type and Name in case you have other controls in the flow layout panel. 与Wolfgang Ziegler的答案类似的方法应该起作用,但是如果您在流布局面板中还有其他控件,则需要检查“类型”和“名称”。

private void get_value_Click(object sender, EventArgs e)
    {
        for (i = 1; i <= 3; i++)
        {
            string value = this.flowLayoutPanel1.Controls["Text Box" + i].Text;
            richTextBox1.SelectedText  = "\r\n" + value;
        }
    }

This ought to do it. 这应该做到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 在文本框中一次取一段文本 - C# Take a piece of text at a time in a textbox 如何取两个文本框值的平均值C# - how to take average of two text box values C# 如何使用派生类中的值填充文本框? C# - How to populate a text box with values from a derived class? C# 如何将C#中的文本框值插入Oracle数据库 - How to insert text box values from C# into Oracle Database 如何从文本框中获取文本并将其转换为C#中可用的文件路径? - How do I take the text from a textbox and make it into a usable file path in C#? 在运行时创建多个文本框并从数据库分配值文本框。我想显示数据库中的所有数据 - create at run time multiple text box and assign value textbox from database..i want to show all the data from database 如何显示从文本文件到textBox1或textBox2的大量用户名或密码c#/。net - how to show multy username or password from text file to textBox1 or textBox2 c#/.net 如何使用C#for Android在Xamarin中获取密码文本框 - How to take Password Text box in Xamarin using C# for Android 如何使用正则表达式等在C#中处理文本框/富文本框中的文本 - How to process text from textbox/richtext box in C# using regular expression and more 如何从文本框中获取用户输入并使用列表C#显示? - How to take user input from a text box and display using a list C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM