简体   繁体   English

如何使用来自另一个部分 class 的部分 class 中的方法

[英]How to use a method that is in a partial class from another partial class

I am working in an Excel file with Visual Studio and programming in C#.我正在使用 Visual Studio 处理 Excel 文件并在 C# 中编程。 I added a listbox control to the sheet Seismic2D.我在工作表 Seismic2D 中添加了一个列表框控件。 I want to fill this listbox with information that is in the sheet FileList.我想用工作表 FileList 中的信息填充这个列表框。 The button that I am trying to program is in ribbon.我正在尝试编程的按钮位于功能区中。

I tried to use Sis2D clase=new Sis2D();我尝试使用 Sis2D case=new Sis2D(); but I got an error (There is no argument given that corresponds to the required formal parameter 'factory'of Sis2D.Sis2D(Factory,IServiceProvider)但我得到一个错误(没有给出与所需的形式参数'factory' of Sis2D.Sis2D(Factory,IServiceProvider)相对应的参数

In the Ribbon file...在功能区文件中...

public partial class Ribbon1
    {
       private void ListRefresh_Click(object sender, RibbonControlEventArgs e)
       {
           Excel.Worksheet wsFL = Globals.ThisWorkbook.Worksheets["FileList"];
           Sis2D clase = new Sis2D();
           clase.ListaFiles.Items.Add(wsFL.Range["A2"]);
           clase.ListaFiles.Items.Add(wsFL.Range["A3"]);
        }
    }

In the Sis2D.cs file在 Sis2D.cs 文件中

public partial class Sis2D

Another thing that I tried was adding this in Sis2D我尝试的另一件事是在 Sis2D 中添加它

public partial class Sis2D
{
public void LlenaListBox()
        {
           Excel.Worksheet wsFL = Globals.ThisWorkbook.Worksheets["FileList"];
           ListaFiles.Items.Add(wsFL.Range["A2"].Value);
           ListaFiles.Items.Add(wsFL.Range["A3"].Value);
        }
}

But I do not know how to call this method from the Ribbon partial class.但我不知道如何从功能区部分 class 调用此方法。

I want to be able to interact with the ListBox, adding items and reading them.我希望能够与 ListBox 进行交互,添加项目并阅读它们。

The error that 'There is no argument given that corresponds to the required formal '没有给出与所需形式相对应的参数的错误

parameter 'factory'of Sis2D.Sis2D(Factory,IServiceProvider)' means that you only have the Sis2D.Sis2D(Factory,IServiceProvider)'的参数'factory'意味着你只有

following Constructor in the Sis2D class.遵循 Sis2D class 中的构造函数。

 public Sis2D(Factory, IServiceProvider)
        {
         ////////////

        }

If you want to access the method in Sis2D, you could add a empty constructor in Sis2D class.如果要访问 Sis2D 中的方法,可以在 Sis2D class 中添加一个空的构造函数。 Like the following:如下所示:

   public partial  class Sis2D
   {
    public Sis2D()  //  you need to add this code
    {
    }
    public void LlenaListBox()
    {
        Excel.Worksheet wsFL = Globals.ThisWorkbook.Worksheets["FileList"];
        ListaFiles.Items.Add(wsFL.Range["A2"].Value);
        ListaFiles.Items.Add(wsFL.Range["A3"].Value);
    }
    }

Then you could use the following code to access the method without throwing exception:然后您可以使用以下代码访问该方法而不会引发异常:

public partial  class Ribbon1
    {
        public void ListRefresh_Click()
        {
            Sis2D clase = new Sis2D();
            clase.LlenaListBox();

        }
    }

First I want to say thank you for all your help and interest in helping me.首先,我要感谢您对帮助我的所有帮助和兴趣。 I want to apologize if possibly I was not clear with my question, because I am a beginner.如果我的问题可能不清楚,我想道歉,因为我是初学者。 Finally, what I wanted, filling the ListBox inside a worksheet from Visual Studio with C#, I got it.最后,我想要的是,用 C# 在 Visual Studio 的工作表中填充 ListBox,我明白了。

In the Ribbon, the code for the button is:在功能区中,按钮的代码是:

C#
public partial class Ribbon1
{
private void FL_Click(object sender, RibbonControlEventArgs e)
   {
       Sis2D s = Globals.Sis2D;
       s.LlenaListBox();   
   }
}

In the sheet, the code is:在工作表中,代码是:

public partial class Sis2D 
{
public void LlenaListBox()
    {
            Excel.Worksheet wsFL = Globals.ThisWorkbook.Worksheets["Sheet1"];
            ListaF.Items.Add(wsFL.Range["A10"].Value);
            ListaF.Items.Add(wsFL.Range["A11"].Value);
            ListaF.Items.Add(wsFL.Range["A12"].Value);
    }
}

This is all.这是所有的了。 ListaF is a ListBox added to sheet1 into a worksheet from C# in Visual Studio. ListaF 是从 Visual Studio 中的 C# 添加到 sheet1 的 ListBox。 The following picture shows how the worksheet looks: enter image description here下图显示了工作表的外观:在此处输入图像描述

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

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