WinForm传递对象编辑后,主窗体如何同步更新属性值?
Pokemon WinForm 编辑属性后Form1不更新的解决方法
问题描述
我开发了一个简单的Pokemon WinForm应用,包含两个窗体:Form1用于展示Pokemon及其关联属性;点击编辑按钮后,会弹出EditCharacter窗体(Form2),允许用户编辑角色属性。我从Form1传入currentCharacter对象让EditCharacter加载选中的Pokemon,但修改属性后返回Form1,数值并未更新(比如把Rayquaza的HealthPoints改成0,Form1里还是原来的数值)。
现有代码
Form1代码
public partial class Form1 : Form { CardCollection cardCollection; public Character currentCharacter; public Form1() { InitializeComponent(); cardCollection = new CardCollection(); } private void Form1_Load(object sender, EventArgs e) { foreach (var keyValuePair in cardCollection.Cards) { cmbPokemon.Items.Add(keyValuePair.Key); } } private void cmbPokemon_SelectedIndexChanged(object sender, EventArgs e) { currentCharacter = cardCollection.Cards[cmbPokemon.Text]; updateControls(); } public void updateControls() { lblHP.Text = currentCharacter.HealthPoints.ToString(); lblStrength.Text = currentCharacter.Strength.ToString(); lblSpecialPower.Text = currentCharacter.SpecialPower; pbPokemon.ImageLocation = @currentCharacter.FileName; } private void btnEdit_Click(object sender, EventArgs e) { EditCharacter EditChar = new EditCharacter(currentCharacter); EditChar.ShowDialog(); } }
EditCharacter窗体代码
public partial class EditCharacter : Form { Character currentCharacter; public EditCharacter( Character c) { InitializeComponent(); currentCharacter = new Character(); currentCharacter = c; lblCharacter.Text = c.Name; pbCharacter.ImageLocation = c.FileName; txtPower.Text = c.SpecialPower; tbHP.Value = c.HealthPoints; tbStrength.Value = c.Strength; lblHP.Text = c.HealthPoints.ToString(); lblStrength.Text = c.Strength.ToString(); } private void trackBar1_Scroll(object sender, EventArgs e) { lblHP.Text = tbHP.Value.ToString(); } private void tbPower_Scroll(object sender, EventArgs e) { lblStrength.Text = tbStrength.Value.ToString(); } private void btnSave_Click(object sender, EventArgs e) { currentCharacter.Name = lblCharacter.Text; currentCharacter.FileName = pbCharacter.ImageLocation; currentCharacter.SpecialPower = txtPower.Text; currentCharacter.HealthPoints = tbHP.Value; currentCharacter.Strength = tbStrength.Value; currentCharacter.SpecialPower = txtPower.Text; this.Close(); } }
解决方案
核心原因
Character是引用类型,你在EditCharacter中修改的属性其实已经同步到Form1的currentCharacter对象里了,但Form1的控件没有重新读取这些更新后的属性值,所以看不到变化。另外EditCharacter构造函数里的currentCharacter = new Character();是多余代码,不影响功能但可以删掉。
修改步骤
- 更新Form1的编辑按钮事件:在关闭EditCharacter对话框后,立即调用
updateControls()刷新控件显示
private void btnEdit_Click(object sender, EventArgs e) { EditCharacter EditChar = new EditCharacter(currentCharacter); EditChar.ShowDialog(); // 对话框关闭后刷新Form1的控件 updateControls(); }
- 优化EditCharacter构造函数:删除多余的对象初始化代码,简化为:
public EditCharacter(Character c) { InitializeComponent(); currentCharacter = c; lblCharacter.Text = c.Name; pbCharacter.ImageLocation = c.FileName; txtPower.Text = c.SpecialPower; tbHP.Value = c.HealthPoints; tbStrength.Value = c.Strength; lblHP.Text = c.HealthPoints.ToString(); lblStrength.Text = c.Strength.ToString(); }
这样修改后,编辑完属性关闭EditCharacter窗体,Form1的控件就会自动刷新显示最新的属性值了。
内容的提问来源于stack exchange,提问作者Tiny
相关产品推荐
相关产品推荐

