程序员的知识教程库

网站首页 > 教程分享 正文

Winform开发——C SHARP 基础(其它常用方法)

henian88 2024-10-23 10:50:54 教程分享 10 ℃ 0 评论

时间处理

  1. 间隔计算

DateTime oldDate = new DateTime(2002,7,15);

DateTime newDate = DateTime.Now;

TimeSpan ts = newDate - oldDate;

int differenceInDays = ts.Days;

Console.WriteLine("Difference in days: {0} ", differenceInDays);

注意,TimeSpan是系统基础类。

利用时间判断

public static bool JM(DateTime T0,int ii)

{

bool Bl=false;

DateTime T1 = DateTime.Now;

TimeSpan ts = T1-T0;

if (ts.Days>ii)

Bl=true;

return Bl;

}

DateTime oldDate = new DateTime(2007,1,29); //起始时间

if(Common1.JM(oldDate,100)) //有效期100天

{

MessageBox.Show("试用版到期,请和北京派得伟业公司联系,电话010-51503625");

Application.Exit();

}

2.Timer类

以下程序在每隔2秒显示一个MessageBox提示。其中TimerEventProcessor方法在timer开始后就自动运行。

static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();

static int alarmCounter = 1;

static bool exitFlag = false;

private static void TimerEventProcessor(Object myObject,EventArgs myEventArgs)

{

myTimer.Stop();

if(MessageBox.Show("继续?","计数:"+alarmCounter,MessageBoxButtons.YesNo)==DialogResult.Yes)

{

alarmCounter +=1;

myTimer.Enabled = true;

}

else

exitFlag = true;

}

private void button1_Click(object sender, System.EventArgs e)

{

myTimer.Tick += new EventHandler(TimerEventProcessor);

myTimer.Interval = 2000;

myTimer.Start();

while(exitFlag == false)

{

// Processes all the events in the queue.

Application.DoEvents();

}

}

3.延时程序

以下为延时程序,单位为毫秒。由于ts.Milliseconds数值在大于1000时出错,所以要转换成ts.Seconds。

public void TimeDelay(int it)

{

bool Bl=true;

DateTime T1,T0;

TimeSpan ts;

int it0;

T0 = DateTime.Now;

while(Bl)

{

T1 = DateTime.Now;

ts=T1-T0;

if (it>1000)

{

it0=Convert.ToInt32(it/1000);

if (ts.Seconds>it0)

Bl=false;

}

else

if (ts.Milliseconds>it)

Bl=false;

Application.DoEvents();

}

}

private void button1_Click(object sender, System.EventArgs e)

{

MessageBox.Show("开始");

TimeDelay(2900);

MessageBox.Show("时间到。");

}

注意:在延时程序没有完成前,不能退出。

dll文件调用

  1. 基本概念

动态链接库 (DLL) 在运行时链接到程序。本程序通过两个cs文件生成一个MyLibrary.DLL的库文件。

Add.cs:为源文件,其中包含 Add(long i, long j) 方法。该方法返回参数之和。包含 Add 方法的 AddClass 类是命名空间 MyMethods 的成员。

Mult.cs:为源文件,其中包含 Multiply(long x, long y) 方法。该方法返回参数之积。包含 Multiply 方法的 MultiplyClass 类也是命名空间 MyMethods 的成员。

MyClient.cs:包含 Main 方法的文件。它使用 DLL 文件中的方法来计算运行时参数的和与积。

源程序

文件:Add.cs

// Add two numbers

using System;

namespace MyMethods

{

public class AddClass

{

public static long Add(long i, long j)

{

return(i+j);

}

}

}

文件:Mult.cs

// Multiply two numbers

using System;

namespace MyMethods

{

public class MultiplyClass

{

public static long Multiply(long x, long y)

{

return (x*y);

}

}

}

文件:MyClient.cs

// Calling methods from a DLL file

using System;

using MyMethods;

class MyClient

{

public static void Main(string[] args)

{

Console.WriteLine("Calling methods from MyLibrary.DLL:");

if (args.Length != 2)

{

Console.WriteLine("Usage: MyClient <num1> <num2>");

return;

}

long num1 = long.Parse(args[0]);

long num2 = long.Parse(args[1]);

long sum = AddClass.Add(num1, num2);

long product = MultiplyClass.Multiply(num1, num2);

Console.WriteLine("The sum of {0} and {1} is {2}",

num1, num2, sum);

Console.WriteLine("The product of {0} and {1} is {2}",

num1, num2, product);

}

}

此文件包含使用 DLL 方法 Add 和 Multiply 的算法。它首先分析从命令行输入的参数 num1 和 num2。然后使用 AddClass 类中的 Add 方法计算和,使用 MultiplyClass 类中的 Multiply 方法计算积。

请注意,文件开头的 using 指令使您得以在编译时使用未限定的类名来引用 DLL 方法,例如:

MultiplyClass.Multiply(num1, num2);

否则,必须使用完全限定名,例如:

MyMethods.MultiplyClass.Multiply(num1, num2);

2.编译

若要生成文件 MyLibrary.DLL,使用以下命令行编译文件 Add.cs 和文件 Mult.cs:

csc /target:library /out:MyLibrary.DLL Add.cs Mult.cs

如果对于一个文件以及缺省dll文件名,可以用:

csc /target:library Mult.cs

这样生成一个Mult.dll的文件。

若要生成可执行文件 MyClient.exe,请使用以下命令行:csc /out:MyClient.exe /reference:MyLibrary.DLL MyClient.cs

使用命令行编译文件,在【开始】/【程序】/【Microsoft Visual Studio .NET 2003】/【Visual Studio .NET工具】/【Visual Studio .NET命令提示】。

默认目录为当前用户目录。如果你使用Administrator,则目录为C:\Documents and Settings\Administrator。你可以把几个cs文件拷贝到这个目录中。

3.执行

若要运行程序,请输入 EXE 文件的名称,文件名的后面跟两个数字,例如:

MyClient 1234 5678

运行结果为:

Calling methods from MyLibrary.DLL:

The sum of 1234 and 5678 is 6912

The product of 1234 and 5678 is 7006652

自动编译dll

建立一个bat文件如下:

target:library contour01.cs

就可以自动把contour01.cs编译成contour01.dll了。

在IDE中调用dll

把以上的文件编译成MyLibrary.DLL后,在一个控制台应用程序中使用MyClient.cs。先引用MyLibrary.DLL,然后修改程序为:

using System;

using MyMethods;

class MyClient

{

public static void Main(string[] args)

{

Console.WriteLine("Calling methods from MyLibrary.DLL:");

long num1 = 100;

long num2 = 550;

long sum = AddClass.Add(num1, num2);

long product = MultiplyClass.Multiply(num1, num2);

Console.WriteLine("The sum of {0} and {1} is {2}",

num1, num2, sum);

Console.WriteLine("The product of {0} and {1} is {2}",

num1, num2, product);

}

}

运行结果和以前一样。

在C#中调用dll

1.编译dll文件。

2.把dll文件拷贝到工作文件夹下面。

3.在右边文件夹的【引用】上右击,选择【添加引用】,在【com】中浏览选择,即可添加。也可以在菜单【项目】/【添加引用】上添加。

4.如果需要修改,需要把目录下的dll文件删除,再次生成。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表