C# WinForm:如何将frmHome选中项字符串显示到frmViewCollection文本框
我使用Visual Studio 2022开发C# .NET Windows Forms项目,需处理frmHome和frmViewCollection两个窗体:点击frmHome的「View」按钮时弹出frmViewCollection,目标是将frmHome中lstCollections的高亮项文本显示到frmViewCollection的「Collection Name」文本框中。
目前frmHome的btnView_Click事件已将选中项赋值给frmViewCollection的collectionName属性,但frmViewCollection构造函数中设置txtCollectionName.Text = collectionName后,文本框未显示对应内容,代码无报错。
frmHome代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Common; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ProjectAlexKadyn { public partial class frmHome : Form { public string collection; public frmHome() { InitializeComponent(); } private void btnView_Click(object sender, EventArgs e) { collection = lstCollections.Text.ToString(); frmViewCollection frmViewCollection = new frmViewCollection(); frmViewCollection.collectionName = collection; frmViewCollection.Show(this); } private void btnExit_Click(object sender, EventArgs e) { this.Close(); } private void btnDelete_Click(object sender, EventArgs e) { } } }
frmViewCollection代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ProjectAlexKadyn { public partial class frmViewCollection : Form { public double totalValue; public string collectionName; public frmViewCollection() { InitializeComponent(); txtCollectionName.Text = collectionName; // 当前无法将变量值显示到txtCollectionName中 totalValue = 0.00; txtTotalValue.Text = totalValue.ToString("C", CultureInfo.CurrentCulture); /* * 接下来的逻辑会稍微复杂点... * * 这部分代码的前提: * * - 需要用四维数组存储当前会话中所有已保存的集合数据 * - 四维数组包含四个维度:集合名称、物品型号、物品简短描述、物品价值 * - 每次保存新集合时,将集合数据存入四维数组,并把collectionName添加到frmHome的lstCollections中 * - Alex,如果你不懂四维数组,先快速查一下,之后就能明白我要做什么了 * * 目标: * * 当在frmHome点击btnView时,如果集合不是默认的「Add New Collection」且是之前保存过的、总价值大于0的集合,要把该集合的实际总价值设置到txtTotalValue中。 * * 代码逻辑: * * 1. 将类的collectionName与四维数组中的集合名称匹配 * 2. 遍历该集合名称下的所有物品型号维度 * 3. 遍历每个物品型号时,获取对应物品价值维度的itemValue * 4. 累加所有itemValue,将总和赋值给类的totalValue * 5. 将totalValue转为货币格式字符串,显示到txtTotalValue中 * 6. 把代码放在这个注释块里,点击frmHome的btnView后就能生效 */ } private void btnAddNewItem_Click(object sender, EventArgs e) { } private void btnExit_Click(object sender, EventArgs e) { this.Close(); } private void btnSaveAsCurrent_Click(object sender, EventArgs e) { } private void btnSaveAsNew_Click(object sender, EventArgs e) { } } }
GUI界面说明
- frmHome界面:包含列表框
lstCollections,以及「View」「Delete」「Exit」按钮 - frmViewCollection界面:包含「Collection Name」文本框、「Total Value」文本框,以及「Add New Item」「Save As Current」「Save As New」「Exit」按钮
问题原因与修复方案
问题原因
构造函数执行时机早于collectionName赋值时机:创建frmViewCollection实例时,构造函数先运行,此时collectionName为默认的null,文本框被设为null;后续赋值collectionName时,不会自动触发文本框更新。
修复方案(二选一)
方案1:修改构造函数,直接传入参数
修改frmViewCollection的构造函数,接收集合名称作为参数,在构造时完成赋值:
// frmViewCollection构造函数修改为: public frmViewCollection(string collectionName) { InitializeComponent(); this.collectionName = collectionName; txtCollectionName.Text = this.collectionName; totalValue = 0.00; txtTotalValue.Text = totalValue.ToString("C", CultureInfo.CurrentCulture); }
同时修改frmHome的btnView_Click事件:
private void btnView_Click(object sender, EventArgs e) { collection = lstCollections.Text.ToString(); // 直接传入参数创建实例 frmViewCollection frmViewCollection = new frmViewCollection(collection); frmViewCollection.Show(this); }
方案2:将collectionName改为属性,赋值时同步更新文本框
把collectionName字段改为属性,在setter中同步更新文本框:
// frmViewCollection中替换原public string collectionName; private string _collectionName; public string collectionName { get => _collectionName; set { _collectionName = value; // 确保控件已初始化后再更新 if (txtCollectionName != null) { txtCollectionName.Text = value; } } }
此方案无需修改frmHome的代码,赋值frmViewCollection.collectionName = collection;时会自动更新文本框。
内容的提问来源于stack exchange,提问作者Gigoiy

