0

I am working in an Excel file with Visual Studio and programming in C#. I added a listbox control to the sheet Seismic2D. I want to fill this listbox with information that is in the sheet FileList. The button that I am trying to program is in ribbon.

I tried to use Sis2D clase=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)

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

public partial class Sis2D

Another thing that I tried was adding this in 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.

I want to be able to interact with the ListBox, adding items and reading them.

5
  • 1
    The constructor is probably in the other partial definition of the class. Commented Sep 26, 2019 at 18:12
  • 2
    partial class is such a bad name. the partial keyword just indicates that your class definition may be spread across multiple files. Once compiled though, you got a single complete class. --- so Dorian, look for the file that defined the constructor for Sis2D. Commented Sep 26, 2019 at 18:17
  • Visual Studio creates a file for Sis2D (because is linked to a worksheet) and creates a file for the Ribbon. It is in the ribbon where I have a button that I want to fill the ListBox. The listbox is inside the worksheet manage by Sis2D. So, what I want is to call a method located in a different partial class from a class located in a different partial class. Commented Sep 26, 2019 at 18:43
  • you want to call a private method from a different class? nuff said, no? Commented Sep 26, 2019 at 20:16
  • The main idea behind this is that I add Listbox. The problem is how I can add items to it or change the items. It is almost the same than the vba listbox: it depends on the worksheet where it is added. Commented Sep 26, 2019 at 21:32

2 Answers 2

0

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

following Constructor in the Sis2D class.

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

        }

If you want to access the method in Sis2D, you could add a empty constructor in 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();

        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very mucho for your answer. I did what you suggest, now the error is: There is no argument given that corresponds to the required formal parameter 'factory'of WorksheetBase.WorksheetBase(Factory,IServiceProvider,string, string)
where is the WorksheetBase class from? Because you don't give the information before, I could not how to set it. You could show all the class so that we can solve your problem wihout meeting the error.
Sorry @Jack, Sis2D is the name I gave to the file that is the class for a worksheet. I did not understand very well how VSTO works. SO, possibly, for this reason, I could not be so clear in my question. Anyway, I think the error gives no much information about the real problem. That's why I wanted to write the solution I got and is above these lines.
0

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.

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. The following picture shows how the worksheet looks: enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.