[英]User Input from textbox1 > store in a list > output in textbox2
我刚才有一个关于C#列表的问题。 在编程方面,我是一个非常棒的人,我真的很抱歉做一个小鸟。 我正在做一些练习编码,我正在创建一个简单的程序,允许用户通过textbox1输入名称,然后一旦按下button1,名称将存储在List中,并将在textbox2上输出。
我很难从textbox1存储数据。 在网上查了一下,但是我找不到合适的文章,所以我在这里试试运气。
对不起,伙计们,我忘了提到我正在使用Winforms。
非常感谢您的快速回复。
假设winforms ...
button1_Click
事件中,将textbox1
的文本添加到列表中 这是一个例子
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
list = new List<string>();
}
List<string> list;
private void button1_Click(object sender, EventArgs e)
{
list.Add(textBox1.Text);
string txt = "";
foreach(string s in list)
{
txt += s + " ";
}
textBox2.Text = txt;
}
}
}
那样的东西?
string name = Textbox1.Text;
ListBox1.Add(name);
如果您使用传统的Windows表单应用程序; 我不确定您是否打算将数据存储在另一个文本框中。 但是列表框可能更符合您的目标。
此时Visual Studio将离开Designer View并进入Code View。 所以你将能够看到代码。 它会自动将您置于Button代码块中。
这些块非常重要,随着您的进步,您会注意到C#的结构。
private void button1_click(object sender, EventArgs e)
{
// Logic to add will go in here.
}
这是什么意思?
整个块是什么,是一个事件。 因此,当它被点击时,它将执行一个动作。 这就是它的意思; 所以这就是你实现目标的地方:
private void btnAddToList_Click(object sender, EventArgs e)
{
// Test to ensure it isn't null.
if(txtAddText.Text != String.EmptyOrNull)
{
// Declare a variable with the initial textbox value.
string txtVar = txtAddText.Text;
// Has the second textbox inherit value from other Textbox.
txtNewText = txtVar
// Now Add it to a Listbox.
lstContainer.Items.Add(txtAddText.Text + DateTime.Now());
}
else
{
// Null or Empty Character, Error Message.
MessageBox.Show("Invalid Entry, please enter a valid entry");
}
}
这将提供基础知识,但正如您从其他示例中可以看到的那样,他们以不同的方式做到了。 您会注意到,如果您不小心,错误可能存在于此类逻辑中。 您将学习根据您配置的结构进行识别。
希望这是有帮助的,看起来很多其他人也为你做了一些了不起的帖子。
快乐的编码。
在button1中单击侦听器(如果没有此钩子进入GUI构建器视图并双击该按钮,它将自动为您创建并注册侦听器),添加以下代码;
textbox2.Text = textbox1.Text; // set tb2 = tb1
textbox1.Text = System.String.Empty; // clear tb1
现在,在您的帖子中,您说要将数据存储在列表中,但是您没有指定用户输入该数据的方式,因此很难给出具体的答案。 如果名称用逗号分隔,以获得一个包含所有名称的数组,您可以这样做;
string[] names = textbox1.Text.Split(',');
但是,从您的帖子来看,您似乎根本不想将数据存储在列表中。 如果您只想在单击输入按钮时在textbox1
中显示textbox2
的输入,请使用第一个代码段。 如果你走第二条路线,你必须将数组转换回一个字符串。 这可以通过for循环轻松完成。
string result = System.String.Empty;
for (int i = 0; i < names.Length; i++)
result = result + names[i] + " ";
使textbox2
显示textbox1
并显示名称的数量;
textbox2.Text = textbox1.Text; // set tb2 = tb1
string[] names = textbox1.Text.Split(','); // i use a comma but use whatever
// separates the names might just want ' ' for whitespace
textbox1.Text = System.String.Empty; // clear tb1
MessageBox.Show("You entered " + names.Count.ToString() + " names."); // show the names count
这很简单。
List<string> mylist=new List<string>();
mylist.Add(textbox1.Text);
textbox2.Text=mylist[mylist.Count - 1]
首先,创建一个字符串对象列表。 然后将文本从textbox1添加到列表的末尾。 然后通过获取列表的长度并减去1来获取从列表中添加的最后一个元素,因为在C#集合中基于0并且第一个元素是[0]而不是[1]。
你可以使用简单的东西:
private void button1_Click_1(object sender, EventArgs e)
{
string[] names = textBox1.Text.Split(new string[] { " ", Environment.NewLine, "," }, StringSplitOptions.RemoveEmptyEntries);
//you can add more parameters for splitting the string
textBox2.Text = string.Join(",", names);
//you can replace the comma with something more suitable for you
}
第一行将您在textBox1中输入的字符串(因此名称由换行符,空白字符或逗号分隔)拆分为字符串数组(而不是您请求的列表),第二行将字符串连接成一大串名称分隔用逗号将其放入textBox2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ShowAllSaveNameAndCountApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> ourList=new List<string>();
string txt =" ";
private void buttonSave_Click(object sender, EventArgs e)
{
ourList.Add(textBoxEntryName.Text);
foreach (string s in ourList)
{
txt+= s+ "\n ";
}
textBoxEntryName.Clear();
ourList.Clear();
}
private void buttonShowAllName_Click(object sender, EventArgs e)
{
textBoxShowName.Text = txt;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.