本文主要是介绍C#上机实验,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace chap4_随堂测试编程
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
button2.Enabled = checkBox1.Checked;//实现checkedbox1按钮可用的时候,勾选注册按钮可用,否则不可用。
//if (checkBox1.Checked == true)
// button2.Enabled = true;
//else
// button2.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)//每隔100秒递增一个值,当达到最大值时,使得timer1不可用,同时将textbox的text添加到listbox的列表中。
{
progressBar1.Value += progressBar1.Step;
if (progressBar1.Value == progressBar1.Maximum)
{
timer1.Enabled = false;
listBox1.Items.Add(textBox1.Text);
}
}
private void button1_Click(object sender, EventArgs e)
{
//openFileDialog1.Filter = "jpg文件|*.jpg|gif文件|*.gif";
//if (openFileDialog1.ShowDialog() == DialogResult.OK)
// textBox3.Text = openFileDialog1.FileName;
openFileDialog1.Filter = "jpg文件|*.jpg|gif文件|*.gif";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
textBox3.Text = openFileDialog1.FileName;
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
}
}
MessageBox.Show()的方法:
运用消息对话框的例子:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 茶品特然
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult dr;//定义对象。
dr = MessageBox.Show("测试一下你的对话框!", "测试测试测试!", MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1);
if(dr==DialogResult.Yes)
MessageBox.Show("你选择的为“是”按钮","系统提示1");
else if(dr==DialogResult.No)
MessageBox.Show("你选择的为“否”按钮","系统提示2");
else if(dr==DialogResult.Cancel)
MessageBox.Show("你选择的为“取消”按钮","系统提示3");
else MessageBox.Show("你没有进行任何操作!","系统提示4");
}
}
}
打开文件对话框:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace 茶品特然
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult dr;//定义对象。
dr = MessageBox.Show("测试一下你的对话框!", "测试测试测试!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
if (dr == DialogResult.Yes)
MessageBox.Show("你选择的为“是”按钮", "系统提示1");
else if (dr == DialogResult.No)
MessageBox.Show("你选择的为“否”按钮", "系统提示2");
else if (dr == DialogResult.Cancel)
MessageBox.Show("你选择的为“取消”按钮", "系统提示3");
else MessageBox.Show("你没有进行任何操作!", "系统提示4");
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "D:\\";
openFileDialog.Filter = "文本文件|*.*|c#文件|*.cs|所有文件|*.*";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if(openFileDialog.ShowDialog()==DialogResult.OK)
{
string fname=openFileDialog.FileName;
//StreamReader sr = File.OpenText(fname);//这里会打开乱码,可能是编码不对。
StreamReader sr = new StreamReader(fname, Encoding.Default);
string str;
while((str=sr.ReadLine())!=null)
{
this.richTextBox1.Text+=str;
}
}
}
}
}
这篇关于C#上机实验的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!