C
C#•3d ago
pauliology

Online/offline colour menu colour change

Hi all, as the title states I am trying to get the menu items to change colour to green if pingable, red if not pingable. An online/offline thing if you will. I am very new and still learning. Currently this is what I have, im trying to use the code in the converter.cs and the main functions are in the MainViewModel.cs any advice is more than welcome! https://paste.mod.gg/mxpnkbwidxks/3
BlazeBin - mxpnkbwidxks
A tool for sharing your source code with the world!
31 Replies
Angius
Angius•3d ago
What constitutes "pingable"? That you can send a HEAD or whatever request and get 200 OK back? In any case, you probably want to do that on a timer Short polling, in other words
pauliology
pauliologyOP•3d ago
Oh i see umm pingable i guess would be if the ipaddress status returns
pauliology
pauliologyOP•3d ago
No description
pauliology
pauliologyOP•3d ago
so this is what the menu looks like, forgive the colours i was just mucking around essentially, they are radio buttons they would each have their own ip to ping for example dr055 would be 8.8.8.8 for giggles 60 would be 192.168.0.1 as an example 61 something ridiculous just to fail the idea was that isPingable would see if it can be pinged, if it can change colour to green in very simplistic terms if it can't be pinged be red
Angius
Angius•3d ago
So, for each IP address you would have to send the ping on a timer Ideally, you'd do something like
class Thing(string name, string ip, bool isOnline = false)
{
// properties
}

public List<Thing> Things { get; } = [
new("DR2130", "8.8.8.8"),
new("DR060", "127.0.0.1"),
// ...
];

var timer = new PeriodicTimer(TimeSpan.FromMinutes(1));
while (await timer.WaitForNextTickAsync())
{
foreach (var thing in Things)
{
thing.IsOnline = await CheckIfOnline(thing.IP);
}
}
class Thing(string name, string ip, bool isOnline = false)
{
// properties
}

public List<Thing> Things { get; } = [
new("DR2130", "8.8.8.8"),
new("DR060", "127.0.0.1"),
// ...
];

var timer = new PeriodicTimer(TimeSpan.FromMinutes(1));
while (await timer.WaitForNextTickAsync())
{
foreach (var thing in Things)
{
thing.IsOnline = await CheckIfOnline(thing.IP);
}
}
Somewhat pseudocode-ish, but you get the idea Then, you'd bind to that list Things, instead of manually creating each individual element
pauliology
pauliologyOP•3d ago
you give me too much credit, i have no idea how to do that. For the most part i've been following a really old outdated tutorial and it stopped and i feel like i haven't really learnt anything as its too advanced for me
pauliology
pauliologyOP•3d ago
may i ask what goes inside the //properties
Angius
Angius•3d ago
The properties 😛
class Thing(string name, string ip, bool isOnline = false)
{
public string Name { get; set; } = name;
public string IP { get; set; } = ip;
public bool IsOnline { get; set; } = isOnline;
}
class Thing(string name, string ip, bool isOnline = false)
{
public string Name { get; set; } = name;
public string IP { get; set; } = ip;
public bool IsOnline { get; set; } = isOnline;
}
pauliology
pauliologyOP•3d ago
ahh i see i had this
public bool IsPingable
{
get
{
return _isPingable;
}
set
{
_isPingable = value;
OnPropertyChanged(nameof(IsPingable));
}
}
public bool IsPingable
{
get
{
return _isPingable;
}
set
{
_isPingable = value;
OnPropertyChanged(nameof(IsPingable));
}
}
but i like yours better it makes sense
Angius
Angius•3d ago
Mid you it's still fairly pseudocode-ish, you might need to do the OnPropertyChanged thing inside of the Thing, not 100% sure MVVM Toolkit is super useful here, though: https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/
pauliology
pauliologyOP•3d ago
wouldn't even know how to use it im struggling to get this, to be honest
Angius
Angius•3d ago
Perhaps start from easier projects, then? UI apps are fairly complex
pauliology
pauliologyOP•3d ago
this is the final thing then im done thats why i wanted it to work
Angius
Angius•3d ago
Understandable
pauliology
pauliologyOP•3d ago
so should i modify my code to match yours or can i just tweak mine a little
Angius
Angius•3d ago
As I said, my code is not copy&paste It's only to give you a general idea
pauliology
pauliologyOP•3d ago
yeah
Angius
Angius•3d ago
Have a list of things, loop over it, check each thing, update the state so it's reflected in the UI That's the general idea
pauliology
pauliologyOP•3d ago
yup so i had something like that
private readonly List<string> ipAddresses = new() { "192.168.1.1", "8.8.8.8", "8.8.4.4", "10.221.79.32"};

