C# 文本框延迟事件

C# 文本框延迟事件,c#,.net,winforms,C#,.net,Winforms,我有一个选项表单,用户必须输入迷你游戏的参数,从8到32。我的问题是,一旦我开始键入,如果我在8下插入一个数字(例如,我想输入20),那么当我键入2并将其转换为8时,事件就会激活 private void TXBheight_TextChanged(object sender, EventArgs e) { if(int.Parse(TXBheight.Text) < 8) { TXBheight.Text = "8";

我有一个选项表单,用户必须输入迷你游戏的参数,从8到32。我的问题是,一旦我开始键入,如果我在8下插入一个数字(例如,我想输入20),那么当我键入2并将其转换为8时,事件就会激活

private void TXBheight_TextChanged(object sender, EventArgs e)
    {

        if(int.Parse(TXBheight.Text) < 8)
        {
            TXBheight.Text = "8";
        }
        else if (int.Parse(TXBheight.Text) > 32)
        {
            TXBheight.Text = "32";
        }
    }
private void TXBheight\u text已更改(对象发送方,事件参数e)
{
if(int.Parse(TXBheight.Text)<8)
{
TXBheight.Text=“8”;
}
else if(int.Parse(TXBheight.Text)>32)
{
TXBheight.Text=“32”;
}
}
有没有什么简单的方法可以延迟,或者等到我打字完毕


对于那些认为这个问题可能重复的人,我看了看,可能的答案是6年前的。在这段时间里,语言和编译器不断发展,所以也许我们都可以从中学习到一些新的东西

为什么不创建任务并在执行之前检查任务是否完成

private Task task; //declare it at the top

private void TXBheight_TextChanged(object sender, EventArgs e)
{
   if(task?.Status == TaskStatus.Running) return;

   task =  Task.Run( () =>
   {
        if(int.Parse(TXBheight.Text) < 8)
        {
            TXBheight.Text = "8";
        }
        else if (int.Parse(TXBheight.Text) > 32)
        {
            TXBheight.Text = "32";
        } 
   });
}
私有任务//在顶部声明它
私有无效TXBheight_TextChanged(对象发送方,事件参数e)
{
if(task?.Status==TaskStatus.Running)返回;
任务=任务。运行(()=>
{
if(int.Parse(TXBheight.Text)<8)
{
TXBheight.Text=“8”;
}
else if(int.Parse(TXBheight.Text)>32)
{
TXBheight.Text=“32”;
} 
});
}

为什么不创建任务并在执行前检查任务是否已完成

private Task task; //declare it at the top

private void TXBheight_TextChanged(object sender, EventArgs e)
{
   if(task?.Status == TaskStatus.Running) return;

   task =  Task.Run( () =>
   {
        if(int.Parse(TXBheight.Text) < 8)
        {
            TXBheight.Text = "8";
        }
        else if (int.Parse(TXBheight.Text) > 32)
        {
            TXBheight.Text = "32";
        } 
   });
}
私有任务//在顶部声明它
私有无效TXBheight_TextChanged(对象发送方,事件参数e)
{
if(task?.Status==TaskStatus.Running)返回;
任务=任务。运行(()=>
{
if(int.Parse(TXBheight.Text)<8)
{
TXBheight.Text=“8”;
}
else if(int.Parse(TXBheight.Text)>32)
{
TXBheight.Text=“32”;
} 
});
}

您可以使用
返回
作为断点,当用户按enter键时,您可以运行代码

您可以将其与按键事件一起使用

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    char ch = e.KeyChar; // Getting the Key that was pressed

    if (ch == 13) // Checking if it equal to 13 ASCII code for return 
    {
        if (int.Parse(textBox1.Text) < 8)
        {
            textBox1.Text = ""; // emptying the textbox
            textBox1.AppendText("8"); // using AppendText() to keep the cursor at the end
        }
        else if (int.Parse(textBox1.Text) > 32)
        {
            textBox1.Text = "";
            textBox1.AppendText("32");
        }
        e.Handled = true; // To say that the event was handled.
    }
}
private void textBox1\u按键(对象发送器,按键事件参数e)
{
char ch=e.KeyChar;//获取按下的键
if(ch==13)//检查返回值是否等于13个ASCII码
{
if(int.Parse(textBox1.Text)<8)
{
textBox1.Text=”“;//清空文本框
textBox1.AppendText(“8”);//使用AppendText()将光标保持在末尾
}
else if(int.Parse(textBox1.Text)>32)
{
textBox1.Text=“”;
文本框1.附录文本(“32”);
}
e、 Handled=true;//表示事件已处理。
}
}

