Dialog Input

Dialog Box から Text(検索キー, 置換文字列) を入力します。

前田稔の超初心者のプログラム入門

Search Menu の説明

  1. 検索メニューを実装するとき Source(検索文字列), Destinate(置き換え文字列)のタイプ入力が問題です。
    DialogBox(Form2)を使ってタイプ入力する方法が本筋なので、その方法を説明します。
    (DialogBox の基本は Dialog Box 及び DialogText を参照して下さい)
  2. DialogBox を使って検索キーをタイプ入力する Dialog Class と検索キーと置換キーをタイプ入力する RepDialog Class です。
    Dialog.cs の名前でタイプしてプロジェクトに組み込んで下さい。
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.IO;            // for File, StreamReader
    using System.Text;          // for Encoding
    
        //★ Dialog Class
        class MyDialog : Form
        {
            public TextBox txtBox;
            public string str;         //取得KeyWord
    
            public MyDialog()
            {
                Text = "Dialog Text Input";
                MaximizeBox = false;
                MinimizeBox = false;
                ControlBox = false;
                ShowInTaskbar = false;
                FormBorderStyle = FormBorderStyle.FixedDialog;
    
                Width = 250;
                Height = 130;
    
                Button btnOK = new Button();
                btnOK.Text = "OK";
                btnOK.Location = new Point(30, ClientSize.Height - btnOK.Height - 5);
                btnOK.Parent = this;
                btnOK.TabIndex = 1;
                btnOK.Click += new EventHandler(btnOK_Click);
                btnOK.DialogResult = DialogResult.OK;
    
                Button btnCancel = new Button();
                btnCancel.Text = "Cancel";
                btnCancel.Location = new Point(ClientSize.Width - btnCancel.Width - 30,
                    ClientSize.Height - btnCancel.Height - 5);
                btnCancel.Parent = this;
                btnCancel.TabIndex = 2;
                btnCancel.DialogResult = DialogResult.Cancel;
    
                txtBox = new TextBox();
                txtBox.Parent = this;
                txtBox.Location = new Point(10, 10);
                txtBox.Width = ClientSize.Width - 20;
                txtBox.TabIndex = 0;
            }
    
            void btnOK_Click(object sender, EventArgs e)
            {   str = txtBox.Text;  }
        }
        
        class RepDialog : Form
        {
            public TextBox k_Box;
            public TextBox r_Box;
            public string k_str;         //KeyWord
            public string r_str;         //Replace
    
            public RepDialog()
            {
                Text = "Dialog Text Input";
                MaximizeBox = false;
                MinimizeBox = false;
                ControlBox = false;
                ShowInTaskbar = false;
                FormBorderStyle = FormBorderStyle.FixedDialog;
    
                Width = 250;
                Height = 130;
    
                Button btnOK = new Button();
                btnOK.Text = "OK";
                btnOK.Location = new Point(30, ClientSize.Height - btnOK.Height - 5);
                btnOK.Parent = this;
                btnOK.TabIndex = 2;
                btnOK.Click += new EventHandler(btnOK_Click);
                btnOK.DialogResult = DialogResult.OK;
    
                Button btnCancel = new Button();
                btnCancel.Text = "Cancel";
                btnCancel.Location = new Point(ClientSize.Width - btnCancel.Width - 30,
                    ClientSize.Height - btnCancel.Height - 5);
                btnCancel.Parent = this;
                btnCancel.TabIndex = 3;
                btnCancel.DialogResult = DialogResult.Cancel;
    
                k_Box = new TextBox();
                k_Box.Parent = this;
                k_Box.Location = new Point(10, 10);
                k_Box.Width = ClientSize.Width - 20;
                k_Box.TabIndex = 0;
                r_Box = new TextBox();
                r_Box.Parent = this;
                r_Box.Location = new Point(10, 36);
                r_Box.Width = ClientSize.Width - 20;
                r_Box.TabIndex = 1;
            }
    
            void btnOK_Click(object sender, EventArgs e)
            {
                k_str = k_Box.Text;
                r_str = r_Box.Text;
            }
        }
    
  3. Text 領域を検索キーで調べる Search() 関数は次のようになります。
    MyDialog() からタイプ入力した Text は Mydlg.str から取得します。
    SearchMenu(1) の 1 は Text 本文の検索です。
        private void Search(object sender, EventArgs e)
        {   CheckText();
            MyDialog Mydlg = new MyDialog();
            Mydlg.txtBox.Text = m_sou;
            DialogResult dr = Mydlg.ShowDialog();
            if (dr == DialogResult.OK)
            {   m_sou = Mydlg.str;
                SearchMenu(1);
            }
        }
    
  4. Text 領域の検索キーを置換キーで置き換える Replace 関数は次のようになります。
    RepDialog() からタイプ入力した Text は Repdlg.k_str と Repdlg.r_str から取得します。
    SearchMenu(2) の 2 は Text 本文の置き換えです。
        private void Replace(object sender, EventArgs e)
        {   CheckText();
            RepDialog Repdlg = new RepDialog();
            Repdlg.k_Box.Text = m_sou;
            DialogResult dr = Repdlg.ShowDialog();
            if (dr == DialogResult.OK)
            {   m_sou = Repdlg.k_str;
                m_des = Repdlg.r_str;
                SearchMenu(2);
            }
        }
    
  5. Dialog Box の使用により button1, textBox1, textBox2 は不要になるので削除して下さい。
    また Hide() や Show() で表示・非表示を切り替えていた処理も不要になります。
    検索キーや置換キーは、メニューが選択されたときにタイプ入力するので事前にキーをタイプする「検索/キー」メニューも不要になるので削除して下さい。
  6. パスワードを入力する PassWord() 関数です。
    m_et[] が Encode 領域で、shuffle() 関数で設定します。
    m_dt[] が Decode 領域です。
        byte[]      m_et = new byte[208];       // Encode Table 
        byte[]      m_dt = new byte[208];       // Decode Table
    
        private void PassWord(object sender, EventArgs e)
        {   uint wk = 0;
            int  i;
            MyDialog Mydlg = new MyDialog();
            Mydlg.txtBox.Text = "PASS WORD";
            DialogResult dr = Mydlg.ShowDialog();
            if (dr == DialogResult.OK)
            {   m_pass = Mydlg.str;
                for(i = 0; i < m_pass.Length; i++) wk += m_sou[i];
                shuffle(wk);
                for(i = 0; i < 208; i++) m_dt[m_et[i] - 48] = (byte)(i + 48);
                m_pass = "Decode";
            }
        }
    

超初心者のプログラム入門(C# Frame Work)