locked
How to access Form1 class members from inside another classes method. RRS feed

  • Question

  • I'm creating a Winform Desktop Application and I have a class named UpdateTextboxes and inside that class is a method for adding new text boxes to screen. The problem that i am having is that I can't add the text box to the control so it can be added to a container and printed on screen because when i use the "this" keyword the compiler says i can't use it in a static method. I've thought about making the method non-static but that wouldn't make since given that i don't want to have to instantiate a new object to be able to use the method.

    class UpdateTextBoxes
        {
            public static void AddNewTextbox()
            {
                List<int> PrntPrisonerName = new List<int>() { 1,2,3,4 };
                for (int i = 4; i < PrntPrisonerName.Capacity; i++)
                {
                    TextBox TxtBox = new TextBox();                
                    TxtBox.Name = "PrntPrisonerName" + PrntPrisonerName.Capacity.ToString();
                    PrntPrisonerName.Add(i);
                    TxtBox.BackColor = System.Drawing.SystemColors.ActiveBorder;
                    TxtBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                    TxtBox.Enabled = false;
                    this.PrisonerNameViewBox.Controls.Add(TxtBox); //Line causing Trouble.
                    TxtBox.Font = new System.Drawing.Font("Rockwell", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    TxtBox.Location = new System.Drawing.Point(7, (167 + 42));
                    TxtBox.Size = new System.Drawing.Size(122, 25);
                }          
            }          
        }

    Tuesday, July 31, 2018 2:05 AM

All replies

  • You need to pass the ControlCollection to the method as an argument.

    class UpdateTextBoxes
        {
            public static void AddNewTextbox(ControlCollection controls) // Pass the collection in here.
            {
                List<int> PrntPrisonerName = new List<int>() { 1,2,3,4 };
                for (int i = 4; i < PrntPrisonerName.Capacity; i++)
                {
                    TextBox TxtBox = new TextBox();                
                    TxtBox.Name = "PrntPrisonerName" + PrntPrisonerName.Capacity.ToString();
                    PrntPrisonerName.Add(i);
                    TxtBox.BackColor = System.Drawing.SystemColors.ActiveBorder;
                    TxtBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                    TxtBox.Enabled = false;
                    controls.Add(TxtBox); //Use the controls passed in here.
                    TxtBox.Font = new System.Drawing.Font("Rockwell", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    TxtBox.Location = new System.Drawing.Point(7, (167 + 42));
                    TxtBox.Size = new System.Drawing.Size(122, 25);
                }          
            }          
        }
    
    
    // Call something like this.
    UpdateTextBoxes.AddNewTextBox(PrisonerNameViewBox.Controls);

    Tuesday, July 31, 2018 4:42 AM
  • Thank you for your feedback and to answer your questions, I am trying to have it so every time a button is clicked it adds a new text box to the screen with the same name (PrntPrisonerName) but with a different number at the end so the names don't overlap (I will be using these textboxes to show data from a database onto a screen). That is why the number increases every iteration. As far as the Capacity I noticed after I posted my question that Count should have been used instead. Also, this is my first time creating a desktop application and I was confused on where to put the logic part of my code and sectioning the logic parts out into different classes sounded the safest way to avoid mixing logic with UI code, was this the wrong approach? If so would you mind helping me in the right direction. I hope i've cleared up some confusion you may have had. 

    Tuesday, July 31, 2018 7:15 AM