Solved

C# accessing GUI object outside of the mainform class

Posted on 2011-02-23
4
797 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
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…

691 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