您可以使用
返回
作为断点,当用户按enter键时,您可以运行代码

您可以将其与按键事件一起使用

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    char ch = e.KeyChar; // Getting the Key that was pressed

    if (ch == 13) // Checking if it equal to 13 ASCII code for return 
    {
        if (int.Parse(textBox1.Text) < 8)
        {
            textBox1.Text = ""; // emptying the textbox
            textBox1.AppendText("8"); // using AppendText() to keep the cursor at the end
        }
        else if (int.Parse(textBox1.Text) > 32)
        {
            textBox1.Text = "";
            textBox1.AppendText("32");
        }
        e.Handled = true; // To say that the event was handled.
    }
}
private void textBox1\u按键(对象发送器,按键事件参数e)
{
char ch=e.KeyChar;//获取按下的键
if(ch==13)//检查返回值是否等于13个ASCII码
{
if(int.Parse(textBox1.Text)<8)
{
textBox1.Text=”“;//清空文本框
textBox1.AppendText(“8”);//使用AppendText()将光标保持在末尾
}
else if(int.Parse(textBox1.Text)>32)
{
textBox1.Text=“”;
文本框1.附录文本(“32”);
}
e、 Handled=true;//表示事件已处理。
}
}
简单的答案是: (C#7风格)

公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
this.textBox1.TextChanged+=textBox1\u TextChanged;
this.textBox1.Leave+=textBox1_Leave;
}
私有void TextBox1\u TextChanged(对象发送方,事件参数e)
{
字符串文本=this.textBox1.text;
if(!int.TryParse(text,NumberStyles.Integer,CultureInfo.CurrentCulture,out int number))
{
this.textBox1.Text=“”;
返回;
}
如果(数字>32)
{
this.textBox1.Text=“32”;
}
}
私有无效文本框1_Leave(对象发送方,事件参数e)
{
字符串文本=this.textBox1.text;
if(!int.TryParse(text,NumberStyles.Integer,CultureInfo.CurrentCulture,out int number))
{
this.textBox1.Text=“8”;
返回;
}
如果(数字>32)
{
this.textBox1.Text=“32”;
}
如果(数字<8)
{
this.textBox1.Text=“8”;
}
}

我标准地控制了按下的键和文本的变化(包括粘贴)来检查窗口的正确内容。不幸的是,我只有Borland C++ +Builder和VS6的代码。重新创建这个代码不是那么简单(代码太多),因此只有简单的答案。

< P>简单的答案是: (C#7风格)

公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
this.textBox1.TextChanged+=textBox1\u TextChanged;
this.textBox1.Leave+=textBox1_Leave;
}
私有void TextBox1\u TextChanged(对象发送方,事件参数e)
{
字符串文本=this.textBox1.text;
if(!int.TryParse(text,NumberStyles.Integer,CultureInfo.CurrentCulture,out int number))
{
this.textBox1.Text=“”;
返回;
}
如果(数字>32)
{
this.textBox1.Text=“32”;
}
}
私有无效文本框1_Leave(对象发送方,事件参数e)
{
字符串文本=this.textBox1.text;
if(!int.TryParse(text,NumberStyles.Integer,CultureInfo.CurrentCulture,out int number))
{
this.textBox1.Text=“8”;
返回;
}
如果(数字>32)
{
this.textBox1.Text=“32”;
}
如果(数字<8)
{
this.textBox1.Text=“8”;
}
}

我标准地通过控制按下键和文本更改(包含粘贴)来检查窗口的正确内容。不幸的是,我只在Borland C++ Builder和VS6中使用代码。重新创建此代码不是那么简单(代码太多),因此只需简单的ANSW。

private void Form1_Load(object sender, EventArgs e)
{
    IObservable<long> query =
        Observable
            .FromEventPattern<EventHandler, EventArgs>(
                h => TXBheight.TextChanged += h,
                h => TXBheight.TextChanged -= h)
            .Select(x => Observable.Timer(TimeSpan.FromMilliseconds(250.0)))
            .Switch()
            .ObserveOn(this);

    IDisposable subscription = query.Subscribe(ep =>
    {
        if (int.Parse(TXBheight.Text) < 8)
        {
            TXBheight.Text = "8";
        }
        else if (int.Parse(TXBheight.Text) > 32)
        {
            TXBheight.Text = "32";
        }
    });
}