简体   繁体   English

从不同的表单调用表单实例的公共方法导致错误

[英]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."我收到错误消息“Form 不包含 'GetProgressBar' 的定义,并且找不到接受类型 'Form' 的第一个参数的可访问扩展方法 'GetProgressBar'。” I'm simply trying to update the progress bar on a different form for a task.我只是想在不同的表单上更新任务的进度条。 Here is method in Form1,这是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();错误发生在 ProgressBar progressBar1 = form12.GetProgressBar(); 行上

Here is my code in Form12 class.这是我在 Form12 类中的代码。

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. progressBar1 是我通过 Visual Studio 设计器添加到表单中的 ProgressBar 实例。 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.显然,创建 Form12 的实例然后调用其公共方法之一在这里不起作用。 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?这里有什么区别?

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.这是因为GetProgressBar()是在Form12 中定义的,但是您试图从Form类型的变量访问它,该变量是父类并且没有该方法。

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

Replace Form with Form12 in your form12 variable declaration or simply use var keyword.在您的form12变量声明中用Form12替换Form或简单地使用var关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM