標題: 用C語言的風格寫一個C#控件使用例子 [打印本頁]

作者: wtf3505    時間: 2019-3-15 23:28
標題: 用C語言的風格寫一個C#控件使用例子

有同事看我打開了VS,說能不能幫忙寫個例子,他也要學,入門大家都知道是簡單的,語言都是相通的嘛。

好了,下面簡單說下:
using System;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;//引用WINDOWS FRAMWORK 庫文件
這些都是系統(tǒng)自帶的庫,好比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循環(huán),基本上和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 庫文件
//引用其他外來庫
//本工程只演示最常用幾個系統(tǒng)組件的用法,深入了解每個組件的用法請閱讀官網(wǎng)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;
                }
            }
        }
    }
}





WindowsFormsApplication1.7z

51.11 KB, 下載次數(shù): 10, 下載積分: 黑幣 -5






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1