将上述代码中提取结果的部分添加到一个按钮的点击事件上
时间: 2024-04-15 18:30:30 浏览: 18
下面是将代码中提取结果的部分添加到一个按钮的点击事件上的示例代码:
```csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
public class UserInfo
{
public string Name { get; set; }
public string Age { get; set; }
public string Gender { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
}
public class Program : Form
{
private Button extractButton;
public Program()
{
extractButton = new Button();
extractButton.Text = "提取用户信息";
extractButton.Click += ExtractButton_Click;
Controls.Add(extractButton);
}
private void ExtractButton_Click(object sender, EventArgs e)
{
string filePath = "path/to/your/txt/file.txt";
UserInfo userInfo = ExtractUserInfo(filePath);
MessageBox.Show($"姓名: {userInfo.Name}\n" +
$"年龄: {userInfo.Age}\n" +
$"性别: {userInfo.Gender}\n" +
$"电话: {userInfo.Phone}\n" +
$"邮箱: {userInfo.Email}");
}
private UserInfo ExtractUserInfo(string filePath)
{
string content = File.ReadAllText(filePath);
string name = GetMatchValue(content, @"姓名:([\w\s]+)");
string age = GetMatchValue(content, @"年龄:(\d+)");
string gender = GetMatchValue(content, @"性别:([\w\s]+)");
string phone = GetMatchValue(content, @"电话:(\d+)");
string email = GetMatchValue(content, @"邮箱:([\w\.]+@[\w\.]+)");
UserInfo userInfo = new UserInfo
{
Name = name,
Age = age,
Gender = gender,
Phone = phone,
Email = email
};
return userInfo;
}
private string GetMatchValue(string content, string pattern)
{
Match match = Regex.Match(content, pattern);
return match.Success ? match.Groups[1].Value : null;
}
public static void Main(string[] args)
{
Application.Run(new Program());
}
}
```
在这个示例代码中,我们创建了一个继承自 `Form` 的 `Program` 类,并在窗体中添加了一个按钮 `extractButton`。当按钮被点击时,会触发 `ExtractButton_Click` 方法,在该方法中调用 `ExtractUserInfo` 函数提取用户信息,并使用 `MessageBox` 显示提取的结果。
请确保将 `filePath` 替换为实际的文件路径。运行程序后,点击按钮即可触发提取操作,并弹出消息框显示用户信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)