C#textbox问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#textbox问题相关的知识,希望对你有一定的参考价值。

为什么我在网页上 无论怎么改文本里面的值 在断点里看到获取到textbox里面的值还是数据库原来的值 然后一直不能修改
是因为 我写了TextBox6.Text = user.CUtel.ToString();这句 然后一按修改按钮 它自动刷新变成原来的值吗?

网上搜C#日历控件~~然后调用他们写好的JS即可。如果搜不到我再发给你吧~ 参考技术A 1)在Form1上拖入两个TexBox,分别为textBox1和textBox2。textBox1用来显示输入的文本;textBox2用来输入文本。

2)在Form1.cs中添加一个类TextUndoBuffer。代码如下:
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 WindowsFormsApplication1 public partial class Form1 : Form public Form1() InitializeComponent(); TextUndoBuffer buffer; private void Form1_Load(object sender, EventArgs e) buffer = new TextUndoBuffer(); private void textBox1_KeyDown(object sender, KeyEventArgs e) //按下Ctrl+z,取消最近一次输入的内容 if (e.KeyCode == Keys.Z && e.Modifiers == Keys.Control) buffer.Undo(); textBox1.Text = buffer.GetAll(); private void textBox2_KeyPress(object sender, KeyPressEventArgs e) if (e.KeyChar == (char)Keys.Return) string s = textBox2.Text.Trim(); if (s != string.Empty) buffer.PutInBuffer(" " + textBox2.Text.Trim()); textBox2.Clear(); textBox1.Text = buffer.GetAll(); /// <summary> /// 这个类用来记录输入的内容并支持Undo /// </summary> class TextUndoBuffer List<string> buffer; public TextUndoBuffer() this.buffer = new List<string>(); /// <summary> /// 将输入的一句话放入缓冲区 /// </summary> /// <param name="s"></param> public void PutInBuffer(string s) this.buffer.Add(s); /// <summary> /// 从缓冲区获取所有输入的内容 /// </summary> /// <returns></returns> public string GetAll() string s = string.Empty; foreach (var q in this.buffer) s += q; return s; /// <summary> /// Undos实现取消最近一次输入的内容 /// </summary> public void Undo() if (this.buffer.Count == 0) return; buffer.RemoveAt(this.buffer.Count - 1); 3)事件处理

注意:textBox1和textBox2“共用”了同一个KeyDown事件处理函数,详细见上面代码本回答被提问者采纳

C# Visual Studio 2012 语音到 textBox1

【中文标题】C# Visual Studio 2012 语音到 textBox1【英文标题】:C# Visual Studio 2012 Speech to textBox1 【发布时间】:2014-05-29 19:21:05 【问题描述】:

我遇到了语音 API 的问题。我正在使用命令,这很好,但是当我希望在 textBox1 中看到语音时,它不会显示它。

这是我需要帮助的代码。我正在使用开关盒。我尝试了几个 if 语句,但没有一个有效。

case "listen":
                    AI.Speak("I am listening");
                    textBox1.Text = textBox1.Text + " " + e.Result.Text.ToString();
                    break;

每次我说听时,文本框中只有“听”可见

这里是完整的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.IO;
using System.Xml;
using System.Web;
using WindowsMicrophoneMuteLibrary;
using TweetSharp;
/*                                          
 *                                          
 *                                         
 *  
 *                                          
 * 
 */

