从文本框输入更改标签

本文关键字:标签 输入 文本 | 更新日期: 2023-09-27 18:32:25

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;

这是我拥有的代码,我正在尝试让它注册文本框输入,并在按下按钮时重命名不同形式的标签。此标签来自我自己制作并导入到"中央车站"窗体中的用户控件。但是我收到错误:

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();
        }
    }
}

从文本框输入更改标签

尝试类似...

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中。

例如,在您的中央车站类中,

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();
    }

在您的患者注册类中,

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;
        }
    }
}