C# 从文本框输入更改标签
这就是导致错误的原因C# 从文本框输入更改标签,c#,winforms,C#,Winforms,这就是导致错误的原因 namespace HospitalMonitor { public partial class PatientRegister : Form { public PatientRegister() { InitializeComponent(); } private void btnRegister_Click(object sender, EventArgs e)
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”中的usercontrol。然而,我得到了一个错误:
CS0120-非静态字段需要对象引用,
方法或属性“CentralStation.centralModule1”
我自己也不明白,我认为这是一个可能的解决办法。我环顾四周,还是不明白
我希望你明白我想做什么
这就是我隐藏表格的方式
CentralStation.centralModule1.lblPatientName = txtPatientName.Text;
试试像
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();
}
}
}
更新:
作为对“如何获取参考”的响应,您可以将中心位置传递到PatientRegister
例如,在您的CentralStation课程中
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();
}
}
在你的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();
}
您需要CentralStation表单的实例来更改UserControl内的标签。这是面向对象编程的一个基本方面。CentralStation是一个类的名称,而不是从该类创建的对象(您可以使用new关键字创建的许多实例之一)。您应该创建CentralStation类的实例。好的,我现在试试,我正试图回到C#显然我忘记了一个重要的基本方面。我该如何引用我隐藏的方面?我已经检查了你输入的密码。我只是不想打开一个全新的表格。如果你可以隐藏表格,你已经有了它的参考资料。你一开始是怎么藏起来的?
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;
}
}
}