namespace Test

    public partial class Form1 : Form
    
        SpeechRecognitionEngine sRecognizer = new SpeechRecognitionEngine();
        SpeechSynthesizer AI = new SpeechSynthesizer();
        DateTime now = DateTime.Now;
        Random rnd = new Random();
        WindowsMicMute micMute = new WindowsMicMute();
        TwitterService twitter = new TwitterService("--", "--", "--", "--");
        //string QEvent;
        //string ProcWindow;
        //double timer = 10;
        //int count = 1;

        public Form1()
        
            InitializeComponent();
        

        private void Form1_Load(object sender, EventArgs e)
        
            sRecognizer.SetInputToDefaultAudioDevice();
            sRecognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"D:\Bibliotheek\Mijn Documenten\Commands.txt")))));
            sRecognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(rSpeechRecognized);
            sRecognizer.RecognizeAsync(RecognizeMode.Multiple);
            // LAAD COMMANDS BIJ START-UP
            string[] commands = (File.ReadAllLines(@"D:\Bibliotheek\Mijn Documenten\Commands.txt"));
            lstCommands.Items.Clear();
            lstCommands.SelectionMode = SelectionMode.None;
            foreach (string command in commands)
            
                lstCommands.Items.Add(command);
            
        

        void rSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        
            int ranNum = rnd.Next(1, 10);
            string speech = e.Result.Text;
            switch (speech)
            
                // GROETEN
                case "hello":                               // ALS "hello" WORDT INGESPROKEN IN DE MICROFOON
                    if (ranNum <= 3)                         
                    
                        AI.Speak("Hello Sir");          // ALS RANDOM NUMMER < 5 IS = "hello sir"
                    
                    else if (ranNum >= 4 && ranNum <= 6)                    
                    
                        AI.Speak("Greetings");          // ALS RANDOM NUMMER >= 5 IS = "greetings"
                    
                    else if (ranNum >= 7)
                    
                        AI.Speak("Good day to you");
                    
                    break;
                case "AI":                              // ALS "AI" WORDT INGESPROKEN IN DE MICROFOON
                    if (ranNum <= 4)                         
                    
                        AI.Speak("Yes Sir");            // ALS RANDOM NUMMER < 5 IS = "yes sir"
                    
                    else if (ranNum >= 5)                   
                    
                        AI.Speak("Yes?");               // ALS RANDOM NUMMER >= 5 IS = "yes?"
                    
                    break;

                // SLUIT
                case "exit program":                        // ALS "exit program" WORDT INGESPROKEN IN DE MICROFOON
                    AI.Speak("Until next time");        
                    this.Close();                                // APPLICATIE WORDT GESLOTEN
                    break;

                // WEBSITES
                case "open google":                         // ALS "open google" WORDT INGESPROKEN IN DE MICROFOON
                    System.Diagnostics.Process.Start("http://www.google.nl");   // GOOGLE WORDT GEOPEND
                    break;
                case "open youtube":                        // ALS "open youtube" WORDT INGESPROKEN IN DE MICROFOON
                    System.Diagnostics.Process.Start("https://www.youtube.com/feed/subscriptions"); // YOUTUBE WORDT GEOPEND
                    break;
                case "open tweakers":                        // ALS "tweakers" WORDT INGESPROKEN IN DE MICROFOON
                    System.Diagnostics.Process.Start("http://tweakers.net/"); // TWEAKERS WORDT GEOPEND
                    break;

                // PROGRAMMA'S
                case "run guild wars":                      // ALS "run guild wars" WORDT INGESPROKEN IN DE MICROFOON
                    System.Diagnostics.Process.Start("D:\\Entertainment\\Guild Wars 2\\Gw2.exe");   // GUILD WARS 2 WORDT GEOPEND
                    AI.Speak("Loading program");
                    break;

                // GEGEVENS VAN DE DAG
                case "whats the time":                     // ALS "what time is it" WORDT INGESPROKEN IN DE MICROFOON
                    AI.Speak(DateTime.Now.ToString("HH:mm"));           // TIJD VAN DE DAG WORDT VERTELD
                    break;
                case "whats the day":                      // ALS "what day is it" WORDT INGESPROKEN IN DE MICROFOON
                    AI.Speak(DateTime.Today.ToString("dddd"));          // DAG VAN VANDAAG WORDT VERTELD
                    break;
                case "whats the date":                      // ALS "whats the date" WORDT INGESPROKEN IN DE MICROFOON
                    AI.Speak(DateTime.Today.ToString("dd-MMM-yyyy"));   // DATUM VAN VANDAAG WORDT VERTELD
                    break;

                // ANDERE COMMANDS
                case "go fullscreen":                       // ALS "go fullscreen" WORDT INGESPROKEN IN DE MICROFOON
                    FormBorderStyle = FormBorderStyle.None;
                    WindowState = FormWindowState.Maximized;
                    TopMost = true;
                    AI.Speak("Going into fullscreen mode");
                    break;
                case "exit fullscreen":                     // ALS "exit fullscreen" WORDT INGESPROKEN IN DE MICROFOON
                    FormBorderStyle = FormBorderStyle.Sizable;
                    WindowState = FormWindowState.Normal;
                    TopMost = false;
                    AI.Speak("Exiting fullscreen mode");
                    break;

                // TWITTER
                case "post on twitter":
                    if (listBox1.Visible == true)
                    
                        this.textBox1.Location = new System.Drawing.Point(89, 163);
                        this.label1.Location = new System.Drawing.Point(18, 166);
                    
                    textBox1.Visible = true;
                    label1.Visible = true;
                    break;
                case "post":
                    if (textBox1.Visible == false)
                    
                        AI.Speak("say post on twitter first");
                    
                    else if (String.IsNullOrEmpty(textBox1.Text.Trim()))
                    
                        AI.Speak("you will have to write down something");
                    
                    else
                    
                        twitter.SendTweet(new SendTweetOptions()  Status = textBox1.Text );
                        AI.Speak("Your tweet has been posted");
                        textBox1.Clear();
                    
                    break;
                case "clear post":
                    textBox1.Visible = false;
                    label1.Visible = false;
                    break;
                case "show tweets":
                    listBox1.Visible = true;
                    label2.Visible = true;
                    if (textBox1.Visible == true)
                    
                        this.textBox1.Location = new System.Drawing.Point(89, 163);
                        this.label1.Location = new System.Drawing.Point(18, 166);
                    
                    listBox1.Visible = true;
                    label2.Visible = true;
                    listBox1.Items.Clear();
                    var getTweets = twitter.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions()  Count = 10 );
                    foreach (var tweets in getTweets)
                    
                        listBox1.Items.Add(tweets.Text);
                    
                    break;
                case "clear tweets":
                    listBox1.Visible = false;
                    label2.Visible = false;
                    this.textBox1.Location = new System.Drawing.Point(89, 9);
                    this.label1.Location = new System.Drawing.Point(18, 12);
                    break;
                case "update tweets":
                    if (listBox1.Visible == false)
                    
                        AI.Speak("I cant update without getting the tweets first");
                    
                    else
                    
                        listBox1.Items.Clear();
                        var update = twitter.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions()  Count = 10 );
                        foreach (var tweets in update)
                        
                            listBox1.Items.Add(tweets.Text);
                        
                    
                    break;

                **case "listen":
                    AI.Speak("I am listening");
                    textBox1.Text = textBox1.Text + " " + e.Result.Text.ToString();
                    break;**


                /*
                case "show commands":
                    string[] commands = (File.ReadAllLines(@"D:\Bibliotheek\Mijn Documenten\Commands.txt"));
                    JARVIS.Speak("Very well");
                    lstCommands.Items.Clear();
                    lstCommands.SelectionMode = SelectionMode.None;
                    lstCommands.Visible = true;
                    foreach (string command in commands)
                    
                        lstCommands.Items.Add(command);
                    
                    break;
                case "hide commands":
                    lstCommands.Visible = false;
                    break;
                 */
            
        

        private void lstCommands_SelectedIndexChanged(object sender, EventArgs e)
        

        

        private void btnMic_Click(object sender, EventArgs e)
        
            if (btnMic.Text == "Mute")
            
                btnMic.Text = "Unmute";
                micMute.MuteMic();
                AI.Speak("Muted");
            
            else if (btnMic.Text == "Unmute")
            
                btnMic.Text = "Mute";
                micMute.UnMuteMic();
                AI.Speak("Unmuted");
            
        
    

