惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Troy Hunt's Blog
Scott Helme
Scott Helme
T
Threat Research - Cisco Blogs
T
Tenable Blog
L
LINUX DO - 热门话题
V
Visual Studio Blog
I
Intezer
Blog — PlanetScale
Blog — PlanetScale
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
C
Cyber Attacks, Cyber Crime and Cyber Security
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
Know Your Adversary
Know Your Adversary
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
N
Netflix TechBlog - Medium
SecWiki News
SecWiki News
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Project Zero
Project Zero
W
WeLiveSecurity
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
Recorded Future
Recorded Future
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Vercel News
Vercel News
S
Securelist
Spread Privacy
Spread Privacy
L
LangChain Blog
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
MongoDB | Blog
MongoDB | Blog
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The Last Watchdog
The Last Watchdog
Attack and Defense Labs
Attack and Defense Labs
博客园 - 司徒正美
Help Net Security
Help Net Security
L
Lohrmann on Cybersecurity
人人都是产品经理
人人都是产品经理
Forbes - Security
Forbes - Security
Hacker News - Newest:
Hacker News - Newest: "LLM"
PCI Perspectives
PCI Perspectives
博客园 - 【当耐特】
T
Tor Project blog

博客园 - soulsjie

一种在winfrom窗体中显示计算公式的解决方案 winform窗体DataGridView合并单元格处理 C#GDI+阴影笔刷样式HatchStyle探讨 C#代码混淆工具ConfuserEx的使用 Aspose.Words在指定位置插入图片、调整图片大小 C#获取对象实体的键值对信息 C#使用Aspose.Words将Spread表格插入到Word中 C# Aspose.Words将word中自定义的标签进行替换 Python文件操作基础方法 C# 将项目资源文件保存到磁盘上 SqlSugar数据库辅助类 使用存储过程备份MS SQLServer数据库 案例3:JAVA GUI 随机点名程序 案例2:JAVA GUI 简易计算器 ArcGIS JS API 添加要素图层 点击时获取图层属性 ArcGIS JS API 将天地图设置为底图 HTML5PLUS实现类似右侧弹出菜单 SqlSugar各数据库连接串 案例1:JAVAGUI用户管理
C#将Winform上的TextBox和ComBox的值导入和导出
soulsjie · 2023-02-23 · via 博客园 - soulsjie

一、将所有控件上的值导出到文件

1.使用递归遍历窗体上所有的TextBox和ComBox,将控件名称和控件的值存入List

实体类Result.cs

/// <summary>
/// 结果数据
/// </summary>
public class Result
{
/// <summary>
/// 文本框名称
/// </summary>
public string TextBoxName { get; set; }

/// <summary>
/// 文本框的值
/// </summary>
public string TextBoxValue { get; set; }
}

递归获取想要的控件

        private void GetAllTextAndCombox(Control controls)
        {
            foreach (Control control in controls.Controls)
            {
                if (control is TextBox || control is ComboBox)
                {
                    AllControls.Add(control);
                }
                if (control.Controls.Count > 0)
                {
                    GetAllTextAndCombox(control);
                }
            }
        }

获取控件的名称和值

            List<Result> Results=new List<Result>();
            for (int i = 0; i < AllControls.Count; i++)
            {
                Result result = new Result();
                result.TextBoxName = AllControls[i].Name;
                result.TextBoxValue = AllControls[i].Text;
                Results.Add(result);
            }

2.将实体序列化成字符串,保存字符串

if (Results.Count > 0)
            {
                string Resultstr = JsonConvert.SerializeObject(Results);
                if (string.IsNullOrEmpty(Resultstr)) return;
                SaveFileDialog saveFile = new SaveFileDialog();
                saveFile.Title = "请选择保存文件路径";
                saveFile.Filter = "数据文件(*.Single)|*.Single";
                saveFile.OverwritePrompt = true;  //是否覆盖当前文件
                saveFile.RestoreDirectory = true;  //还原目录
                if (saveFile.ShowDialog() == DialogResult.OK)
                {
                    if (!File.Exists(saveFile.FileName))
                    {
                        using (File.Create(saveFile.FileName)) { }
                    }
                    using (StreamWriter sw = new StreamWriter(saveFile.FileName, false))
                    {
                        sw.Write(Resultstr);
                        sw.Close();//写入
                        MessageBox.Show("已保存!");
                    }
                }
            }

二、从文件导入所有控件的值

读取文件内容,反序列化成实体,循环给控件赋值。

            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Title = "请选择保存文件路径";
            openFile.Filter = "数据文件(*.Single)|*.Single";
            openFile.Multiselect = false;
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                using (StreamReader sr = new StreamReader(openFile.FileName))
                {
                    string Resultstr = sr.ReadToEnd();
                    if (string.IsNullOrEmpty(Resultstr)) return;
                    Results = JsonConvert.DeserializeObject<List<Result>>(Resultstr);
                    if (Results.Count <= 0) return;
                    for (int i = 0; i < Results.Count; i++)
                    {
                        Control ct = AllControls.Where(o => o.Name == Results[i].TextBoxName).FirstOrDefault();
                        if (ct != null)
                        {
                            ct.Text = Results[i].TextBoxValue;
                        }
                    }
                    sr.Close();
                }
            }