Solved

Setting GUI object from another class other than GUI class. How do you do it.

Posted on 2006-06-20
8
198 Views
Last Modified: 2010-04-16
Hi,

I've created an GUI form in C# on .NET (called form1).  I also have another class (class1) with no GUI stuff in it, what I want to do is for a function in Class1 to set a label on form1.
But, Im not sure how you do it?  I've tried to inherit form1 into Class1, but with no luck.  

Would anyone know how I should do it?

Thanks in advance for your help!

 
0
Comment
Question by:malben
  • 4
  • 3
8 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16946116
Of the MANY ways this could be done...

How about:

    (1) Pass a reference to either Form1 or your Label into your Class so you can directly manipulate them from within the Class

    (2) Make your Class raise a custom event that Form1 subscribes to.  When it receives this event, it uses the parameters to update the appropriate label.

Do you have a preference between these two methods?
0
 

Author Comment

by:malben
ID: 16946424
I'm think passing a reference would be the best option.

I'm new to programming so I wonder if you could help with the syntax i.e. what should I write below?

Thanks for your help

using System;


namespace WindowsApplication1
{

      public class Class1()
      {
            public Class1()
      
            
            }
      }
}
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 50 total points
ID: 16946724
2003 example:

namespace WindowsApplication1
{

     public class Class1()
     {
         
           public void ShowForm()
           {
               Form1 frm = new Form1();
               frm.Label1.Text = "This is a test";
               frm.ShowDialog();
               frm.Dispose();
          }
     }
}

You would need to set the modifier for the Label to Internal.

Bob

Bob
0
 

Author Comment

by:malben
ID: 16946807
Many thanks bob!

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16947029
Is that really what you wanted?

In Bobs example, Class1 is creating a new instance of Form1.

I was thinking that Form1 was creating an instance of Class1 and so we needed to pass a reference to Form1 into Class1 so that the class could manipulate the form...
0
 

Author Comment

by:malben
ID: 16948750
Idle Mind. how would that look like as code?

Thanks
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16951209
At it's simplest...         (this assumes that on Form1, the Modifiers() property of textBox1 has been changed to Internal)

    public partial class Form1 : Form
    {

        private void button1_Click_1(object sender, EventArgs e)
        {
            Class1 c1 = new Class1();
            c1.frm1 = this;
            c1.Foo();
        }

    }

    public class Class1
    {
        public Form1 frm1;

        public void Foo()
        {
            if (frm1 != null)
            {
                frm1.textBox1.Text = "Hello from Class1";
            }
        }
    }
0
 

Author Comment

by:malben
ID: 16954181
Many thanks Idle Mind your help was much appreciated!

 
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data Saving 5 56
MVC5 Dataformat String for Phone not working 3 26
Allow space in this pattern 2 49
Stream.BeginRead and Stream.EndRead in .NET Core 5 38
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now