官术网_书友最值得收藏!

How to do it...

  1. Open Visual Studio 2017.
  2. Now open the solution from the previous recipe. Click File | Open | Project/Solution, or press Ctrl + Shift + O, and select the Chapter1.Library solution. 
  3. Now click on the Chapter1.Library solution label. Click File | Add | New Project....
  4. In the Add New Project template dialog box, expand the Visual C# node in the left-hand pane. 
  5. Select Windows Classic Desktop and select Windows Forms App (.NET Framework) in the right template pane:
  1. Now, in the Name: textbox, type a name for the new project. Let's type Chapter1.Library.HelloWindowsForms and leave the Location: textbox as it is and the defaults as well. Click OK to create the new project.
  1. The new project will be added to the Solution Explorer and it should look like this: 
  1. Now let's do some cleaning of the names. Change Form1.cs to MainForm.cs. Remember, giving a meaningful name to your files is very important and a very good practice. 
  2. Select the Form in the MainForm [Design] tab and go to the Properties window (or press F4). Now change the Text property to Hello World
  1. Let's add some UI components to the form. Go to the tool box window (or press Ctrl + Alt + X ) and drag and drop a Label, a TextBox, and a Button to the form. Arrange them as per the following screenshot: 
  1. Let's change some properties of the components we just dropped on the form. Go to the Properties window and change the defaults to the following:

After the changes, the Windows form designer should look like this: 

  1. Let's add our library to the Windows Forms project. To do this, expand References under the Chaper1.Library.HelloWindowsForms project. Right-click on the References label and select Add Reference....
  2. Under the Reference Manager dialog box, click on the Projects label in the left-hand pane. In the middle pane, check the Chapter1.Library.HelloLib project: 
  1. Click OK.
  2. Now double-click on the Say Hello button to open the code window. 
  3. In the code window, scroll to the top and type the following code, at the end of  the very last line of using directive:
      using Chapter1.Library.HelloLib;
  1. Now scroll down to the HelloButton_Click method. In between the curly brackets, type the following code: 
      var helloMessage = new HelloWorld();
var yourName = NameTextBox.Text;
MessageBox.Show(helloMessage.SayHello(yourName));
  1. Time to test our classic Windows application with the class library created in the previous recipe. Hit F5 to debug the code. Now you should see the Windows form created. 
  1. Type your name in the text box and hit the Say Hello button. A message box will appear with a message from the class library: 
  1. Congratulations!!! You have just used a class library from a classic Windows application. 
主站蜘蛛池模板: 宁波市| 宁都县| 永春县| 延边| 南陵县| 五莲县| 甘谷县| 宁河县| 江都市| 阳山县| 米脂县| 马山县| 怀远县| 赤峰市| 博乐市| 西乡县| 揭阳市| 芜湖县| 策勒县| 公安县| 休宁县| 财经| 沙坪坝区| 滨州市| 克什克腾旗| 克什克腾旗| 庆元县| 邢台市| 尼勒克县| 治县。| 阳城县| 高平市| 盐山县| 兴山县| 贡山| 浮梁县| 榆中县| 孙吴县| 德兴市| 万源市| 天峻县|