有同事看我打开了VS,说能不能帮忙写个例子,他也要学,入门大家都知道是简单的,语言都是相通的嘛。
好了,下面简单说下:
using System;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;//引用WINDOWS FRAMWORK 库文件
这些都是系统自带的库,好比C语言自带的库。
也可以using其他外来的库,好比C语言的.lib或.a之类。
然后C#中的枚举:
//枚举实列,
public enum BtnNameType
{
按钮A,//
按钮B,
按钮C,
}
这个和C语言的也很类似,今天写的例子有一个点就是程序可扩展性,就是以这个枚举来扩展举例
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
这个是窗体的类,继承于FORM基类,是刚才System.Windows.Forms中的类。
InitializeComponent();是窗体的初始化,有兴趣也可以去研究下,比较简单。
private void checkBox1_CheckedChanged(object sender, EventArgs e)
还有
private void button2_Click(object sender, EventArgs e)
等等都是事件处理,好比C语言的事件或消息或邮箱或者变量标志
private void button3_Click(object sender, EventArgs e)
{
int cCurptr = 0;
for (cCurptr = 0; cCurptr < this.comboBox1.Items.Count; cCurptr++)
{
TreeNode cTreeNode = new TreeNode(this.comboBox1.Items[cCurptr].ToString());
this.treeView1.Nodes.Add(cTreeNode);
}
this.treeView1.Enabled = true;
this.button1.Enabled = true;
}
如上,C#里面也是有FOR循环,基本上和C一样呢
private void BtnNameList_BtnClickEvent(object sender, System.EventArgs e)
{
int cIndex=(e as BtnList.BtnClickEventArgs).GetIndex;
switch ((BtnNameType)cIndex)
{
case BtnNameType.按钮A:
{
MessageBox.Show("我是按钮A");
}break;
case BtnNameType.按钮B:
{
MessageBox.Show("我是按钮B");
} break;
case BtnNameType.按钮C:
{
MessageBox.Show("我是按钮C");
} break;
}
}
这个就是按钮点击事件的集中处理了,相当于有了一个框架,如果要新增一个按钮以及这个按钮的点击处理,那么
就只要两个地方更改,一个是刚才的枚举多加一个,另外就这个地方加个CASE处理
public enum BtnNameType
{
按钮A,//
按钮B,
按钮C,
按钮D,
}
case BtnNameType.按钮C:
{
MessageBox.Show("我是按钮C");
} break;
case BtnNameType.按钮D:
{
MessageBox.Show("我是按钮D");
} break;
所以说,框架和编程的思想也是通用的,一个程序要能够很容易的扩展,降低维护成本
然后整体上就是一些常用控件的简单使用,为了同事更好理解,特地按照显示的箭头操作,前一步操作后才能后一个使能操作
,这样会比较好理解些。
具体的要看代码,先贴上全部代码来:
using System;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;//引用WINDOWS FRAMWORK 库文件
//引用其他外来库
//本工程只演示最常用几个系统组件的用法,深入了解每个组件的用法请阅读官网MDSN类库说明
namespace WindowsFormsApplication1
{
//枚举实列,
public enum BtnNameType
{
按钮A,//
按钮B,
按钮C,
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
this.button5.Enabled = this.checkBox1.Checked;
}
private void button2_Click(object sender, EventArgs e)
{
int cCurptr = 0;
this.comboBox1.Enabled = true;
char[] cText = this.textBox1.Text.ToArray();
for (cCurptr = 0; cCurptr < cText.Length; cCurptr++)
this.comboBox1.Items.Add(cText[cCurptr]);
this.comboBox1.SelectedIndex = 0;
this.button3.Enabled = true;
}
private void button3_Click(object sender, EventArgs e)
{
int cCurptr = 0;
for (cCurptr = 0; cCurptr < this.comboBox1.Items.Count; cCurptr++)
{
TreeNode cTreeNode = new TreeNode(this.comboBox1.Items[cCurptr].ToString());
this.treeView1.Nodes.Add(cTreeNode);
}
this.treeView1.Enabled = true;
this.button1.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
this.buttonList1.BtnClickEvent += new BtnList.ButtonList.BtnClickEventHandler(BtnNameList_BtnClickEvent);
this.buttonList1.GcBtnTxt = Enum.GetNames(typeof(BtnNameType));
this.buttonList1.BackColor = Color.BlueViolet;
}
private void BtnNameList_BtnClickEvent(object sender, System.EventArgs e)
{
int cIndex=(e as BtnList.BtnClickEventArgs).GetIndex;
switch ((BtnNameType)cIndex)
{
case BtnNameType.按钮A:
{
MessageBox.Show("我是按钮A");
}break;
case BtnNameType.按钮B:
{
MessageBox.Show("我是按钮B");
} break;
case BtnNameType.按钮C:
{
MessageBox.Show("我是按钮C");
} break;
}
}
private void button1_Click(object sender, EventArgs e)
{
int cCurptr = 0;
for (cCurptr = 0; cCurptr < this.treeView1.Nodes.Count; cCurptr++)
{
this.textBox2.Text += this.treeView1.Nodes[cCurptr].Text;
}
this.textBox2.Enabled = true;
this.button4.Enabled = true;
}
private void button4_Click(object sender, EventArgs e)
{
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string fileName = this.saveFileDialog1.FileName;
Stream stream = File.OpenWrite(fileName);
StreamWriter writer = new StreamWriter(stream);
writer.Write(this.textBox2.Text);
this.button1.Enabled = false;
this.button2.Enabled = false;
this.button3.Enabled = false;
this.button4.Enabled = false;
this.textBox1.Enabled = false;
this.textBox2.Enabled = false;
this.comboBox1.Enabled = false;
this.treeView1.Enabled = false;
}
}
private void button5_Click(object sender, EventArgs e)
{
this.textBox1.Enabled = true;
this.openFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
this.openFileDialog1.Filter = "文本文件 | *.txt";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fileName = this.openFileDialog1.FileName;
if (fileName!="")
{
string cText;
try
{
StreamReader sr = new StreamReader(fileName);
while ((cText = sr.ReadLine()) != null)
{
this.textBox1.Text += cText;
}
}
catch { }
this.button2.Enabled = true;
}
}
}
}
}
|