简体   繁体   中英

Switching PictureBoxes

On a Windows Form, I'm trying to use PictureBoxes as status icons. I have 4 icons for Running, Stopped, StartPending and StopPending. I wasn't sure how best to do this, so I decided to just stack them on each other and make only the one that's valid visible. I came up with something like this.

switch (currentServiceStatus)
{
    case "Running":
        pb_startedTestService.Visible = true;
        pb_startingTestService.Visible = false;
        pb_stoppedTestService.Visible = false;
        pb_stoppingTestService.Visible = false;
        break;
    case "StartPending":
        pb_startedTestService.Visible = false;
        pb_startingTestService.Visible = true;
        pb_stoppedTestService.Visible = false;
        pb_stoppingTestService.Visible = false;
        break;
    case "Stopped":
        pb_startedTestService.Visible = false;
        pb_startingTestService.Visible = false;
        pb_stoppedTestService.Visible = true;
        pb_stoppingTestService.Visible = false;
        break;
    case "StopPending":
        pb_startedTestService.Visible = false;
        pb_startingTestService.Visible = false;
        pb_stoppedTestService.Visible = false;
        pb_stoppingTestService.Visible = true;
        break;
}

Which I'm okay with if it was just one service, but there are at least 7 services I'm going to be checking and think that's a little much for a little icon next to the service name. Am I being obsessive? Is it not that big of a deal and won't make my code as sloppy as I think it will? Is there an easier or better way to do this?

Why not use one PictureBox and in your switch statement simply change out which image is being displayed. IE: PictureBox1.Image = ...

And assuming you have the pictures loaded into your resources, here is the syntax for accessing them.

PictureBox1.Image = global::(Namespace).Properties.Resources.(PictureName);

Try to set PictureBox.Image property:

I used a Dictionary collection, and may need to use Properties.Resources to accessing resources at run-time:

var imageDic = new Dictionary<string, Image>
                    {
                        {"Running", Properties.Resources.YourImageName},
                        {"StartPending", new Bitmap("...")},
                        {"Stopped", new Bitmap("...")},
                        {"StopPending", new Bitmap("...")}
                    };

// and use it:
pb.Image = imageDic[currentServiceStatus];

or in your way:

Image imgRunning = ...;
Image imgStartPending = ...;
Image imgStopped = ...;
Image imgStopPending = ...;

switch (currentServiceStatus)
{
    case "Running":
        pb.Image = imgRunning;
        break;
    case "StartPending":
        pb.Image = imgStartPending;
        break;
    case "Stopped":
        pb.Image = imgStopped;
        break;
    case "StopPending":
        pb.Image = imgStopPending;
        break;
}

Only use one picture box per service and assign an image based on the status. C# won't create additional copies of the image, so using them that way is perfectly fine. Or draw the whole control yourself, drawing the pictures for each service while only using one control (or the whole window) as your canvas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM