UCProgrammer
UCProgrammer

Reputation: 557

Calling public method of form instance from different form causes error

I'm slightly baffled as to what the reason for this issue is. I'm receiving the error "Form does not contain a definition for 'GetProgressBar' and no accessible extension method 'GetProgressBar' accepting a first argument of Type 'Form' could be found." I'm simply trying to update the progress bar on a different form for a task. Here is method in Form1,

        private async void button_patches_Click(object sender, EventArgs e)
        {
            if (dbConnect.Connection != null)
            {
                if (dbConnect.Connection.State == ConnectionState.Open)
                {
                    if (textBox1.Text != "")
                    {
                        Form form12 = new Form12();
                        form12.ShowDialog();
                        ProgressBar progressBar1 = form12.GetProgressBar(); 
                        WSUS wsus = new WSUS(dbConnect, textBox1.Text);
                        Array kbList = wsus.ReadPatchList();
                        int patchesLength = kbList.Length; 
                        progressBar1.Maximum = 100;
                        progressBar1.Step = 1;
                        var progress = new Progress<int>(v => { progressBar1.Value = v; }) ;
                        await Task.Run(() => AddPatches(wsus, kbList, progress));  
                    }
                    else { MessageBox.Show("Please select your path for patches file first"); }
                }
                else { MessageBox.Show("Please open a connection to the database first."); }
            }
            else { MessageBox.Show("Please open a connection to the database first."); }
        }

The error is occurring on the line ProgressBar progressBar1 = form12.GetProgressBar();

Here is my code in Form12 class.

namespace myDBTemplate1.Forms
{
    public partial class Form12 : Form
    {
        public Form12()
        {
            InitializeComponent();
        }

        public ProgressBar GetProgressBar()
        {
            return progressBar1;
        }
    }
}

progressBar1 is an instance of ProgressBar that I added to the form via visual studio designer. Also, I set my modifier for my progress bar to public although that shouldn't cause this issue. Evidently, creating an instance of Form12 and then calling one of its public methods isn't working here. I have plenty of other classes in this project where I've calling public instance methods on already instantiated objects and they've worked fine. What's the difference here?

Upvotes: 0

Views: 42

Answers (1)

Vardir
Vardir

Reputation: 46

It's because GetProgressBar() is defined in Form12 but you trying to access it from variable of Form type which is parent class and does not have the method.

if (textBox1.Text != "")
{
   Form form12 = new Form12();
   form12.ShowDialog();

Replace Form with Form12 in your form12 variable declaration or simply use var keyword.

Upvotes: 1

Related Questions