1

I have a hidden control which contains a textbox control and I want to set the text property of the textbox but i get an NullReferenceException. However if I show the control, set the value and then hide it then i get no exception.

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

However this feels like a really unclean and not very elegant way to do it. And i'm seeing some flickering because i have to do this to 4 controls with up to 8 textboxes on each.

Is there any way to set the text propery of the textboxes while the control is hidden? Or is it perhaps a better idea to populate my textboxes when showing the control? And will this slow down my application as it needs to populate everytime the control is shown?

popIoItem - code

public void popIoItem(object obj){

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

My interface

I'm trying to create the menu to the right and on each pressing of the categories the menus slide up/down and i hide/show the proper user control with the textboxes and other io-elements.

Interface: it is the menu to the right i'm trying to create

More code

When one of the boxes to the left is click'ed the following method is run:

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

        }

The caseInfoMenu is the right menu. It consists of some drawing and mouse logic that draws the menu and handles hit-detection. Besides this it contains 4 user controls, one for each of the vertical tabs.

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



        }

Inside each of these user controls I have io-items user controls which essentially draws a blue box and puts a control in front of if ie. the textbox.

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;


            }


        }
    }
Bildsoe
  • 1,310
  • 6
  • 31
  • 44
  • Just out of interest: What problem do you try to solve with this? Just asking because there might be a better way. – ChrisWue Apr 15 '11 at 09:26
  • What exactly is happening inside popIoItem(caseState)? – Edwin de Koning Apr 15 '11 at 09:28
  • @ChrisWue - I have a custom menu with some vertical tabs. Each tab contain a range of controls which contains information about a selected project. I'll add a screen to the question. – Bildsoe Apr 15 '11 at 09:59
  • This is of course impossible. If you can `Hide` and `Show` the control, then it must exist, and you can't be getting a `NullReferenceException`. You need to show more of your code to be able to diagnose the *actual* problem. – Cody Gray - on strike Apr 15 '11 at 10:02
  • @Cody Gray - i'll post more code and info. – Bildsoe Apr 15 '11 at 10:04
  • Can this happen because i've put code into ioItem_Load, and when I run the popIoItem, it hasn't loaded yet? I initially did this, to be able to create the ioItems quickly by dragging them in the designer and if i placed this code in the constructor it iqnored the values of the properties of padding and type. – Bildsoe Apr 15 '11 at 10:27

2 Answers2

1

Try checking the code with debugger..May be there is something other going on there? NullReference means that you try to do something with object that doesn't exist. Show/Hide just set Visible property of the control to true/false in normal situation(without custom overload/changes of Control class).

0x49D1
  • 8,505
  • 11
  • 76
  • 127
1

So i figured out what was wrong. The problem was something else than what I initially expected. The reason why it didn't work was because I created my textbox controls in the Load_event. When I tried to set the value of the text property of the textbox controls they hadn't been created yet. The reason why I had created the user controls containing the textboxes this way was in order to make it easy to drag them into the screen in the Designer. I found this discussion 'UserControl' constructor with parameters in C#, which showed me another way of doing it and now it works.

Community
  • 1
  • 1
Bildsoe
  • 1,310
  • 6
  • 31
  • 44