none
How to call methods in ThisAddin class from a Form in my VSTO project ? RRS feed

  • Question

  • Hi

    I have created Excel Addin project in C#.NEt.Now i have ThisAddin.cs file which has a partial  class ThisAddin.Later i have added an item called Form to the same solution.In form when i click on a button(let's say Refresh) , for that button click event i want to call a method(.eg.RefreshExcelData) inside ThisAddin.cs file.This method access the values in the form and executes some code.

    namespace ExcelAddIn
    {
        public partial class ThisAddIn
        {
            public void RefreshExcelData()
            {
            }
        }
    }

     Now in MyForm.cs , while trying to create an object for ThisAddin class there is a compilation error that Thisaddin class doesnt have a construcot that takes o arguments.
    private void btnUploadTestCases_Click(object sender, EventArgs e)
    {
        ThisAddIn objrefresh = new ThisAddin();
    }

    I declared a parameterless constructor in the ThisAddin partial class .But it throws error :Microsoft.Office.Tools.AddInBase' does not contain a constructor that takes 0 arguments

    public ThisAddin()

    {

    }

    I tried with the parameter constructor , but the same error.

    One more question i have is how do i access the form fields in the ThisAddin class?


    • Edited by Krrrishna Sunday, August 19, 2012 6:09 AM
    Sunday, August 19, 2012 6:09 AM

Answers

  • either pass thisaddin class instnce to your form (by constructor or property) or make your function on thisaddin a static one.
    Sunday, August 19, 2012 8:04 AM
  • I have created a refddrence to the form in RefresheExcelData() method and able to access its fields.

    Let me know if there is a better approach than this.

    static RefreshExcelData()

    {

    myForm = new TFSForm();
    myForm.Activate();
    myForm.ShowDialog(); 

    strTFSAddress = myForm.Controls["txtTFSSerAddress"].Text;
    strTeamProject = myForm.Controls["txtTestProject"].Text;
    strTestPlan = myForm.Controls["txtTestPlan"].Text;
    strTestSuite = myForm.Controls["txtTestSuite"].Text;

    }

    • Marked as answer by Krrrishna Sunday, August 19, 2012 11:00 AM
    Sunday, August 19, 2012 11:00 AM

All replies

  • either pass thisaddin class instnce to your form (by constructor or property) or make your function on thisaddin a static one.
    Sunday, August 19, 2012 8:04 AM
  • I made the function RefreshExcelData static and able to call it.Now how do i access the fields in the form ?
    Sunday, August 19, 2012 9:53 AM
  • By 'access the fields in the form' do you mean retrieve properties of your form class from your AddIn class? If so, you need to make them public properties, and then you can access them via the variable returned from construction of the form.
    Sunday, August 19, 2012 10:18 AM
  • make your static function accept properties from your form and fill them when invoking RefreshExcelData from inside your form
    Sunday, August 19, 2012 10:53 AM
  • I have created a refddrence to the form in RefresheExcelData() method and able to access its fields.

    Let me know if there is a better approach than this.

    static RefreshExcelData()

    {

    myForm = new TFSForm();
    myForm.Activate();
    myForm.ShowDialog(); 

    strTFSAddress = myForm.Controls["txtTFSSerAddress"].Text;
    strTeamProject = myForm.Controls["txtTestProject"].Text;
    strTestPlan = myForm.Controls["txtTestPlan"].Text;
    strTestSuite = myForm.Controls["txtTestSuite"].Text;

    }

    • Marked as answer by Krrrishna Sunday, August 19, 2012 11:00 AM
    Sunday, August 19, 2012 11:00 AM