编辑:

我需要有关这段代码的帮助:

case "listen":
                    AI.Speak("I am listening");
                    textBox1.Text = textBox1.Text + " " + e.Result.Text.ToString();
                    break;

当我说“听”时,AI 会跟着“我在听”。之后,它应该将文本即时消息放入 textbox1 中的麦克风中,但它没有。它只在其中放置“听”。

其他一切正常!

【问题讨论】:

那是很多代码。请将您的问题编辑成一个简洁的问题示例。 e.Result.Text.ToString() 不是已经设置好了,你需要调用一些东西来捕捉语音吗? 【参考方案1】:
case "listen":
    AI.Speak("I am listening");
    textBox1.Text = textBox1.Text + " " + e.Result.Text.ToString();
    break;

e.Result 将仅包含用户说“听”时获得的结果。 在你让 AI 说“我在听”之后,你实际上需要再次倾听用户。

case "listen":
    AI.Speak("I am listening");
    var result = sRecognizer.Recognize();
    textBox1.Text += " " + result.Text;
    break;

【讨论】:

我现在收到此错误:在识别器进行识别时无法执行此操作。所以当我说听的时候识别器已经激活了,这是否意味着我不需要再次调用它,对吧。 那是因为你还没有处理完当前的语音("Listen")。你将不得不改变程序的流程来解决这个问题。您可以保存最后一个语音命令,如果最后一个语音命令是“听”,则继续将当前结果写入文本框。或者根据需要中断当前流程并使用无限循环来托管主要流程和分支。【参考方案2】:

你说“听”的速度有多快? 我怀疑你的问题是语音 API 只是在寻找第一个“Listen”,然后在你再次说“Listen”时它正忙于解析数据。我相信另一个答案正确地解决了这个问题 - 一旦你认识到语音文本,您必须再次调用识别。

试着放慢语速,或者说听(等待命令解析)然后再听。它是否按预期工作?

您可能想调查using asych methods

[编辑] 更改描述后,试试这个:

textBox1.Text = textBox1.Text + " " + speech;

【讨论】:

不管我做什么,我都会得到: 识别器进行识别时无法执行此操作;当我在那里放一个 var 时。

以上是关于C#textbox问题的主要内容,如果未能解决你的问题,请参考以下文章

有没有办法为 TextBox 实现 Redo() 函数? C#

C# TextBox常用方法总结

C#--TextBox的四种禁止编辑方法

Texbox在C#中复制另一个文本框

c#里面使textbox里面只能输入数字,或者小数点的代码

C#TextBox按下回车键触发的事件名称是?