Answered Changing button text color depending on link status

JonnyPeck

New member
Joined
May 1, 2020
Messages
1
Programming Experience
Beginner
Hello there,

I hope all are coping well in this 'lock-down'?

I'm getting to grips with C# and have created a very basic form in Visual Studio (which I will jazz up a bit):

1588339559077.png


Each of the buttons when clicked - will open up a specific web based environment.

An example of the code for the top left button is: (I have removed some values)

C#:
private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://********/sites/dept/csm/catt/Pages/AccountChanges.aspx?isdlg=1&&FilterField1=Instance_x0020_ID&FilterValue1=******");

Is there anything I can add to this line of code so that the text on the button will show as red if the link is down, and green to show the link is up?

I'm still new to c# so your expertise is much appreciated.
 
Last edited by a moderator:
Yes, there is. But that depends on how you are proven if the URL is reachable or not. How are you able to provide this information in the button click event?
 
No, there isn't anything you can add to that specific line of code to dynamically mark the button whether the related site is up or down. There are other lines of code that you could write that can update the look of the button to indicate whether it is up or down, but it will not be a one-liner, and the code to do the UI update should not really live in the click event.

Think of if it this way, good car dashboard design would tell you how much gas you currently have in the car without having to start the car first. With your current approach of wanting to mark the button red or green when the button is clicked is like having to start the car first and then the dashboard's gas gauge showing the gas level. (Granted, most car designers have opted for this design choice where you have to at least turn on the car accessories to see the gas level -- probably as a theft deterrent -- but at least you don't have to start the engine.)

Anyway, for your button, it would be good if you setup a timer that would periodically check the status of the systems that you depend on, and update the button colors as appropriate. See previous posts above about approaches for checking whether the system is up or down.
 
While the best answer has been provided, it is the course of action you should proceed with for a proper implementation to check that the server status is reachable, and that header requests can be read appropriately.

Sticking with what you asked for however. You can check the title of the window you are opening the process of and if the title of your users browser contains the title set by your website in it's header, you can confirm the user is logged in. You can do that with the following thread-safe code, as par Skydiver's comments regarding updating your UI from a button :
C#:
        public delegate void Call_Back_To_Set(Color new_Value);

        private void Update_UI(Color this_color)
        {
            /* I am talking with your UI, and I can update your UI with the new color. */
            button1.ForeColor = this_color;
        }
        private void Execute_Non_UI()
        {
            using (Process my_Process = Process.Start("notepad.exe"))
            {
                /* Using the process, you start it, then wait for the process to start,
                and if the title of the process equals the title of your logged in browser page,
                you can confirm the user is logged in. */

                Thread.Sleep(1000); /* Wait for your process to open */
                bool hasTitle = my_Process.MainWindowTitle.Contains("Untitled - Notepad");
                Debug.WriteIf(hasTitle, "User is logged in.");
                /* Update color */
                button1.Invoke(new Call_Back_To_Set(Update_UI), Color.Green);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            /* Start a new thread and run it */
            Thread new_Thread = new Thread(Execute_Non_UI);
            new_Thread.Start();
        }
The reason I advise @JohnH's answer, is because its safer, and its appropriate.

However, you should also note, if someone "spy's" on your source code using various unmentioned tools, they will see this as a security flaw in your application and they will be able to override the vulnerability of your bug which you created, and subsequently force your application to operate as a logged in user by overriding and setting the title of some web browser pages window title. For example, they can do that by setting the following API Call :
C#:
        [DllImport("user32.dll")]
        static extern int SetWindowText(IntPtr wHdl, string wVlu);
        private void button2_Click(object sender, EventArgs e)
        {
            Process[] process = Process.GetProcessesByName("Notepad");
            if (process.Length > 0)
            SetWindowText(process[0].MainWindowHandle, "I hacked your title");
        }
Since we don't know the context of which you are planning to use your application, the code I posted in response to your question could be a security risk easily exploited. That said, if you still wish to use this method, you should further consider obfuscating your code before publishing it. Doing so will make it a little harder for those pesky hackers to read your source more clearly.
 
Back
Top Bottom