用户输入自textbox1 >存储在列表中>输出形式为textbox2
本文关键字:输出 textbox2 输入 textbox1 存储 用户 列表 | 更新日期: 2023-09-27 18:01:33
我有一个关于c#列表的问题。说到编程,我完全是个新手,我真的很抱歉我是个笨蛋。我正在做一些练习编码,我正在创建一个简单的程序,允许用户通过textbox1输入名字,然后一旦他们按下按钮1,名字将被存储在一个列表中,并将在textbox2上输出。
我有困难的时间从textbox1存储数据。我在网上查了一下,但是没有找到我关心的合适的文章,所以我在这里试试运气。
对不起,伙计们,我忘了说我正在使用Winforms。非常感谢您的快速回复。
假设winforms…
- 拖放2个列表和一个按钮到你的设计器。
- 将按钮拖到设计器
- 双击按钮自动创建事件
- 在表单的某处创建一个列表结构来存储列表
- 在表单构造器中实例化你的列表
- 在
button1_Click
事件中将textbox1
的文本添加到列表 - 生成1textbox2 '的文本
这里有一个例子
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窗体应用程序;我不确定您是否打算将数据存储在另一个文本框中。但列表框可能更符合您的目标。
- 从工具箱中拖动下列内容:文本框、第二文本框、列表框和按钮到窗体中。
- 你可以随意调整它们,把它们当作画布来画。
- 一旦它出现配置你想要双击按钮。
这些模块非常重要,随着你的学习,你会注意到c#的结构。
private void button1_click(object sender, EventArgs e)
{
// Logic to add will go in here.
}
这是什么意思?
- Private:是修饰符,表示它被限制在这个类中。
- Void:表示不要求返回类型
- button1_click:这是按钮的名称,你可以在它的属性中改变它。在前面命名组件是一种很好的做法,这样你就知道你在用什么了。
整个block是一个Event。当它被点击时,它会执行一个动作。这就是它的意思;这就是你的目标实现的地方:
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");
}
}
这将提供基本知识,但正如您从其他示例中看到的那样,它们的做法不同。如果不小心的话,您会注意到这种逻辑中可能存在错误。您将学习如何根据您配置的结构来识别。
希望这是有帮助的,看起来很多其他人也为你做了一些很棒的帖子。
快乐编码。
您可以使用如下简单的代码:
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
这很简单。
List<string> mylist=new List<string>();
mylist.Add(textbox1.Text);
textbox2.Text=mylist[mylist.Count - 1]
首先创建一个字符串对象列表。然后将textbox1中的文本添加到列表的末尾。然后通过获取列表的长度并减去1来获得您从列表中添加的最后一个元素,因为c#集合是以0为基础的,并且第一个元素是[0]而不是[1]。
在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
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;
}
}
}