写一个C#语言的winforms取出textbox里面的数据,并放到数组中的完整程序
时间: 2024-09-12 08:03:45 浏览: 62
在C# WinForms中,你可以创建一个简单的Windows应用程序来获取TextBox控件的内容并将其放入数组中。下面是一个完整的示例程序:
```csharp
using System;
using System.Windows.Forms;
public class Form1 : Form
{
private TextBox textBox1; // TextBox控件
private string[] arrayData; // 存储数据的数组
public Form1()
{
InitializeComponent();
// 初始化数组,假设初始长度为5
arrayData = new string[5];
// 设置TextBox控件
textBox1 = new TextBox();
textBox1.Location = new System.Drawing.Point(10, 10);
textBox1.Size = new System.Drawing.Size(200, 20);
this.Controls.Add(textBox1);
}
private void button1_Click(object sender, EventArgs e) // 当按钮被点击时执行此操作
{
// 获取TextBox中的文本
string text = textBox1.Text;
// 将文本添加到数组中
if (!string.IsNullOrEmpty(text)) // 验证文本是否为空
{
arrayData[arrayData.Length] = text; // 如果非空,添加到数组末尾
}
else
{
MessageBox.Show("TextBox内容不能为空");
}
}
}
class Program
{
static void Main()
{
Application.Run(new Form1());
}
}
```
在这个程序中,我们创建了一个名为`Form1`的窗体,其中有一个TextBox和一个按钮。当用户点击按钮时,会从TextBox获取文本并尝试将其存储到数组`arrayData`中。如果TextBox为空,则会显示一个警告消息。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)