Follow these steps to use SpreadsheetGear 2023 in a Windows Forms Project
Create a new Windows Application Project
-
Launch Visual Studio 2013, Visual Studio 2015, Visual Studio 2017, Visual Studio 2019 or Visual Studio 2022.
-
On the File menu, point to New and click Project. The New Project dialog box appears.
- Under Project Types, choose Visual C#.
- Under Templates, choose Windows Forms Application.
- Change the Name and Location as desired.
- Click OK to create the project.
Create a Window with a Workbook and a Button
- Open the SpreadsheetGear 2023 category in the Visual Studio Toolbox.
- Click and drag WorkbookView onto Form1.
- Open the Common Controls category in the Toolbox.
- Click and drag a Button onto Form1.
- Adjust the size of Form1 and the controls as desired.
Add Code to Calculate a Value and Display it in a Cell
- Double click the Button to display the Button's Click event handler.
- 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
- On the Build menu, click Build Solution. The solution builds with no errors.
- 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
- Close the application if you have not already done so.
- Select the Form1.cs (Design) tab in Visual Studio.
- Right click on the WorkbookView control on Form1 and click Workbook Designer.
- Enter values and formulas in cells in the Workbook Designer.
- 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.
- 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.
- Close the Workbook Designer and notice that any changes you made in the Workbook Designer are reflected in the WorkbookView control in Visual Studio.
- 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.
- 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.