如何在计算循环中更新文本框的值? [英] How do I update the value of a textbox in calculation loop?

查看:45
本文介绍了如何在计算循环中更新文本框的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在do..while循环中显示文本框中的值更改以进行计算。我发现文本框只显示计算的最终值。请帮忙。谢谢!



我尝试了什么:



 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Threading;
使用 System.Windows.Forms;

命名空间 Testprogram1
{
// < span class =code-comment>声明委托
public 委托 void ValueChangedEventHander( object sender,EventArgs ex);
public partial class FrmMainW :表格
{
public FrmMainW()
{
InitializeComponent();
}

private void btnAdd_Click( object sender,EventArgs e)
{
Add();
}

private void btnSub_Click( object sender,EventArgs e)
{
Sub();
}

private void btnAutoAdd_Click( object sender,EventArgs e)
{
do
{
Add( );
} while (num < 20000 < /跨度>);
}

<跨度类= 代码关键字>私有 <跨度类= 代码关键字>空隙 btnAutoSub_Click(<跨度类= code-keyword> object
sender,EventArgs e)
{
do
{
Sub( );
} while (num > -20000);
}

私人 void 添加()
{
num = num + 1 ;
strnum = string .Format( { 0:0.00},num);
TxtResult.Text = strnum;
}
私有 void Sub()
{
num = num - 1 ;
strnum = string .Format( { 0:0.00},num);
TxtResult.Text = strnum;
}
}
}

解决方案
文本框被更新,然而形式仅重绘时它有时间处理它的事件队列,但它没有时间这样做,因为你的代码在循环中。如果每次更新文本框时都使用Application.DoEvents(),它将导致重绘



Application.DoEvents方法(System.Windows.Forms的) [ ^ ]



更好的解决方案是在单独的线程上运行计算并使用Invoke从单独的线程更新GUI,这样GUI可以自由更新而你的代码在自己的线程上运行。


I want to show the value change in a textbox for calculation in a do..while loop. I found that the textbox only show the final value of the calculation. Please help. Thanks !

What I have tried:

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;

namespace Testprogram1
{        
    // Declare a delegate
    public delegate void ValueChangedEventHander(object sender, EventArgs ex);
    public partial class FrmMainW : Form
    {
        public FrmMainW()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Add();
        }

        private void btnSub_Click(object sender, EventArgs e)
        {
            Sub();
        }

        private void btnAutoAdd_Click(object sender, EventArgs e)
        {
            do
            {
                Add();
            } while (num < 20000);
        }

        private void btnAutoSub_Click(object sender, EventArgs e)
        {
            do
            {
                Sub();
            } while (num > -20000);
        }

        private void Add()
        {
            num = num + 1;
            strnum = string.Format("{0:0.00}", num);
            TxtResult.Text = strnum;
        }
        private void Sub()
        {
            num = num - 1;
            strnum = string.Format("{0:0.00}", num);
            TxtResult.Text = strnum;
        }
    }
}

解决方案

The textbox is updating, however the form only redraws when it has time to process its event queue, but it has no time to do that as your code is in a loop. If you use Application.DoEvents() every time you update the textbox it will cause a redraw

Application.DoEvents Method (System.Windows.Forms)[^]

A better solution is to run your calculations on a separate thread and use "Invoke" to update the GUI from your separate thread and that way the GUI is free to update while your code runs on its own thread.


这篇关于如何在计算循环中更新文本框的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