All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
C#教程:C#调用动态链接库

Writer: qq Article type: Programming skills(编程技巧) Time: 2014/7/11 9:22:00 Browse times: 299 Comment times: 0

C#教程:C#调用动态链接库


Head photo

Go homepage
Upload pictures
Write articles

C#教程:C#调用动态链接库

C#调用动态链接库
下面主要通过一个示例来讲解如何调用动态链接库。
示例
通过调用类库实现简单计算器程序
通过引用类库,使用类库中的相关方法开发一个简单的计算器,运行效果如图1所示。

图1 简单计算器
(1)在菜单栏中选择“项目”/“添加引用”,弹出“添加引用”对话框,在“添加引用”对话框中选择“浏览”选项卡。
(2)通过浏览找到上一节中创建的DLL类库,单击【确定】按钮,将类库添加到项目中,如图2和图3所示。

图2 引用类库

图3 添加DLL类库成功
类库添加成功后,在程序中就可以直接使用了。示例完整代码如下所示。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using _4_01;
namespace _4_02
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Operation oper = new Operation();
private void button1_Click(object sender, EventArgs e)
{
switch (comboBox1.Text)
{
case "+":
textBox3.Text = oper.Add(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2. ext)). oString();
break;
case "-":
textBox3.Text = oper.Minus(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text)). oString();
break;
case "*":
textBox3.Text = oper.Multiplication(Convert.ToDouble(textBox1.Text), Convert.ToDouble (textBox2.Text)).
ToString();
break;
case "/":
textBox3.Text = oper.Division(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text)).
ToString();
break;
}
}
}
}
完整程序代码如下:
★ ★★★★Form1.cs窗体代码文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using _4_01;
namespace _4_02
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Operation oper = new Operation();
private void button1_Click(object sender, EventArgs e)
{
switch (comboBox1.Text)
{
case "+":
textBox3.Text = oper.Add(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text)).ToString();
break;
case "-":
textBox3.Text = oper.Minus(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text)).ToString();
break;
case "*":
textBox3.Text = oper.Multiplication(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text)).ToString();
break;
case "/":
textBox3.Text = oper.Division(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text)).ToString();
break;
}
}
}
}
★ ★★★★Form1.designer.cs窗体设计文件完整程序代码★★★★★
namespace _4_02
{
partial class Form1
{
///


/// 必需的设计器变量。
///

private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///

/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///

private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();




There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.