C# accessing GUI object outside of the mainform class

Posted on 2011-02-23
Medium Priority
Last Modified: 2012-05-11

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:


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?
Question by:piattnd
  • 2
  • 2
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.
LVL 12

Author Comment

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:


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.
LVL 86

Accepted Solution

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()
            uc.MyMainForm = this; // <--- pass reference to our MainForm into UIClass


    public class UIClass

        public MainForm MyMainForm;

        private void Foo()

Open in new window

LVL 12

Author Closing Comment

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


Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Watch the video of Kernel Migrator for SharePoint, which demonstrate the process easily of migration from SharePoint to SharePoint, OneDrive for Business & Google Drive servers, Public Folder to SharePoint, File Server to SharePoint. The tool has va…
From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …

607 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