天天看點

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;
                }

        }
    }
    }
}