The Visual Studio about C#

 

C#关于BackgroundWorker组件的简单应用

public partial class frmBackgroundWorker : Form

{

public frmBackgroundWorker()

{

InitializeComponent();

}

private int numberToCompute = 0;

private int highestPercentageReached = 0;

// 操作开在另一个线程上运行事件处理和序

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

{

e.Result = ComputeFibonacci((int)e.Argument, this.backgroundWorker1, e);

}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)

{

this.progressBar1.Value = e.ProgressPercentage;

}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

{

if (e.Error != null)

{

MessageBox.Show(e.Error.Message);

}

else if (e.Cancelled)

{

resultLabel.Text = "Canceled";

}

else

{

resultLabel.Text = e.Result.ToString();

}

this.numericUpDown1.Enabled = true;

startAsyncButton.Enabled = true;

cancelAsyncButton.Enabled = false;

}

private void startAsyncButton_Click(object sender, EventArgs e)

{

resultLabel.Text = String.Empty;

this.numericUpDown1.Enabled = false;

this.startAsyncButton.Enabled = false;

this.cancelAsyncButton.Enabled = true;

numberToCompute = (int)numericUpDown1.Value;

highestPercentageReached = 0;

backgroundWorker1.RunWorkerAsync(numberToCompute);

}

long ComputeFibonacci(int n, BackgroundWorker worker, DoWorkEventArgs e)

{

if ((n < 0) || (n > 91))

{

throw new ArgumentException(

"value must be >= 0 and <= 91", "n");

}

long result = 0;

if (worker.CancellationPending)

{

e.Cancel = true;

}

else

{

if (n < 2)

{

result = 1;

}

else

{

result = ComputeFibonacci(n - 1, worker, e) +

ComputeFibonacci(n - 2, worker, e);

}

int percentComplete =

(int)((float)n / (float)numberToCompute * 100);

if (percentComplete > highestPercentageReached)

{

highestPercentageReached = percentComplete;

worker.ReportProgress(percentComplete);

}

}

return result;

}

private void cancelAsyncButton_Click(object sender, EventArgs e)

{

this.backgroundWorker1.CancelAsync();

cancelAsyncButton.Enabled = false;

}

}

C#关于ErrorProvider组件的简单应用

需要引用两个ErrorProvider控件

string strA = null;

string strB = null;

private void txtPasword_Validating(object sender, CancelEventArgs e)

{

if (txtPasword.Text != "mrsoft")//判断输入是否正确

{

errPassword.SetError(txtPasword, "密码确误");

}

else

{

errPassword.SetError(txtPasword, "");

strA = txtPasword.Text;

}

}

private void txtUser_Validating(object sender, CancelEventArgs e)

{

if (txtUser.Text != "mr")

{

errUser.SetError(txtUser, "登录名错误");

}

else

{

errUser.SetError(txtUser, "");

strB = txtUser.Text;

}

}

private void btnOK_Click(object sender, EventArgs e)

{

if (strB != null && strA != null)

{

MessageBox.Show("登录成功");

}

else

{

MessageBox.Show("输入用户名和密码");

}

}

C#关于Eventlog组件的简单应用

private void Form1_Load(object sender, EventArgs e)

{

//检查事件源是否存在,如果不存在将注册事件源

if (System.Diagnostics.EventLog.SourceExists("ZhyScoure"))

{

System.Diagnostics.EventLog.DeleteEventSource("ZhyScoure");//注册事件源

}

//为NewLog1日志名称注册事件源

System.Diagnostics.EventLog.CreateEventSource("ZhyScoure", "NewLog1");

eventLog1.Log = "NewLog1";//NewLog1日志,

eventLog1.Source = "ZhyScoure";//事件源名

this.eventLog1.MachineName = ".";//表示本机

// this.eventLog1.Clear();

}

//写入日志

private void button1_Click(object sender, EventArgs e)

{

if (System.Diagnostics.EventLog.Exists("NewLog1"))

{

if (textBox1.Text != "")

{

eventLog1.WriteEntry(textBox1.Text.ToString());

MessageBox.Show("日志写成功");

textBox1.Text = "";

}

else

{

MessageBox.Show("日志内容不能为空");

}

}

else

{

MessageBox.Show("日志不存在");

}

}

private void button2_Click(object sender, EventArgs e)

{

listBox1.Items.Clear();

if (eventLog1.Entries.Count > 0)

{

foreach (System.Diagnostics.EventLogEntry entry

in eventLog1.Entries)

{

listBox1.Items.Add(entry.Message);

}

}

else

{

MessageBox.Show("日志中没有记录.");

}

}

