Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual C#Windows窗体文本框未自动更新_C#_Winforms_Visual Studio 2008 - Fatal编程技术网

Visual C#Windows窗体文本框未自动更新

Visual C#Windows窗体文本框未自动更新,c#,winforms,visual-studio-2008,C#,Winforms,Visual Studio 2008,我试图编写一个从IMU读取值的应用程序。当使用getAtr\u Click方法时,我试图在1秒内获得IMU姿态(即方向)的不同值。但是,虽然这是调用Get_Attitude函数,但它只会在我的表单上更改一次文本框值。我如何使它每次都改变?(我想看到文本框上闪过10个不同的值) 这是我的密码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using S

我试图编写一个从IMU读取值的应用程序。当使用
getAtr\u Click
方法时,我试图在1秒内获得IMU姿态(即方向)的不同值。但是,虽然这是调用
Get_Attitude
函数,但它只会在我的表单上更改一次文本框值。我如何使它每次都改变?(我想看到文本框上闪过10个不同的值)

这是我的密码:

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;
using System.Threading;
using System.Timers;
using VectorNav.Devices;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public static Vn100 vn100 = new Vn100("COM5", 9600);
    // New Vn100 on COM5

    private void Get_Attitude()
        //gets the current yaw, pitch, roll in degrees, and displays
    {
        var attitude = vn100.CurrentAttitude;
        yaw.Text = Convert.ToString(attitude.Ypr.YawInDegs);
        pitch.Text = Convert.ToString(attitude.Ypr.PitchInDegs);
        roll.Text = Convert.ToString(attitude.Ypr.RollInDegs);
    }
    public Form1()
        //connect to the Vn100, set its output to YPR, output at 10Hz
    {
        InitializeComponent();
        vn100.Connect();
        vn100.SetAsyncDataOutputType(Vn100.AsyncOutputType.Ypr, true);
        vn100.SetAsyncDataOutputFreq(10, true);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Get_Attitude();
    }

    private void tare_Click(object sender, EventArgs e)
    {
        vn100.Tare(true);
        vn100.Tare(true); //for some reason it doesn't display the correct Attitude     values w/out a double Tare
        Get_Attitude();
    }

    private void getAtr_Click(object sender, EventArgs e)
    {
        for (int i = 1; i <= 10; i++)
        {
            while (vn100.CurrentAttitude == null)
                Thread.Sleep(10);
            Get_Attitude();
        }
    }

    protected override void OnFormClosing(FormClosingEventArgs e)
        //disconnect from the Vn100 when the box is closed
    {
        vn100.Disconnect();
        base.OnFormClosing(e);
    }

    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用系统线程;
使用系统计时器;
使用矢量导航设备;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
公共静态Vn100 Vn100=新的Vn100(“COM5”,9600);
//COM5上的新Vn100
个人态度
//获取当前的偏航、俯仰、滚动(以度为单位)并显示
{
var姿态=vn100.Current姿态;
yaw.Text=Convert.ToString(姿态.Ypr.YawInDegs);
pitch.Text=Convert.ToString(姿态.Ypr.PitchInDegs);
roll.Text=Convert.ToString(姿态.Ypr.RollInDegs);
}
公共表格1()
//连接到Vn100,将其输出设置为YPR,输出为10Hz
{
初始化组件();
vn100.Connect();
SetAsyncDataOutputType(vn100.AsyncOutputType.Ypr,true);
vn100.SetAsyncDataOutputFreq(10,true);
}
私有void Form1\u加载(对象发送方、事件参数e)
{
获取态度();
}
私有无效皮重点击(对象发送者,事件参数)
{
vn100.皮重(真);
vn100.Tare(true);//由于某些原因,它没有显示正确的姿态值,而不是双皮重
获取态度();
}
私有void getAtr_单击(对象发送方,事件参数e)
{
对于(int i=1;i将其更改为:

 private void Get_Attitude()
    //gets the current yaw, pitch, roll in degrees, and displays
{
    var attitude = vn100.CurrentAttitude;
    yaw.Text = Convert.ToString(attitude.Ypr.YawInDegs);
    pitch.Text = Convert.ToString(attitude.Ypr.PitchInDegs);
    roll.Text = Convert.ToString(attitude.Ypr.RollInDegs);

    vn100.CurrentAttitude = null;
}

private void getAtr_Click(object sender, EventArgs e)
{
for (int i = 1; i <= 10; i++)
{
    while (vn100.CurrentAttitude == null)
        Thread.Sleep(10);
    Get_Attitude();
    Application.DoEvents();
}
}
private void Get_态度()
//获取当前的偏航、俯仰、滚动(以度为单位)并显示
{
var姿态=vn100.Current姿态;
yaw.Text=Convert.ToString(姿态.Ypr.YawInDegs);
pitch.Text=Convert.ToString(姿态.Ypr.PitchInDegs);
roll.Text=Convert.ToString(姿态.Ypr.RollInDegs);
vn100.Current姿态=零;
}
私有void getAtr_单击(对象发送方,事件参数e)
{

对于(int i=1;i这是因为您正在拖拽UI线程:

private void getAtr_Click(object sender, EventArgs e)
{
    for (int i = 1; i <= 10; i++)
    {
        while (vn100.CurrentAttitude == null)
            Thread.Sleep(10); // < - UI can't respond in a sleep
        Get_Attitude();
    }
}
或者可以使用超时来更新表单,而不是使用循环

private void tare_Click(object sender, EventArgs e)
{
    System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
    myTimer.Tick += new EventHandler(TimerEventProcessor);
    myTimer.Interval = 10;
    myTimer.Start();
}

void TimerEventProcessor(object sender, EventArgs e)
{
    Get_Attitude();
}
调用getAttr_Click()后,是否希望在1s内以10ms的间隔在文本框中显示10个(可能)不同的值

如果是,那么就是这样做的:

private void getAtr_Click(object sender, EventArgs e)
{
    for (int i = 1; i <= 10; i++)
    {
        while (vn100.CurrentAttitude == null)
            Thread.Sleep(10);
        Get_Attitude();

        Thread.Sleep(10);
    }
}
private void getAtr\u单击(对象发送者,事件参数e)
{

对于(int i=1;我有点像。除了从来没有真正做过第一个。
DoEvents
是非常糟糕的风格,并且是许多难以调试的问题的根源。@Cody Gray,同意的DoEvents可能是不好的谢谢!完全有道理。如果你想知道,VN100被设置为10 Hz的输出值。我只是用这段代码获取足够的值来帮助指导我的机器人的方向。
private void tare_Click(object sender, EventArgs e)
{
    System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
    myTimer.Tick += new EventHandler(TimerEventProcessor);
    myTimer.Interval = 10;
    myTimer.Start();
}

void TimerEventProcessor(object sender, EventArgs e)
{
    Get_Attitude();
}
private void getAtr_Click(object sender, EventArgs e)
{
    for (int i = 1; i <= 10; i++)
    {
        while (vn100.CurrentAttitude == null)
            Thread.Sleep(10);
        Get_Attitude();

        Thread.Sleep(10);
    }
}