【问题标题】:In multiline textbox, my cursor automatically goes to next line在多行文本框中,我的光标自动转到下一行
【发布时间】:2015-03-13 10:47:38
【问题描述】:

我正在使用 winForms 制作一个信使应用程序。因此,当我在发件人的文本框中键入一条消息并按 Enter 键时,消息会被传递,但我的文本框上的光标第一次会自动转到下一行。然后对于我输入和发送的所有消息,它都会保留在那里。我正在为使用返回的输入按钮使用简单的按键事件。问题可能是什么?代码如下:

     private void txtChatOperatorMsg_KeyPress (object sender, KeyPressEventArgs e) {

         if (e.KeyChar == (char) Keys.Return) {
             pushToClientAndEmpty (txtChatOperatorMsg.Text);
         }

     }

     private void pushToClientAndEmpty (string message) {
         operatorgetset = message;
         Thread operatorChatThread = new Thread (new ThreadStart (newThreadOperatorChat));
         operatorChatThread.Start ();             
         txtChatOperatorMsg.Clear();
                  }

     string operatorgetset {get;set;}

     private void newThreadOperatorChat () {

         SetText (operatorgetset);
     }

【问题讨论】:

    标签: c# winforms textbox cursor multiline


    【解决方案1】:

    您是否尝试将KeyPressEventArgs.Handled 设置为true?这会将事件标记为已处理,并跳过进一步的处理。为了工作,您的EventHandler 需要先于内部的。

    【讨论】:

    • 你的意思是e.Handled :)
    • 取决于你如何命名你的变量:P
    【解决方案2】:

    这对你有用:

        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13) return;
            textBox.Clear();
            e.Handled = true;
        }
    

    正如 Binkam 所建议的,关键是 e.Handled

    【讨论】:

    • if(e.KeyChar == (char) Keys.Return)if(e.KeyChar == 13) 都应该重构为适当的 switch(e.KeyCode)case Keys.Return:...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多