Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)qq
published in(发表于) 2014/7/11 9:22:00
C#教程:C#调用动态链接库

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();


If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759