c# Trackbar增加/减少
我对 C# 确实很陌生,并且轨迹栏确实有一些问题。 我试图通过上下调整轨迹栏来增加/减少音量。 我确实有两个命令可以通过串行电缆使用协议发送,“向上”表示增大音量,“向下”表示减小音量。写下“UP”后,您还可以写下您想要增加音量的次数(nn 0-254)。例如,“UP10”会将音量增加 10。
如果您有两个按钮,这非常容易。
void VolumeDownClick(object sender, EventArgs e)
{
try
{ //writing to serial port
serialPort.WriteLine("UP10");
}
catch
{
}
}
但如何使用轨迹栏获得相同的结果呢? 我尝试使用 trackbar.Value,但没有结果。
I am really new to C# and do have a few problems with the trackbar.
I am trying to increase/decreasing the volume by adjusting the trackbar up and down.
I do have two commands that I can send with a protocol through serial cable, lets that it is "UP" for increased and "DOWN" for decreased volume. after writing "UP", you also write how many times (nn 0-254) you want to increase the volume. e.g. "UP10" will increase the volume by 10.
This is really easy if you have two buttons.
void VolumeDownClick(object sender, EventArgs e)
{
try
{ //writing to serial port
serialPort.WriteLine("UP10");
}
catch
{
}
}
But how would I achieve the same result with a trackbar?
I have tried to play around with trackbar.Value, but without result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作为快速猜测,但没有平台信息
如果您可以获得设备当前的音量信息,那么将轨迹栏中的值与实际音量结合起来将很有用,并将其存储在本地值(可能是最后一个音量)中。
在 valueChanged 事件中,检索新值。然后将其与上一卷进行比较,如果其>然后发送 UP(currentvalue-lastvalue),否则如果小于 DOWN(lastvalue-currentvalue) 并将新值存储在最后一个值中。
您最终应该得到一个有效的音量控制。
As a quick guess, but without platform info
If you can get the devices current volume info, then that would be useful to then marry the value in the track bar and the actual volume., and store it in a local value (lastvolume perhaps)
In the valueChanged event, retrieve the new value. Then compare it to the last volume, if its > then send an UP(currentvalue-lastvalue) else if its less DOWN(lastvalue-currentvalue) and store the new value in last value.
You should end up with a working volume control.
假设您将旧卷存储在名为
oldVolume
的变量中。您可以使用轨迹栏的ValueChanged
事件来确定newVolume
并计算差值
。然后您只需将其写入串行端口即可。Assuming you have the old volume stored in a variable named
oldVolume
. You can use the Trackbar'sValueChanged
event to determine thenewVolume
and calculate thedifference
. You can then simply write that to your serial port.使用变量来跟踪当前值。这将用于增加或减少音量。
您只需确定增加或减少的值,您可以通过检查之前的值,然后根据该结果发送 UPValue 或 DOWNValue 来完成此操作。
Use a variable to keep track of what the current value is. This will be used to increase or decrease the volume.
All you have to determine the value increased or decreased, you can do this by checking what the previous value was, and then sending your UPValue or DOWNValue based on that result.