?
Solved

C# accessing GUI object outside of the mainform class

Posted on 2011-02-23
4
Medium Priority
?
803 Views
Last Modified: 2012-05-11
Experts:

I have a MainForm class and a UI class.  The UI class is meant to handle things like populating tables and handling actions of the UI.  The item I'm trying to add objects to is a list and is defined in the MainForm class as "CompsList".

I tried to access that list with the code:

this.CompsList.Items.Add(...);

I get an error on the THIS statement, and if I remove that, I get a message stating CompsList doesn't exist.

How can I access the object to add or remove items when I want to?
0
Comment
Question by:piattnd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34962582
CompsList in MainForm would need to Public.  Then, you'd have to pass a REFERENCE to MainForm into your UI Class so that it can access it.
0
 
LVL 12

Author Comment

by:piattnd
ID: 34962700
public System.Windows.Forms.ListView CompsList;

That is the code that creates the list view in MainForm.  I tried to then use this line of code to execute the add of the list view item:

CompsList.Items.Add(listViewItem1);

Still getting an error saying CompsList isn't found.  Pardon the ignorance here, but I'm still not sure what I'm missing.  I understand what's going on, just not sure why or how you're meaning to get around it.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 34962760
I assume MainForm creates an instance of the UI Class?

One way to do it:
public partial class MainForm : Form
    {

        public UIClass uc = new UIClass();
        public System.Windows.Forms.ListView CompsList;

        public MainForm()
        {
            InitializeComponent();
            uc.MyMainForm = this; // <--- pass reference to our MainForm into UIClass
        }

    }

    public class UIClass
    {

        public MainForm MyMainForm;

        private void Foo()
        {
            MyMainForm.CompsList.Items.Add(...)
        }
    }

Open in new window

0
 
LVL 12

Author Closing Comment

by:piattnd
ID: 34963994
Worked perfectly AND I think it actually makes sense!  Great thorough answer, just a lack of brain power on my part!

Thanks.
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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

770 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