Results 1 to 8 of 8

Thread: [2.0] GUI editing problem via thread

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    10

    [2.0] GUI editing problem via thread

    Hello,

    I would like to edit textMain from Form1 via a static class on an other thread. But I get the following error:

    An object reference is required for the nonstatic field, method, or property 'WindowsApplication1.Form1.SetText(string)'

    I simplified the example. Anybody who can help me?

    Code:
        public partial class Form1 : Form
        {
            // Set text delegate
            public delegate void SetTextDelegate(string text);
    
            Thread trdTest;
    
            // Form1
            public Form1()
            {
                InitializeComponent();
                trdTest = new Thread(new ThreadStart(DoThread));
                trdTest.Start();
            }
    
            // Thread
            void DoThread()
            {
                StaticClass.EditGUI();
            }
    
            // Change textbox text
            public void SetText(string text) 
            {
                if (txtMain.InvokeRequired) 
                {
                    object[] params_list = new object[] { text };
                    txtMain.Invoke(new SetTextDelegate(SetText), params_list);
                }
                else 
                {
                    txtMain.Text = text;
                }
            }
    
        }
    Code:
        // Static class
        public static class StaticClass
        {
            public static void EditGUI()
            {
                Form1.SetText("test");
            }
        }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] GUI editing problem via thread

    SetText is not a static method so you can't just call it on the Form1 class. You have to call it on an instance of the Form1 class. If there's no Form1 object then there's no TextBox, so what Text are you supposed to be setting?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    10

    Re: [2.0] GUI editing problem via thread

    Ok so now I have this:

    Code:
        // Static class
        public static class StaticClass
        {
            public static void EditGUI()
            {
                Form1 frm = new Form1();
                frm.SetText("test");
            }
        }
    It doesn't come up with the error, but I don't see any changes on Form1 either.
    If I use frm.Show(); then the changes are there, but on a new form, thats not really what i want. The constructor also gets called again, which makes my app go crazy.

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

    Re: [2.0] GUI editing problem via thread

    That code is creating a new Form1 object and calling its SetText method, which will set the Text property of the TextBox on that new form. If you build a new house and give it a paint job, does that mean that your old house is anew colour? Of course not, because the new house is a completely different object to the old house. The same is true of your forms. You can't create a new form and change something about and expect that to change the old form too. If you have an existing form and you want to change the Text of its TextBox then you have to call the SetText method of that existing Form1 object, not a new one.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    10

    Re: [2.0] GUI editing problem via thread

    I understand, but how do I do that in this case?

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

    Re: [2.0] GUI editing problem via thread

    I'm not quite sure why you've chosen to use a static class to perform your action. I would think that an instance of a class would be preferable. That said, I'm not quite sure why you're not just using a method of the form itself. I don't really see what purpose your StaticClass serves.

    All that aside, if you want to pass data to a thread's entry method then try using a ParameterizedThreadStart delegate instead of a vanilla ThreadStart. You can then pass one object to the Thread.Start method and get that object back as a parameter in your entry method.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    10

    Re: [2.0] GUI editing problem via thread

    The thread is a TCP listening thread, it has a listen function in Form1. It passes the received packets to the static class. I can receive about 50 kind of packets, so I have about 50 methods in the static class. I put them there to make it all a bit more clean.

    I tried to make it a normal class, created an instance of it in Form1 and referred to the methods that way. But then it still requires a instance of an object.

    I also tried making it a partial Form1 class, that seems to work, but I don't know if this is the right way to do it.

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

    Re: [2.0] GUI editing problem via thread

    OK, obviously your class is a lot more complex than you've shown so it sounds like having a class is the right way to go. It seems like you think that a static class can perform some magic that an instance of a class can't though. I would go with a regular class and have your form create an instance of it and pass a reference to itself at that point. This class could take a form as a constructor parameter for instance. If that was the only constructor then you couldn't create an instance without passing a form. That will create the relationship you're looking for.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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