Solved

C# accessing GUI object outside of the mainform class

Posted on 2011-02-23
4
789 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
  • 2
  • 2
4 Comments
 
LVL 85

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 85

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

10 Experts available now in Live!

Get 1:1 Help Now