C#关于Eventlog组件的简单应用

private void Form1_Load(object sender, EventArgs e)

{

//事件源,用于存储系系的错误日志

if (System.Diagnostics.EventLog.SourceExists("ErrEventLog"))

{

System.Diagnostics.EventLog.DeleteEventSource("ErrEventLog");

}

//事件类型为Application创建事件源

System.Diagnostics.EventLog.CreateEventSource("ErrEventLog", "Application");

eventLog2.Log = "Application";

eventLog2.Source = "ErrEventLog";

this.eventLog1.MachineName = ".";

eventLog2.Clear();//清除ErrEventLog事件源的日志信息

}

private void button1_Click(object sender, EventArgs e)

{

//查找系统日志

if (eventLog1.Entries.Count > 0)

{

foreach (System.Diagnostics.EventLogEntry entry in eventLog1.Entries)

{

if (entry.EntryType == System.Diagnostics.EventLogEntryType.Error)

{

//MessageBox.Show(entry.Message);

listBox1.Items.Add(entry.Message);                       eventLog2.WriteEntry(entry.Message,System.Diagnostics.EventLogEntryType.Error);

}

}

}

else

{

MessageBox.Show("系系没有错误日志.");

}

}

C#关于Eventlog组件的简单应用

public int intCount=0;

private void Form1_Load(object sender, EventArgs e)

{

if (eventLog1.Entries.Count > 0)

{

foreach (System.Diagnostics.EventLogEntry entry in eventLog1.Entries)

{

if (comboBox1.Items.Count == 0)

{

comboBox1.Items.Add(entry.Source.ToString());//

}// end if

else

{

if(entry.Source.ToString() != comboBox1.Items[intCount].ToString())

  comboBox1.Items.Add(entry.Source.ToString());

intCount++;

}

}// end if

}// from

}// end froeach

}//

private void button1_Click(object sender, EventArgs e)

{

if (comboBox1.SelectedItem == null)

{

MessageBox.Show("请选择日志名称");

return;

}

if (textBox1.Text == "")

{

MessageBox.Show("请填写日志内容");

textBox1.Focus();

return;

}

eventLog1.Log = "System";

eventLog1.Source=comboBox1.SelectedItem.ToString();

eventLog1.MachineName = ".";

eventLog1.WriteEntry(textBox1.Text);

MessageBox.Show("添加成功");

if (eventLog1.Entries.Count > 0)

{

foreach (System.Diagnostics.EventLogEntry entry in eventLog1.Entries)

{

listView1.Items.Add(entry.Message);

}// from

}// end froeach

}// end

C#关于FileSystemWatcher组件监视系统日志的应用

private void Form1_Load(object sender, EventArgs e)

{

textBox1.Text = Environment.SystemDirectory + "//config";

textBox1.Enabled = false;

}

private void button1_Click(object sender, EventArgs e)

{

fileSystemWatcher1.Path = textBox1.Text;

//提示对于此示例,您可以使用本地计算机上所希望的任何目录。

this.fileSystemWatcher1.Filter = "*.Evt";//此属获取或设置筛选字符串,用于确定在目录中监视哪些文件。

this.fileSystemWatcher1.EndInit();

}

//创建文件是发生

private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)

{

listBox1.Items.Add("日志文件:" + e.FullPath+"被创建");

}

private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)

{

listBox1.Items.Add("日志文件:" + e.FullPath + "被更改");

}

private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)

{

listBox1.Items.Add("日志文件:" + e.FullPath + "被册除");

}

C#关于HelpProvider组件调用帮助文件的应用

private void Form1_Load(object sender, EventArgs e)

{

string strpath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("//")).LastIndexOf("//"));

strpath += @"/mrHelp.chm";

helpProvider1.HelpNamespace=strpath;

}

C#关于Process组件的简单应用

private void Form1_Load(object sender, EventArgs e)

{

process1.StartInfo.FileName = "notepad.exe";

}

private void button1_Click(object sender, EventArgs e)

{

process1.Start();

}

private void button2_Click(object sender, EventArgs e)

{

System.Diagnostics.Process[] myProcesses;

myProcesses =  System.Diagnostics.Process.GetProcessesByName("Notepad");

foreach (System.Diagnostics.Process instance in myProcesses)

{

instance.CloseMainWindow();

instance.WaitForExit(3000);

instance.Close();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值