Walkthrough: use Windows Forms to collect data

Source: Internet
Author: User

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
    • Create an Excel Workbook project named "winforminput" and select "create new document" in the Wizard ".

    •  

Add a specified range to sheet1
    1. Select cell A1 in sheet1 ".

    2. 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.

    3. 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
    1. In Solution Explorer, select the winforminput project.

    2. On the project menu, click Add windows form ".

    3. Name the form as getinputstring. CS, and click Add ". The new form is opened in the designer.

    4. Add a textbox and a button to the form.

    5. 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
    1. In Solution Explorer, right-click thisworkbook. CS and click View code ". Thisworkbook, which seems to be an applicationProgramSystem-level management

    2. 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 ();}
    3. 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
    1. In Solution Explorer, right-click "getinputstring" and click "view designer ".

    2. Double-click the button to open the code file. The click event handler of the button is also added.

    3. 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
    1. Press F5 to run the project.

    2. Confirm that the windows form is displayed.

    3. Type "Hello World" in the text box and click "OK ".

    4. Confirm that "Hello world" is displayed in cell "A1" of the worksheet.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.