Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I'm trying to call a non-static method (LoadingListBox) in my class (ListDisplay) from a button click event in my AddTask class.

namespace Project
{
    public partial class ListDisplay : Form
    {

        public  void LoadingListBox()
        {
            //code
        }
    }
 }

namespace Project
{
    public partial class AddTask : Form
    {
         private void btnSubmit_Click(object sender, EventArgs e)
         {
             //code
             //I want to call LoadingListBox here
         }
    }
}

I have tried creating an instance of ListDisplay by doing ListDisplay listDisplayI = new ListDisplay(); and then writing ListDisplay.LoadingListBox();

There were no errors with this code but when debugging the flow of control wouldn't go to the method it just carried on past it. I have also tried doing what the answer suggests from this question:

Call a public method of main class from another class

But that comes up with an error as I can't overload the btnSubmit_Click method as the Designer freaked out. So I created a new method that had all of the code of btnSubmit_Click called public static void addingTask(ListDisplay) but I then had to create an instance of ListDisplay in order to put it as an argument in the btnSubmit_Click method hence ending up with two instances which is not what I want (due to answer of question).

So now I have come up with nothing and would appreciate any help you could give in order to call this LoadingListBox method. Thanks!

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Your two choices are based on whether or not you want a new instance of ListDisplay when you click the button or not.

Option 1:

namespace Project
{
    public partial class AddTask : Form
    {
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            var ld = new ListDisplay();
            ld.Show();
            ld.LoadingListBox();
        }
    }
}

Option 2:

namespace Project
{
    public partial class AddTask : Form
    {
        private ListDisplay _ld;
        public AddTask(ListDisplay ld)
        {
            _ld = ld;
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            _ld.LoadingListBox();
        }
    }
}
share|improve this answer
    
Thanks for answering, with option 2 how would I call AddTask as I've tried putting AddTask AddTaskI = new AddTask(this); and AddTask AddTaskI = new AddTask(ListDisplay); and they haven't worked? – Emma D Apr 12 at 0:51
    
@EmmaD - What is this in the new AddTask(this)? Is it an instance of ListDisplay? Also, on SO, please never say "they haven't worked" without explaining what went wrong. – Enigmativity Apr 12 at 1:40
    
Don't worry I must have typed it wrong before as using new AddTask(this) now works! this being the instance of ListDisplay as I am calling it from the ListDisplay form. – Emma D Apr 12 at 1:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.