class scoping Form1 OK but -not at button, newbie 
Author Message
 class scoping Form1 OK but -not at button, newbie

What can't the button see the class foobar declared and
accessed by Form1 ?

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

                public class foobar
                {
                public string str;
                }

                public Form1()
                {
                        //
                        // Required for Windows Form
Designer support
                        //
                        InitializeComponent();
            foobar foo = new foobar();  // OK HERE
                        foo.str="test";
                        //
                        // TODO: Add any constructor code
after InitializeComponent call
                        //
                }

                /// <summary>
                /// Clean up any resources being used.
                /// </summary>
                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.button1 = new
System.Windows.Forms.Button();
                        this.SuspendLayout();
                        //
                        // button1
                        //
                        this.button1.Name = "button1";
                        this.button1.TabIndex = 0;
                        this.button1.Text = "Click Me";
                        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, 273);
                        this.Controls.AddRange(new
System.Windows.Forms.Control[] {

                                  this.button1});
                        this.Name = "Form1";
                        this.Text = "Form1";
                        this.ResumeLayout(false);

                }
                #endregion

                /// <summary>
                /// The main entry point for the
application.
                /// </summary>
                [STAThread]
                static void Main()
                {
                        Application.Run(new Form1());
                }

                private void button1_Click(object sender,
System.EventArgs e)
                {
         // foo.    BROKE HERE



Tue, 29 Jun 2004 23:16:38 GMT  
 class scoping Form1 OK but -not at button, newbie
You may want to try this inside your button's code

this.foo.str = "STRING";

A better method would be to not declare the class inside the Form's class.
Add a class file to your project and then you should be able to just
reference it as "foo" from anywhere.  This would also be neater programming
practice because you wouldn't have classes inside of other classes.  Just a
tip.

Timmy


Quote:
> What can't the button see the class foobar declared and
> accessed by Form1 ?

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

> public class foobar
> {
> public string str;
> }

> public Form1()
> {
> //
> // Required for Windows Form
> Designer support
> //
> InitializeComponent();
>             foobar foo = new foobar();  // OK HERE
> foo.str="test";
> //
> // TODO: Add any constructor code
> after InitializeComponent call
> //
> }

> /// <summary>
> /// Clean up any resources being used.
> /// </summary>
> 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.button1 = new
> System.Windows.Forms.Button();
> this.SuspendLayout();
> //
> // button1
> //
> this.button1.Name = "button1";
> this.button1.TabIndex = 0;
> this.button1.Text = "Click Me";
> 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, 273);
> this.Controls.AddRange(new
> System.Windows.Forms.Control[] {

>   this.button1});
> this.Name = "Form1";
> this.Text = "Form1";
> this.ResumeLayout(false);

> }
> #endregion

> /// <summary>
> /// The main entry point for the
> application.
> /// </summary>
> [STAThread]
> static void Main()
> {
> Application.Run(new Form1());
> }

> private void button1_Click(object sender,
> System.EventArgs e)
> {
>          // foo.    BROKE HERE



Wed, 30 Jun 2004 05:11:41 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Newbie, simulate pressing the OK button in a propertysheet

2. Newbie, how to simulate pressing the ok button in a propertysheet

3. how to create a window without any close button (include OK button and X button)

4. OK should not be the DEFAULT BUTTON in property sheet

5. To Nudds: OK, OK, C is not portable

6. Dialog Box two OK buttons, two Cancel buttons and TODO still on it

7. Force default button on PropertyPage instead of OK button on PropertySheet

8. Class scoping problem

9. Class Wizard variable scoping

10. Newbie - Flat buttons - but not in a toolbar

11. How to code OK control button

12. Handling a click on the OK button in a formview

 

 
Powered by phpBB® Forum Software