'C# Forms. Changing the background color (Blink)
I am working on a program that allows you to control the OBS program. I receive json messages from OBS which I then process. These messages control the lighting on the Launchpad. However, I would like the buttons in the program itself to change color in addition to the lights on the Launchpad. The problem for me is the fact that the colors are not only static, but flickering too. I need to change them depending on the state of some control in OBS. I don't know how to solve my problem. Create a new method or something?
Example json i recive
{"muted":false,"sourceName":"Audio Device","update-type":"SourceMuteStateChanged"}
{"muted":true,"sourceName":"Audio Device","update-type":"SourceMuteStateChanged"}
In this example, the first message means that the audio input has been unmuted and then that it has been muted.
The part of c # that handles this
else if (upadte_type.Groups[0].Value == "SourceMuteStateChanged")
{
Match sourceName = Regex.Match(e.Data, "(?<=\"sourceName\":\").*?(?=\")");
if (sourceName.Success)
{
Match isMuted = Regex.Match(e.Data, "(?<=\"muted\":).*?(?=,)");
if (isMuted.Success)
{
if(isMuted.Groups[0].Value == "false")
{
foreach(MuteListClass s in padsMute)
{
if(s.mute == sourceName.Groups[0].Value)
{
outputDevice.Open();
byte[] stat = { 240, 0, 32, 41, 2, 13, 3, 0, (byte)s.id, (byte)padColorSettingsON[s.id, 0], 247 };
outputDevice.SendSysEx(stat);
outputDevice.Close();
}
}
}
else
{
foreach (MuteListClass s in padsMute)
{
if (s.mute == sourceName.Groups[0].Value)
{
outputDevice.Open();
byte[] stat = { 240, 0, 32, 41, 2, 13, 3, 0, (byte)s.id, (byte)padColorSettingsOFF[s.id, 0], 247 };
outputDevice.SendSysEx(stat);
outputDevice.Close();
}
}
}
}
}
}
outputDevice = DeviceManager.OutputDevices[midiOutBox.SelectedIndex];
List<MuteListClass> padsMute = new List<MuteListClass>();
public class MuteListClass
{
public string mute { get; set; }
public int id { set; get; }
}
padColorSettingsON //int array which holds a number that corresponds to the color of the light in Launchpad
byte[] stat = { 240, 0, 32, 41, 2, 13, 3, 0, (byte)s.id, (byte)padColorSettingsON[s.id, 0], 247 }; //byte array that will be sent to Launchpad
I know Regex is not a right way for json, but this part of the code is quite old and I'm going to change it to serialization. (I am still learning all of this)
I would like those gray buttons in the program to change color like the lights on the Launchpad. For example. When the device is unmuted the lower left corner is green, but when the device is muted it blinks red and green.
For Launchpad there is no big problem because this function is built in (I send a different byte array) but I don't know how to solve it in c #. I want to be able to control between static and flickering colors.
I've heard about async and await but I don't know how to apply them and if they will work for me in this case.
I was also thinking about making a separate function that would constantly change the color of the buttons based on boolean or something like that, but I don't know how to make a function that would still run in the background and check everything.
I think it is also worth mentioning that the function in c # that I showed earlier is only called when OBS sends a new json message, and all 81 buttons that you can see in the screenshot can be lit simultaneously. The buttons themselves are also modified and each button has an ID corresponding to the buttons on the Launchpad so they can be edited separately, for example.
foreach(LaunchpadButton b in GetAll(this, typeof(LaunchpadButton)))
{
if(b.ID == 11){b.BackColor = Color;}
}
Solution 1:[1]
This question is a little vague to be honest, so it's not quite clear what you're asking in full, maybe looking for guidance on how to approach it which isn't really what SO is for, but I'll take a stab.
So this kind of creates the effect you're looking for I think?
Button targetButton;
public Form1()
{
InitializeComponent();
targetButton = button1;
}
private void button2_Click(object sender, EventArgs e) => Task.Run(() => FlickerButton(targetButton, 1, Color.Red));
private void button3_Click(object sender, EventArgs e) => Task.Run(() => FlickerButton2(targetButton, 1, Color.Red, Color.Blue, Color.Green));
public void FlickerButton(Button button, int seconds, Color color)
{
var currColor = button.BackColor;
button.BackColor = color;
Thread.Sleep(seconds * 1000);
button.BackColor = currColor;
}
public void FlickerButton2(Button button, int seconds, params Color[] colors)
{
var currColor = button.BackColor;
foreach (var color in colors)
{
button.BackColor = color;
Thread.Sleep(seconds * 1000);
}
button.BackColor = currColor;
}
You can see when I click button 2, changes color for 1 second and then changes back

And I'm not going to screenshot this, but clicking on button 3 will do similar but with multiple colors.
I'm not quite sure how your byte array fits in if I'm honest, but I guess that's for you to figure out
byte[] stat = { 240, 0, 32, 41, 2, 13, 3, 0, (byte)s.id, (byte)padColorSettingsON[s.id, 0], 247 };
You can do this 100 times all at the same time, which based on the video I watched on Launchpad is what you're going for.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DubDub |


