【问题标题】:Is there anyway to set a value of a hidden control?无论如何设置隐藏控件的值?
【发布时间】:2011-08-06 04:38:15
【问题描述】:

我有一个包含文本框控件的隐藏控件,我想设置文本框的文本属性,但我得到了 NullReferenceException。但是,如果我显示控件,设置值然后隐藏它,那么我不会有任何异常。

miStatus1.Show();
miStatus1.ioItem1.popIoItem(caseState); 
miStatus1.Hide();

但是,这感觉是一种非常不干净且不太优雅的方式。而且我看到一些闪烁,因为我必须对 4 个控件执行此操作,每个控件最多有 8 个文本框。

有没有办法在控件隐藏时设置文本框的文本属性?或者在显示控件时填充我的文本框可能是一个更好的主意?这是否会减慢我的应用程序,因为它需要在每次显示控件时填充?

popIoItem - 代码

public void popIoItem(object obj){

            if (ioType == 1)
            {
                tb.Text = (string)obj;
            }
        }

我的界面

我正在尝试在右侧创建菜单,每次按下类别时,菜单都会向上/向下滑动,并且我会使用文本框和其他 io 元素隐藏/显示正确的用户控件。

更多代码

当单击左侧的框之一时,将运行以下方法:

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-items 用户控件,它基本上绘制了一个蓝色框并将一个控件放在 if ie 前面。文本框。

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;


            }


        }
    }

【问题讨论】:

  • 只是出于兴趣:你想用这个解决什么问题?只是问,因为可能有更好的方法。
  • popIoItem(caseState) 内部到底发生了什么?
  • @ChrisWue - 我有一个带有一些垂直标签的自定义菜单。每个选项卡都包含一系列控件,其中包含有关所选项目的信息。我将在问题中添加一个屏幕。
  • 这当然是不可能的。如果您可以HideShow 控件,那么它必须存在,并且您无法获得NullReferenceException。您需要展示更多代码才能诊断出实际问题。
  • @Cody Gray - 我会发布更多代码和信息。

标签: c# winforms textbox


【解决方案1】:

尝试使用调试器检查代码。可能还有其他问题吗? NullReference 意味着您尝试对不存在的对象执行某些操作。 Show/Hide 只是在正常情况下将控件的 Visible 属性设置为 true/false(无需自定义重载/Control 类的更改)。

【讨论】:

    【解决方案2】:

    所以我知道出了什么问题。问题出在我最初的预期之外。它不起作用的原因是因为我在 Load_event 中创建了我的文本框控件。当我尝试设置尚未创建的文本框控件的文本属性值时。我以这种方式创建包含文本框的用户控件的原因是为了便于将它们拖到设计器中的屏幕中。我发现了这个讨论 'UserControl' constructor with parameters in C#,它向我展示了另一种方法,现在它可以工作了。

    【讨论】:

    • +1 用于在您自己想出答案后发布答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 2022-01-17
    • 1970-01-01
    • 2011-08-12
    • 2018-09-11
    相关资源
    最近更新 更多