This walkthrough shows how to open a Windows form from a Microsoft Office Excel document-level custom, collect information from users, and write the information to worksheet cells.
The first step is to create an Excel Workbook project.
Create a new project
Add a specified range to sheet1
Select cell A1 in sheet1 ".
In the Name box, type "forminput ".
The "Name box" is located on the left side of the editing bar, above the "A" column of the worksheet.
Press enter.
The namedrange control is added to cell A1. After "A1" is selected, no obvious indication is displayed in the worksheet, but "forminput" is displayed in the "name" box (just above the left worksheet) and "properties" window. The namerange control is actually a name for a cell. When we modify the default name of the cell, the system automatically adds a control named "namerange" to the cell, in a custom windows form, we can use globals. sheet1.forminput (glotify. the name of the workbook. custom cell name) to use the namerange control, and can use the namerange method attributes, such as "globals. sheet1.forminput. value2"
Add windows forms to the Project
Create a Windows form that prompts you to enter information.
Add Windows Forms
In Solution Explorer, select the winforminput project.
On the project menu, click Add windows form ".
Name the form as getinputstring. CS, and click Add ". The new form is opened in the designer.
Add a textbox and a button to the form.
Select this button, find the "text" attribute in the "properties" window, and change the text to "OK ".
Next, add thisworkbook. CS to collect user informationCode.
Create and display an instance of getinputstring windows form, and then write user information to a cell in the worksheet.
Display the form and collect information
In Solution Explorer, right-click thisworkbook. CS and click View code ". Thisworkbook, which seems to be an applicationProgramSystem-level management
In the thisworkbook open event handler, add the following code to declare a variable in the getinputstring form, and then display the form.
Note:
In C #, you must add an event handler, as shown in the following startup event.
Private void thisworkbook_startup (Object sender, system. eventargs e) {this. Open + = new Microsoft. Office. InterOP. Excel. workbookevents_openeventhandler (thisworkbook_open );}
Register an event and it runs automatically when the Excel document is opened. Private void thisworkbook_open () {getinputstring inputform = new getinputstring (); inputform. Show ();}
Create a method named writestringtocell to write the text to a specified range. This method will be called from the form, and user input will be passed to the namedrange control forminput on cell "A1.
Public void writestringtocell (string formdata) {globals. sheet1.forminput. value2 = formdata ;}
Globle. Workbook name. Custom cell name. namerange Control Properties
Next, add the Event code for processing the button to the form.
Send information to the worksheet
In Solution Explorer, right-click "getinputstring" and click "view designer ".
Double-click the button to open the code file. The click event handler of the button is also added.
Add code to the event handler to extract the input content from the text box, send it to the writestringtocell function, and close the form.
Globals. thisworkbook. writestringtocell (this. textbox1.text); this. Dispose ();
Test workbook
Press F5 to run the project.
Confirm that the windows form is displayed.
Type "Hello World" in the text box and click "OK ".
Confirm that "Hello world" is displayed in cell "A1" of the worksheet.