none
テキストボックス全選択理由について RRS feed

  • 質問

  • お世話になります。

    VS2010,C#,.Netframework4.0でフォーム内にテキストボックスとボタンを配置して、

    ボタンクリック時にボタンEnabledプロパティ操作とテキストボックスのテキストを設定すると

    2回目以降のボタンクリックでテキストボックスのテキストが全選択状態となります。

    ボタンプのロパティ操作を行わない場合やテキストボックスのSelectionStartプロパティを設定すると

    全選択状態は回避できるのですが、なぜ全選択になるのか理由が分かりません。

    どなたか、ご説明頂けないでしょうか?

    namespace FormTextbox
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                textBox1.Text = "123";
                textBox1.SelectionStart = textBox1.SelectionStart;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                button1.Enabled = false;
                textBox1.Text = "123";
                //textBox1.SelectionStart = textBox1.SelectionStart;
                button1.Enabled = true;
            }
        }
    }

    2015年9月17日 1:48

回答

すべての返信

  • TextBox.Textプロパティに値を設定するとselectionSetフィールドがfalseに設定されるからです。

    /// <devdoc>
    ///     True if the selection has been set by the user.  If the selection has
    ///     never been set and we get focus, we focus all the text in the control
    ///     so we mimic the Windows dialog manager.
    /// </devdoc>
    private bool selectionSet = false;

    • 回答としてマーク r8c38 2015年9月17日 2:39
    2015年9月17日 2:01
  • 佐祐理 様

    なるほどTextプロパティでselectionSetプロパティが設定されるのですね。

    またSelectionStartプロパティにて、selectionSet=trueが設定される為、

    全選択がされないということも分かりました。

    button Enabledプロパティ操作で状況が変化する理由は分かりませんが、

    今回の質問からは脱線するので、自分で調べてみます。

    ご回答、どうも有難うございました。

    2015年9月17日 2:39