C# 是否仍要设置隐藏控件的值?
我有一个隐藏控件,其中包含一个textbox控件,我想设置textbox的text属性,但我得到了一个NullReferenceException。但是,如果我显示控件,设置值,然后隐藏它,那么我不会得到任何异常C# 是否仍要设置隐藏控件的值?,c#,winforms,textbox,C#,Winforms,Textbox,我有一个隐藏控件,其中包含一个textbox控件,我想设置textbox的text属性,但我得到了一个NullReferenceException。但是,如果我显示控件,设置值,然后隐藏它,那么我不会得到任何异常 miStatus1.Show(); miStatus1.ioItem1.popIoItem(caseState); miStatus1.Hide(); 然而,这样做感觉真的不干净,也不是很优雅。我看到一些闪烁,因为我必须对4个控件执行此操作,每个控件上最多有8个文本框 当控件隐藏时
miStatus1.Show();
miStatus1.ioItem1.popIoItem(caseState);
miStatus1.Hide();
然而,这样做感觉真的不干净,也不是很优雅。我看到一些闪烁,因为我必须对4个控件执行此操作,每个控件上最多有8个文本框
当控件隐藏时,有没有办法设置文本框的文本属性?或者在显示控件时填充文本框是更好的主意?这是否会降低我的应用程序的速度,因为它需要在每次显示控件时进行填充
popIoItem-代码
public void popIoItem(object obj){
if (ioType == 1)
{
tb.Text = (string)obj;
}
}
我的界面
public void popIoItem(object obj){
if (ioType == 1)
{
tb.Text = (string)obj;
}
}
我试图创建右侧的菜单,每次按下类别时,菜单都会向上/向下滑动,我会用文本框和其他io元素隐藏/显示适当的用户控件
更多代码
public void popIoItem(object obj){
if (ioType == 1)
{
tb.Text = (string)obj;
}
}
单击左侧的一个框后,将运行以下方法:
public void openMenu(int caseNum)
{
caseDB.casesDataTable chosenCase;
chosenCase = _casesAdapter.GetDataByID(caseNum);
string caseName = "";
int caseOwner = -1;
DateTime caseDate = DateTime.Today;
string caseDesc = "";
int caseState = -1;
foreach (caseDB.casesRow casesRow in chosenCase)
{
if (!casesRow.IscaseNameNull())
caseName = casesRow.caseName;
if (!casesRow.IscaseCreatedByNull())
caseOwner = casesRow.caseCreatedBy;
if (!casesRow.IscaseCreatedNull())
caseDate = casesRow.caseCreated;
if (!casesRow.IscaseDescNull())
caseDesc = casesRow.caseDesc;
if (!casesRow.IscaseStateNull())
caseState = casesRow.caseState;
}
int caseJobs = (int)_jobsAdapter.JobCount(caseNum);
string caseStateStr = Enum.GetName(typeof(caseState), caseState);
caseInfoMenu1.popMenu(caseName, caseOwner, caseDate, caseDesc,caseJobs,caseStateStr);
}
caseInfoMenu是右菜单。它由一些绘图和鼠标逻辑组成,用于绘制菜单和处理命中检测。除此之外,它还包含4个用户控件,每个垂直选项卡一个
public void popMenu(string caseName, int caseOwner ,DateTime caseDate, string caseDesc, int caseJobs, string caseState)
{
marked = 0;
miGeneral1.Show();
miEconomy1.Hide();
miStatus1.Hide();
miHistory1.Hide();
miGeneral1.ioItem1.popIoItem(caseName);
miGeneral1.ioItem2.popIoItem(caseOwner.ToString());
miGeneral1.ioItem3.popIoItem(caseDate.ToShortDateString());
miGeneral1.ioItem4.popIoItem(caseJobs.ToString());
miGeneral1.ioItem5.popIoItem(caseDesc.ToString());
//miStatus1.ioItem1.popIoItem(caseState);
//This is commented out because it makes the application crash. However if I show miStatus1, set the value and hide it, it does not crash.
this.Invalidate();
}
在每个用户控件中,我都有io项用户控件,它基本上绘制了一个蓝色框,并在文本框前面放置了一个控件
public partial class ioItem : UserControl
{
public int ioType { get; set; }
public int ioPadding { get; set; }
RichTextBox tb;
public ioItem()
{
InitializeComponent();
}
public void popIoItem(object obj){
if (ioType == 1)
{
tb.Text = (string)obj;
}
}
private void ioItem_Load(object sender, EventArgs e)
{
switch (ioType)
{
case 1:
tb = new RichTextBox();
tb.Location = new System.Drawing.Point(ioPadding, ioPadding);
tb.Name = "textbox";
tb.Size = new Size(this.Size.Width - (ioPadding * 2), this.Size.Height - (ioPadding * 2));
tb.BorderStyle = BorderStyle.None;
tb.Visible = true;
tb.BackColor = Color.FromArgb(255, 184, 198, 208);
tb.Font = new Font("Microsoft Sans Serif", 7);
this.Controls.Add(tb);
break;
case 2:
historyCtrl hiCtrl = new historyCtrl();
hiCtrl.Location = new Point(0,0);
hiCtrl.Size = new Size(this.Width, this.Height);
hiCtrl.Name = "history";
hiCtrl.Visible = true;
hiCtrl.BackColor = Color.FromArgb(255, 184, 198, 208);
this.Controls.Add(hiCtrl);
break;
default:
goto case 1;
}
}
}
尝试用调试器检查代码..可能还有其他问题吗?NullReference意味着您试图对不存在的对象执行某些操作。Show/Hide只是在正常情况下将控件的Visible属性设置为true/false(没有自定义重载/更改控件类)。所以我找出了问题所在。问题并不是我最初预期的那样。它不起作用的原因是我在Load_事件中创建了textbox控件。当我试图设置textbox控件的text属性值时,它们还没有被创建。我之所以以这种方式创建包含文本框的用户控件,是为了便于将它们拖动到设计器的屏幕中。我找到了这个讨论,它向我展示了另一种方法,现在它开始工作了。只是出于兴趣:你想用这个来解决什么问题?只是问,因为可能有更好的方法。popIoItem(caseState)中到底发生了什么?@ChrisWue-我有一个带有垂直选项卡的自定义菜单。每个选项卡都包含一系列控件,其中包含有关选定项目的信息。我会在这个问题上加一个屏幕。这当然是不可能的。如果可以
Hide
和Show
控件,则该控件必须存在,并且不能获得NullReferenceException
。您需要显示更多代码才能诊断实际问题。@Cody Gray-我将发布更多代码和信息。+1用于在您自己解决问题后发布答案。