【问题标题】:Button to return changes made to a property?按钮返回对属性所做的更改?
【发布时间】:2013-04-04 22:50:41
【问题描述】:

我正在尝试在运行时评估属性的值。首先,我尝试使用一个人的 name 属性来完成此操作,但我不确定如何完成下面的 if 语句。我希望它重新打开我的 Form2,它将学生作为参数,它将显示他们的所有详细信息。任何帮助或建议将不胜感激。如果我做错了,请告诉我一些关于如何在运行时编辑属性数据的指导或建议。

public class Student :IPerson
{
    //Method that changes the original name
    public string ChangeName(string newForename)
    {
        _forename = newForename;
        return _forename;
    }
}

public partial class Edit_Details : Form
{      
    public Edit_Details(Student student)
    {
        InitializeComponent();
        nameLabel.Text = student.Forename;
        student.ChangeName(editNameTextBox.Text);
        //if(savebutton.Click) //how can I get this to replace the old name with the new one when the button is pressed??
    }
}

【问题讨论】:

  • 您需要将一个事件连接到 savebutton.Click,然后每当单击按钮时,该事件就会触发。在这种情况下,您可能希望在事件中为 ChangeName 做一些事情。

标签: c# winforms button methods properties


【解决方案1】:

 public Edit_Details(Student student)
    {
        InitializeComponent();
        nameLabel.Text = student.Forename;
        savebutton.Click+=(sender,e)=>
          {
             savebutton.Text=student.ChangeName(editNameTextBox.Text);
          };
    }

【讨论】:

  • 非常感谢。除了我使用 new Student(student.ChangeName(editNameTextBox.Text) 而不是 savebutton.Text=student.ChangeName(editNameTextBox.Text);
猜你喜欢
  • 1970-01-01
  • 2014-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-04
  • 2014-12-10
  • 1970-01-01
  • 2023-03-13
相关资源
最近更新 更多