C#.Net (New Form) 自製 InputBox ......畫面輸入對話框

  • 5002
  • 0

開始作筆記 最近越來越像魚(只有 7 sec 記憶 ----| 7sec | 忘記| 7sec | 學習| 7sec |忘記) !~

有時候C#內建的對話框 不適合需要,另外建立適合自己的風格對話框,所以就開始作一個

一個主要的Form 另外建議自製的Form (InputBox)當對話框

//======================= Form1.cs ========================================

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {         
        public Form1()
        {
            InitializeComponent();           
        }
          

        private void btnReturn_Click(object sender, EventArgs e)
        {  
            InputBox input = new InputBox();
            //input.ShowDialog(this);            
            DialogResult F2 = input.ShowDialog();            
            if (F2 == DialogResult.OK)
            {
               textBox1.Text =input.GetMsg();
            }
            //else if (F2==DialogResult.Cancel )
            //{
            //    MessageBox.Show("蛋蛋回應 ");
            //}

        }               


        private void btnShow_Click(object sender, EventArgs e)
        {

            if (textBox1.Text == string.Empty)
            {
                MessageBox.Show("沒有字串!~ 建議重新輸入");
                return;
            }

            DialogResult HResult = MessageBox.Show("您要是否要顯示? 請選擇  <是>  還是 <否>  ", "顯示在彈出視窗上面的字", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //MessageBoxButtons和 MessageBoxIcon 有很多種類,可選自己適合需求
            if (HResult == DialogResult.Yes)
            {
                //按了是                
                //錯誤
                //String TmpStr = textBox1.Text;
                //MessageBox.Show("回傳的訊息 : {0} ", textBox1.Text);

                String TmpStr = string.Format("回傳的訊息 {0} .", textBox1.Text);
                MessageBox.Show(TmpStr);
            }
            else if (HResult == DialogResult.No)
            {
                   //按了否
                DialogResult GoBackResult = MessageBox.Show("您要是否要 離開程式?  ", "蛋蛋的哀愁!~葉子離開,是風的追求 ", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (GoBackResult == DialogResult.Yes)
                {
                    //按了是 離開程式
                    Application.Exit();
                }
                else if (GoBackResult == DialogResult.No)
                {
                    //按了否 再次進入輸入文字對話框                  
                    InputBox input = new InputBox();
                    DialogResult F2 = input.ShowDialog();
                    if (F2 == DialogResult.OK)
                    {
                        textBox1.Text = input.GetMsg();
                    }
                }
            }           
         
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();            
        }
    }
}

//======================= InputBox.cs =========

namespace WindowsFormsApplication1
{
    public partial class InputBox : Form
    {
        public InputBox()
        {
            InitializeComponent();
            btnOk.DialogResult = System.Windows.Forms.DialogResult.OK;
            //btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;    
        }

        private string Msg;       
        public string GetMsg()
        {
            return Msg;
        }
        private void btnOk_Click(object sender, EventArgs e)
        {

            if (textBox1.Text == string.Empty)
            {
                MessageBox.Show("沒有字串!~ 建議重新輸入");  
            }
            else
            {
                Msg = textBox1.Text;
            }
            


        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}

開始執行 F5 ,

當發生編譯錯誤

1.確認是否打錯字元. EX :  " ; " 常常錯誤打成 " : "

2.注意字母大小寫. 善用內建關鍵字自動辨識功能.

3.如果學過很多種軟體語言,語法有許多差異性,小心原先習慣讓你變成bug抓抓不完.. @_@!~

 

 

有事沒事多學,學習活在當下,做一件事情後,學會一件事就好。 JT