SpreadsheetGear 2023
Windows Forms
SpreadsheetGear 2023 > Tutorials > Windows Forms

Follow these steps to use SpreadsheetGear 2023 in a Windows Forms Project

Create a new Windows Application Project

  1. Launch Visual Studio 2012, Visual Studio 2013, Visual Studio 2015, Visual Studio 2017, Visual Studio 2019 or Visual Studio 2022.
  2. On the File menu, point to New and click Project.  The New Project dialog box appears.
  3. Under Project Types, choose Visual C#.
  4. Under Templates, choose Windows Forms Application.
  5. Change the Name and Location as desired.
  6. Click OK to create the project.

Create a Window with a Workbook and a Button

  1. Open the SpreadsheetGear 2023 category in the Visual Studio Toolbox.
  2. Click and drag WorkbookView onto Form1.
  3. Open the Common Controls category in the Toolbox.
  4. Click and drag a Button onto Form1.
  5. Adjust the size of Form1 and the controls as desired.

Add Code to Calculate a Value and Display it in a Cell

  1. Double click the Button to display the Button's Click event handler.
  2. Copy the following code to the Button's Click event handler.

    Simple Windows Forms Sample

    Copy Code
    // Interrupt background calculation if necessary and acquire a lock on the workbook set.
    workbookView1.GetLock();
    try
    {
        // Get a reference to the active cell.
        SpreadsheetGear.IRange activeCell = workbookView1.ActiveCell;
        // Set a formula
        activeCell.Formula = "=24901.55 / PI()";
        // Place the result of the formula in cell B2.
        SpreadsheetGear.IRange b2 = workbookView1.ActiveWorksheet.Cells["B2"];
        b2.Value = "The diameter of the earth is " + activeCell.Text + " miles.";
                    
        // Set the font of B2 to italic.
        b2.Font.Italic = true;
    }
    finally
    {
        // Release the lock on the workbook set.
        workbookView1.ReleaseLock();
    }
    

Build and Run the Application

  1. On the Build menu, click Build Solution.  The solution builds with no errors.
  2. On the Debug menu, click Start Without Debugging.  The application runs in a window and when you click the Button, cell B2 will display "The diameter of the earth is 7926.409546 miles."

Explore SpreadsheetGear 2023

  1. Close the application if you have not already done so.
  2. Select the Form1.cs (Design) tab in Visual Studio.
  3. Right click on the WorkbookView control on Form1 and click Workbook Designer.
  4. Enter values and formulas in cells in the Workbook Designer.
  5. On the Format menu, click Cells... to bring up the Range Explorer which is a modeless dialog used to format cells. Notice that changes made in the Range Explorer are displayed immediately.
  6. Right click on the workbook in the Workbook Designer and click Workbook Explorer. Use the Workbook Explorer to add, remove and save workbooks as well as add, delete, rename, copy and move worksheets. Right click on each element in the tree on the left to see a menu of available actions.
  7. Close the Workbook Designer and notice that any changes you made in the Workbook Designer are reflected in the WorkbookView control in Visual Studio.
  8. From Visual Studio, Press F5 to launch the application. Notice that the changes made in the Workbook Designer are saved with the form and loaded at run time.
  9. Right click on the WorkbookView and click Workbook Designer. The Workbook Designer, Workbook Explorer and Range Explorer are available at runtime to applications which are powered by SpreadsheetGear.