private async Task<bool> PingIpAddress(string ipAddress)
{
using (Ping ping = new Ping())
{
try
{
PingReply reply = await ping.SendPingAsync(ipAddress);
return reply.Status == IPStatus.Success;
}
catch
{
return false; // Return false if an exception occurs
}
}
}
private readonly List<string> ipAddresses = new() { "192.168.1.1", "8.8.8.8", "8.8.4.4", "10.221.79.32"};

private async Task<bool> PingIpAddress(string ipAddress)
{
using (Ping ping = new Ping())
{
try
{
PingReply reply = await ping.SendPingAsync(ipAddress);
return reply.Status == IPStatus.Success;
}
catch
{
return false; // Return false if an exception occurs
}
}
}
that was my little list and this was my foreach
private async Task PingAllIps()
{
foreach (var ipAddress in ipAddresses)
{
bool isPingSuccessful = await PingIpAddress(ipAddress);

// Update the corresponding RadioButton's Tag property
UpdateMenuButtonTag(ipAddress, isPingSuccessful ? "Color2" : "Color5");
}
}
private async Task PingAllIps()
{
foreach (var ipAddress in ipAddresses)
{
bool isPingSuccessful = await PingIpAddress(ipAddress);

// Update the corresponding RadioButton's Tag property
UpdateMenuButtonTag(ipAddress, isPingSuccessful ? "Color2" : "Color5");
}
}
Angius
Angius•3d ago
That seems fine too, yeah All you're missing is a timer, then
pauliology
pauliologyOP•3d ago
yeah which i can use your way of doing but the problem was its not changing colours not getting any errors either so im kinda lost
Angius
Angius•3d ago
Do some debugging $debug
MODiX
MODiX•3d ago
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
pauliology
pauliologyOP•3d ago
im thinking my update menu function may be the problem
private static void UpdateMenuButtonTag(string ipAddress, string tag)
{
// Find the RadioButton with the corresponding IP address and update its Tag property
var radioButton = Application.Current.MainWindow.FindName(ipAddress) as RadioButton;
if (radioButton != null)
{
radioButton.Tag = tag;
}
}
private static void UpdateMenuButtonTag(string ipAddress, string tag)
{
// Find the RadioButton with the corresponding IP address and update its Tag property
var radioButton = Application.Current.MainWindow.FindName(ipAddress) as RadioButton;
if (radioButton != null)
{
radioButton.Tag = tag;
}
}
Angius
Angius•3d ago
Place a breakpoint, run the debugger, and see
pauliology
pauliologyOP•3d ago
i'll give it a go
leowest
leowest•3d ago
if u match his code u lose OnPropertyChanged which is an impotant piece of code for WPF it notifies the ui of changes and you could use a XAML datatrigger that changes from green to red with the IsPingable information https://wpf-tutorial.com/styles/trigger-datatrigger-event-trigger/ the 2nd example
taner.
taner.•3d ago
u could bind an converter as well to the button
pauliology
pauliologyOP•2d ago
I have the converter @taner. i thought thats what i was trying to do @Angius you around
taner.
taner.•2d ago
Does it implement the IValueConverter interface?

Did you find this page helpful?