Results 1 to 13 of 13

Thread: [RESOLVED] Classes To Access Form Objects

  1. #1

    Thread Starter
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Resolved [RESOLVED] Classes To Access Form Objects

    Hi. I got 2 things in my project. A form class, and a separate class. I wanna be able to change the data of the form using this separate class or any other class for that matter. For example, I'm trying to change the backcolor of a picturebox using this separate class. However during design time, I'm getting a "Use of unassigned local variable error MyForm." Here is what I have in my class module:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    
    namespace AStar_Pathfinding
    {
        class modMain
        {
            public bool Running = false;
            public bool Done = false;
    
            public void Main()
            {
                frmMain MyForm;
                MyForm.picMain.BackColor = Color.FromArgb(0, 0, 0);
    
            }
    
        }
    }
    In bold is my error. As for the form itself, I have made all my object modifiers public just so they are accessible. The form itself is just this:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace AStar_Pathfinding
    {
        public partial class frmMain : Form
        {
            public frmMain()
            {
                InitializeComponent();
            }
    
            private void frmMain_Load(object sender, EventArgs e)
            {
            }
        }
    }
    I haven't called the Main() just yet though until I get pass this issue. Thanks in advance.

  2. #2

    Thread Starter
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Classes To Access Form Objects

    I sort have fixed it but nothing happened with the picturebox, even though no error appeared. The backcolor didnt change black at all. Instead it did nothing:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    
    namespace AStar_Pathfinding
    {
        public class modMain
        {
            public bool Running = false;
            public bool Done = false;
    
            public void Main()
            {
                frmMain MyForm = new frmMain();
                MyForm.picMain.BackColor = Color.FromArgb(0, 0, 0);
            }
    
        }
    }
    With the form I just called main like so:

    Code:
            private void frmMain_Load(object sender, EventArgs e)
            {
                modMain Test = new modMain();
                Test.Main();
            }

  3. #3

    Thread Starter
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Classes To Access Form Objects

    Anybody? It can't be that hard. The solution I'm looking for should be simple. All I'm doing is similar to vb. Change a form property using another class in the same project.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Classes To Access Form Objects

    You appear to be trying to use a default instance as you can in VB. Default instances are a VB feature that doesn't exist in C#. The fact that you can't figure out how to do this is a perfect example of why default instances are bad. They give the impression that forms are somehow different to other objects. They are not. How would you do this with any type other than a form? You'd have to pass a reference to the appropriate object into this 'modMain' in order to affect it, wouldn't you? It's exactly the same with forms.

    Your first attempt declared a variable of the appropriate type but never assigned an object to that variable. Your second attempt created a new 'frmMain' object and changed that but that's obviously no use if you want to affect an existing 'frmMain' object. If you have an existing piece of paper and you then get a new piece of paper and draw on that, would you expect to see a picture magically appear on the original piece of paper? Of course not. They are two different pieces of paper and what you do to one has no effect on the other. Your code is exactly the same. You have two different 'frmMain' objects and what you do to one has no effect on the other.

    If you want a 'modMain' instance to be able to affect your existing 'frmMain' object then it needs a reference to that 'frmMain' object. Where it gets that reference from is up to you but, in this contrived example, the most likely source would be from the instance itself. You've got the 'frmMain' object creating the 'modMain' object so at that point the form can pass the class instance a reference to itself, either as a constructor parameter or by setting a property.

    Having said all that, if you want to work with forms in C# in a similar way to how you use default instances in VB then follow the CodeBank link in my signature and check out my thread on Singleton Forms.

  5. #5

    Thread Starter
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Classes To Access Form Objects

    Thanks for the reply, but I couldn't find your example. Maybe if you could hook me up with a link to it that would be awesome. Also since my attempts continue to fail could you show me a small code sample of changing a property on a form from a class? I tried google but the samples on there weren't working either. Thanks in advance

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Classes To Access Form Objects

    If I was going to find that CodeBank thread for you, I would navigate to the C# CodeBank forum, sort by Thread Starter and then page until I found threads started by me. You can do that just as easily as I can.

  7. #7
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Classes To Access Form Objects

    Quote Originally Posted by jmcilhinney View Post
    If I was going to find that CodeBank thread for you, I would navigate to the C# CodeBank forum, sort by Thread Starter and then page until I found threads started by me. You can do that just as easily as I can.
    Here is a submission in C# by Forum Account based on your VB.NET version. Is that the one you mean?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Classes To Access Form Objects

    Oh goodness! I had completely forgotten that it wasn't myself who wrote the C# version of that code. Nightwalker, thanks very much. Jacob Roman, sorry very much.

  9. #9

    Thread Starter
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Classes To Access Form Objects

    Its no worries. Basically I am learning C# cause I'm weak at it. Yet I've been a vb guru for far too long. More like 16 years. That's why I'm trying to beef up my C# skills. One of my biggest hurdles is the thread I just started. Simply accessing form objects from another class or a module. Funny thing is that I made many DirectX programs using C# only using just one code window. I'll check out that example you were talking about. Its no wonder I didn't find it. Wasn't under your name

    [EDIT] Ok I downloaded a VB.NET to C# converter which works great. And I recreated what I wanted in vb, then converted it to C# using that program. When I run the C# version, it did exactly what I wanted. Turned the picturebox black from a class. Now here's the problem. I used the same exact code in my actual program, and it did nothing. I even tried comparing properties side by side. Everything was the same as the other. All my modifiers are Public as well. What on earth could I be missing this time? Heres the new code that works in one C# app but not the other.

    c# Code:
    1. //clsMain.cs
    2.  
    3. //INSTANT C# NOTE: Formerly VB project-level imports:
    4. using System;
    5. using System.Collections;
    6. using System.Collections.Generic;
    7. using System.Data;
    8. using System.Drawing;
    9. using System.Diagnostics;
    10. using System.Windows.Forms;
    11. using System.Linq;
    12. using System.Xml.Linq;
    13.  
    14. namespace AStar_Pathfinding
    15. {
    16.     public class clsMain
    17.     {
    18.         public void Main()
    19.         {
    20.  
    21.             frmMain.DefaultInstance.picMain.BackColor = Color.FromArgb(0, 0, 0);
    22.  
    23.         }
    24.  
    25.     }
    26.  
    27. }
    28.  
    29. //frmMain.cs
    30.  
    31. //INSTANT C# NOTE: Formerly VB project-level imports:
    32. using System;
    33. using System.Collections;
    34. using System.Collections.Generic;
    35. using System.Data;
    36. using System.Drawing;
    37. using System.Diagnostics;
    38. using System.Windows.Forms;
    39. using System.Linq;
    40. using System.Xml.Linq;
    41.  
    42. namespace AStar_Pathfinding
    43. {
    44.     public partial class frmMain
    45.     {
    46.  
    47.         internal frmMain()
    48.         {
    49.             InitializeComponent();
    50.         }
    51.  
    52.         private void frmMain_Load(object sender, EventArgs e)
    53.         {
    54.             clsMain Test = new clsMain();
    55.             Test.Main();
    56.         }
    57.  
    58.     private static frmMain _DefaultInstance;
    59.     public static frmMain DefaultInstance
    60.     {
    61.         get
    62.         {
    63.             if (_DefaultInstance == null)
    64.                 _DefaultInstance = new frmMain();
    65.  
    66.             return _DefaultInstance;
    67.         }
    68.     }
    69.     }
    70.  
    71. }

    And heres the code I have in my current program that doesn't work:

    c# Code:
    1. //clsMain.cs
    2.  
    3. using System;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6. using System.Text;
    7. using System.Drawing;
    8.  
    9. namespace AStar_Pathfinding
    10. {
    11.     public class clsMain
    12.     {
    13.         public void Main()
    14.         {
    15.             frmMain.DefaultInstance.picMain.BackColor = Color.FromArgb(0, 0, 0);
    16.         }
    17.     }
    18. }
    19.  
    20. //frmMain.cs
    21.  
    22. using System;
    23. using System.Collections.Generic;
    24. using System.ComponentModel;
    25. using System.Data;
    26. using System.Drawing;
    27. using System.Linq;
    28. using System.Text;
    29. using System.Windows.Forms;
    30.  
    31. namespace AStar_Pathfinding
    32. {
    33.     public partial class frmMain : Form
    34.     {
    35.         public frmMain()
    36.         {
    37.             InitializeComponent();
    38.         }
    39.  
    40.         private void frmMain_Load(object sender, EventArgs e)
    41.         {
    42.             clsMain Test = new clsMain();
    43.             Test.Main();
    44.         }
    45.         private static frmMain _DefaultInstance;
    46.         public static frmMain DefaultInstance
    47.         {
    48.             get
    49.             {
    50.                 if (_DefaultInstance == null)
    51.                     _DefaultInstance = new frmMain();
    52.  
    53.                 return _DefaultInstance;
    54.             }
    55.         }
    56.  
    57.     }
    58. }

  10. #10
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Classes To Access Form Objects

    Rather than implementing Vb's default instance consider the following...
    Code:
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace Passing_forms
    {
        public partial class frmMain : Form
        {
            public frmMain()
            {
                InitializeComponent();
            }        
            
            private void frmMain_Load(object sender, EventArgs e)
            {
                clsMain Test = new clsMain();
                Test.One(this);
    	    Test.Two(picMain);
    	    picMain.BackColor = Test.Three();
            }
    
            public PictureBox MainPictureBox
            {
                get { return picMain; }
            }
        }
    
        public class clsMain
        {
            public void One(frmMain form)
            {
                form.MainPictureBox.BackColor = Color.Black;
            }
    
            public void Two(PictureBox pictureBox)
            {
                pictureBox.BackColor = Color.Black;
            }
    
            public Color Three()
            {
                return Color.Black;
            }
        }
    }
    W o t . S i g

  11. #11

    Thread Starter
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Classes To Access Form Objects

    Sweet! So far that worked using the first one. But the thing is though I kinda wanna avoid using the form as an argument in my Main() function. Is it possible?

  12. #12
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Classes To Access Form Objects

    Going back to the default instance implementation that was not working, I think you were looking at the wrong code. Look for the application entry point.
    Code:
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                //Application.Run(new frmMain());
                Application.Run(frmMain.DefaultInstance);
            }
        }

    Alternatively pass a reference in the constructor. If clsMain only affects the PictureBox then it might be better to pass that instead.
    Code:
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace Passing_forms
    {
        public partial class frmMain : Form
        {
            public frmMain()
            {
                InitializeComponent();
            }        
            
            private void frmMain_Load(object sender, EventArgs e)
            {
                clsMain test = new clsMain(this);
                Test.Main();
            }
    
            public PictureBox MainPictureBox 
            {
                get { return picMain; }
            }
        }
    
        public class clsMain
        {
            frmMain form;
            
            public clsMain(frmMain form)
            {
                this.form = form;
            }
            
            public void Main()
            {
                form.MainPictureBox.BackColor = Color.Black;
            }
        }
    }
    Personally I try to avoid getting into a situation where I need to access form elements from none GUI classes.
    W o t . S i g

  13. #13

    Thread Starter
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Classes To Access Form Objects

    Ill be damn. Changing the Program.cs code worked! Thanks alot for everything guys.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width