[英]C# trackbar not updating
i've run into this pretty tricky problem recently and i hoped somebody could help me. 我最近遇到了这个相当棘手的问题,我希望有人可以帮助我。
i have a program that uses trackbars as to display sound volume and it's controlled with an Arduino via serial. 我有一个程序,使用轨迹栏显示音量,它由Arduino串口控制。
When i try to modify the value (programmaticaly) of the trackar (moving the slider) in any method, it works perfectly with the following code : 当我尝试在任何方法中修改trackar的值(programmaticaly)(移动滑块)时,它与以下代码完美配合:
trackbar1.Value = ...;
However, when i put this in my serial data handler, it doesn't works :/ 但是,当我把它放在我的串行数据处理程序中时,它不起作用:/
I declare the serial data handler this way : 我用这种方式声明了串行数据处理程序:
//declaring arduinoCom
public SerialPort arduinoCOM;
//In form1
arduinoCOM.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
my handler looks like this : 我的处理程序如下所示:
public void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
trackBar2.Value = 50;
}
The serial communication works flawlessly, and the handler does it's job no problem. 串行通信完美无瑕,处理程序完成它的工作没有问题。
I've tried for 2 days now and i was able to identify that the only difference between the working trackbar and the not-working one is where the "trackbar1.value" is located. 我已经尝试了2天了,我能够确定工作轨道栏和不工作轨道栏之间的唯一区别是“trackbar1.value”所在的位置。 So i've remove the rest of the (i hope) unessecary code for clarity reasons. 因此,为了清晰起见,我删除了其余的(我希望的)unessecary代码。
So my Question is why does the Trackbar slider doesn't move when i try to modify it's value outside of the "standards method" 所以我的问题是为什么当我尝试在“标准方法”之外修改它的值时,轨迹栏滑块不会移动
additional informations : I've tried runnning the program and then pausing it with visual stuio and the trackbar.Value has been changed successfully, the only thing that isn't working is the graphics side. 其他信息:我试过运行程序,然后将它与视觉stuio和trackbar暂停.Value已成功更改,唯一不起作用的是图形方面。
I've tested with multiple trackbars, and tried using 我已经测试了多个轨道栏,并尝试使用
trackbar1.Refresh();
it didn't work 它不起作用
Picture of the value of trackbar 1 and 2 as well as picture of all 5 : Values of trackbars 轨迹栏1和2的值的图片以及所有5的图片: 轨迹栏的值
The DataReceived event for SerialPort is raised on a secundary thread (not the UI thread) from which you cannot change UI elements. SerialPort的DataReceived事件是在一个不能更改UI元素的secundary线程(而不是UI线程)上引发的。 Using 'Invoke', you can make the change in the UI thread Instead of 使用“调用”,您可以在UI线程中进行更改而不是
public void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
trackBar2.Value = 50;
}
use: 使用:
public void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
if (trackbBar2.IsHandlecreated) trackBar2.Invoke(new Action(() => trackbar.Value = 50));
}
I found the problem, when i was declaring my serial communication i was using ` 我发现了问题,当我宣布我的串口通信时,我正在使用`
Form1 form1 = new Mixer.Form1();
initialiseSerialEventHandler(arduinoCOM);
and instead i should only use 而我应该只使用
initialiseSerialEventHandler(arduinoCOM);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.