从文本框输入更改标签

时间:2016-01-28 21:58:49

标签: c# winforms

namespace HospitalMonitor
{
    public partial class PatientRegister : Form
    {
        public PatientRegister()
        {
            InitializeComponent();
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtPatientName.Text))
            {
                MessageBox.Show("You did not enter anything, please enter the patients name.");
            }
            else
            {
                CentralStation.centralModule1.lblPatientName = txtPatientName.Text;
            }
        }
    }
}

这是造成错误的原因。

CentralStation.centralModule1.lblPatientName = txtPatientName.Text;

这是我的代码,我试图让它注册文本框输入,当按下按钮时,它重命名一个不同形式的标签。这个标签来自我自己创建的用户控件,并导入到表格“CentralStation”中。然而,我收到了错误:

  

CS0120 - 非静态字段需要对象引用,   方法或属性' CentralStation.centralModule1'。

我自己没有理解我认为可能的解决方案。我环顾四周仍然没有得到它。

我希望你明白我要做的事情!

这就是我隐藏表格的方式。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HospitalMonitor
{
    public partial class CentralStation : Form
    {
        public CentralStation()
        {
            InitializeComponent();
            PopulateCentralStationBedsideDetails();
        }

        public void PopulateCentralStationBedsideDetails()
        {
            centralModule1.lblBedNumber.Text = "Bed 1";
            centralModule2.lblBedNumber.Text = "Bed 2";
            centralModule3.lblBedNumber.Text = "Bed 3";
            centralModule4.lblBedNumber.Text = "Bed 4";
            centralModule5.lblBedNumber.Text = "Bed 5";
            centralModule6.lblBedNumber.Text = "Bed 6";
            centralModule7.lblBedNumber.Text = "Bed 7";
            centralModule8.lblBedNumber.Text = "Bed 8";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Hide();
            BedsideMonitor bedsideMonitor = new BedsideMonitor();
            bedsideMonitor.ShowDialog();
            this.Visible = false;
            Show();
        }

        private void btnRegBed1_Click(object sender, EventArgs e)
        {
            Hide();
            PatientRegister patientRegister = new PatientRegister();
            patientRegister.ShowDialog();
            this.Visible = false;
            Show();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试类似......

private void btnRegister_Click(object sender, EventArgs e)
{
    if (string.IsNullOrWhiteSpace(txtPatientName.Text))
    {
        MessageBox.Show("You did not enter anything, please enter the patients name.");
    }
    else
    {
        //do something to get reference to your form...
        //for example, you can create a new form, or get the form by its id
        CentralStation centralStation = new CentralStation();
        centralStation.centralModule1.lblPatientName = txtPatientName.Text;
        //and then do something else...
        centralStation.Show();
    }
}

更新:

响应"如何获得参考",您可以将CentralStation传递给PatientRegister。

例如,在您的CentralStation类中,

private void btnRegBed1_Click(object sender, EventArgs e)
    {
        Hide();
        //here you pass your CentralStation into the patientRegister you are initiating by using "this"
        PatientRegister patientRegister = new PatientRegister(this);
        patientRegister.ShowDialog();
        this.Visible = false;
        Show();
    }

在您的PatientRegister课程中,

public partial class PatientRegister : Form
{

    private CentralStation centralStation;

    public PatientRegister(CentralStation cs)
    {
        InitializeComponent();
        //here you get the reference of the centralStation 
        this.centralStation = cs;
    }


    private void btnRegister_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrWhiteSpace(txtPatientName.Text))
        {
            MessageBox.Show("You did not enter anything, please enter the patients name.");
        }
        else
        {
            // this.centralStation is the one you have hidden
            this.centralStation.centralModule1.lblPatientName = txtPatientName.Text;
        }
    }
}