C#从另一个表单

时间:2017-08-08 08:22:14

标签: c#

我正在使用C#(WPF)进行项目,我将人员添加到列表中以显示在第一个表单(MainWindow)上;在MainWindow上单击一个按钮以打开一个新表单(Window1),他们可以在其中添加有关要添加到列表中的人员的详细信息(名称,年龄等),我在传递这些数据时遇到问题。这就是我所拥有的:

public partial class MainWindow : Window
{
    public List<Patients> newPatientList = new List<Patients>();
    public MainWindow()
    {
        InitializeComponent();
    }
    private void button_Click(object sender, RoutedEventArgs e)
    {
        Window1 addPx = new Window1();
        addPx.Show(); 
    }

然后在Window1中,单击按钮时:

MainWindow newPxList = new MainWindow(); 

newPxList.newPatientList.Add(new Patients(lstname.Text, frstname.Text, age.Text, 
                                          rm.Text, "", status.SelectedItem.ToString()));

然后窗口关闭,但列表似乎没有在原始表单上更新。

4 个答案:

答案 0 :(得分:2)

OnClick中的Window1 mathod中,您创建新实例MainWindow并将新患者添加到此实例中。在此方法之后,此MainWindow实例将被销毁。您应该将对列表的引用作为Window1的参数传递。

EG。

private void button_Click(object sender, RoutedEventArgs e)
{
    Window1 addPx = new Window1(newPatientList);
    addPx.Show(); 
}

并在Window1内嵌

newPxList.newPatientList.Add(new Patients(lstname.Text, frstname.Text, age.Text, 
                                          rm.Text, "", status.SelectedItem.ToString()));

这样的事情:

patientListFromMainWindow.Add(new Patients(lstname.Text, frstname.Text, age.Text, 
                                              rm.Text, "", status.SelectedItem.ToString()));

答案 1 :(得分:2)

您需要将列表的引用传递给新窗口

public partial class MainWindow : Window
{
    public List<Patients> newPatientList = new List<Patients>();

    public MainWindow()
    {
        InitializeComponent();
    }
    private void button_Click(object sender, RoutedEventArgs e)
    {
        Window1 addPx = new Window1(newPatientList);
        addPx.Show(); 
    }
}

public partial class Window1 : Window
{
    List<Patients> _patients;
    public Window1(List<Patients> patients)
    {
        InitializeComponent();
        _patients = patients;
    }
    private void button_Click(object sender, RoutedEventArgs e)
    {
    _patients.Add(new Patients(lstname.Text, frstname.Text, age.Text, 
                                        rm.Text, "", status.SelectedItem.ToString()));
    }
}

答案 2 :(得分:1)

在Window1中,你正在制作MainWindow的新实例,想象一下你正在创建MainWindow的新的,空白的和隐藏的副本。因此,您的原始MainWindow保持不变。

您需要做的是将MainWindow的引用(例如 link )传递给Window1

要做到这一点,你需要做以下事情:

Main类中创建属性Window1,并更改默认构造函数以获取MainWindow引用作为参数,如下所示:

public MainWindow Main { get; set; }

public Window1(MainWindow main)
{
    InitializeComponent();
    this.Main = main;
}

现在,您必须在显示Window1时更改构造函数,如下所示:

Window1 addPx = new Window1(this);
addPx.Show(); 

MainWindow中创建公共方法,如下所示:

public void AddNewPatient(string lastName, string FirstName, string age)
{
    //add new ListItem here
}

并且,Window1在需要时调用该方法,点击按钮,如下所示:

Main.AddNewPatient(lstname.Text, frstname.Text, age.Text /*etc */);

答案 3 :(得分:1)

您可以使用Window1方法,而不是将任何其他参数传递到ShowDialog()构造函数中,只需在对话框关闭时获取新创建的患者。

private void button_Click(object sender, RoutedEventArgs e)
{
    Window1 wnd1 = new Window1();
    bool? dialogResult = wnd1.ShowDialog();
    if ( dialogResult.HasValue && dialogResult.Value )
    {
        newPatientList.Add(wnd1.Patient);
    }
}

现在您可以调整Window1以保留结果:

class Window1 : Window
{
    Patient _patient;
    public Patient Patient
    {
        get { return _patient; }
    }

    // rest of your code

    void button_Click(object sender, RoutedEventArgs e)
    {
        _patient = new Patients(lstname.Text, frstname.Text, age.Text, rm.Text, "", status.SelectedItem.ToString());
        DialogResult = true;
        Close();
    }
}
相关问题