[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Static controls

Posted on 2005-04-07
5
Medium Priority
?
359 Views
Last Modified: 2010-04-16
hi,
i have some controls in a form which are declared as static
for example :-
public static System.Windows.Forms.ComboBox comboBox1;

it is working fine and i am able to access the control from other forms without creating an object of it. But when i make any changes in the form like adding another control, the access modifier is disappearing.
i.e. my code becomes
public System.Windows.Forms.ComboBox comboBox1;

can anybody tell why this is happening and a solution

Thanking You
0
Comment
Question by:Dhanraj_k
  • 3
5 Comments
 
LVL 9

Expert Comment

by:noulouk
ID: 13724906
I think this is because you use the designer of Visual Studio. When you add any control at design time, VS refresh the code each time.
The only way is to declare your control manually in your code.
0
 
LVL 1

Expert Comment

by:Tiex
ID: 13724931
Visual studio automaticly regenerates code when you make any changes in form designer using special algorithm, this algorithm does not anticipate creating static members.

Currently you have not solution of this problem (I mean VS 2003)
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 13724980
Visual Studio designer removes static keyword from form controls. However, it allows to keep them as public. You can declare static form instance in the form, for example:

public static Form1 form1;

Initialize it:

private void Form1_Load(object sender, System.EventArgs e)
{
      form1 = this;
}

and use from any other form:

Form1.form1.textBox1 ...    

(assuming that textBox1 is public).
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 750 total points
ID: 13724995
This is my test.

//  Form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Test
{
    public class Form1 : System.Windows.Forms.Form
    {
        public System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(68, 52);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(164, 20);
            this.textBox1.TabIndex = 0;
            this.textBox1.Text = "sample";
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(96, 136);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(120, 36);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

        }
        #endregion

        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        public static Form1 form1;

        private void button1_Click(object sender, System.EventArgs e)
        {
            Form2 f = new Form2();

            f.ShowDialog(this);
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            form1 = this;
        }
    }
}


// Form2.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Test
{
    public class Form2 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form2()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.Location = new System.Drawing.Point(68, 76);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(164, 28);
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
            //
            // Form2
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.label1);
            this.Name = "Form2";
            this.Text = "Form2";
            this.Load += new System.EventHandler(this.Form2_Load);
            this.ResumeLayout(false);

        }
        #endregion

        private void Form2_Load(object sender, System.EventArgs e)
        {
            label1.Text = Form1.form1.textBox1.Text;
        }
    }
}
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 13725109
Another way is adding static class member which references desired control:

public static TextBox s_textBox1;

Initialize it in Form_Load handler:

s_textBox1 = textBox1;

and use from other forms:

Form1.s_textBox1 ...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question