0

I am writing a screensaver program in C#. And I want it to do the following:

Start with a text. After about 3 seconds or more, hide the text and show an image. After the next 3 seconds, hide the image show the text and keep going round the loop until the user does something that exits the screensaver.

What I have done: I started with a simple textlabel and a timer control. I was able to get the textlabel change positions on screen after every 3 seconds. I updated my code to include a picturebox, and in my timer_tick method, I inserted an if-else statement to check, when the method is called, if the textlabel is shown, hide it and show the picturebox. else if the picturebox is shown, hide it and show the textbox. Code is shown below:

  private void Form1_Load(object sender, EventArgs e)
    {
        Cursor.Hide();
        TopMost = true;

        moveTimer.Interval = 3000;
        moveTimer.Tick += new EventHandler(moveTimer_Tick);
        moveTimer.Start();
    }

    private void moveTimer_Tick(object sender, System.EventArgs e)
    {
        //Move text to new Location
        //textLabel.Left = rand.Next(Math.Max(1, Bounds.Width - textLabel.Width));
        //textLabel.Top = rand.Next(Math.Max(1, Bounds.Height - textLabel.Height));

        if (pictureBox1.Enabled == true)
            {
                pictureBox1.Hide();
                textLabel.Show();
            }

        if (textLabel.Enabled == true)
            {
                textLabel.Hide();
                pictureBox1.Show();
            }
    }

Here's the problem: WHen I run the screensaver program, the screen starts with the text, changes to the picture after 3 seconds and stops there.

What do I do to get it moving in a continous loop, showing/hiding the textlabel or picturebox?

Have I implemented this in the right way?

Please clear and concise explanations/answers will be highly appreciated.

Thanks!

Lolu
  • 1
  • 1
  • 2
  • 1
    Hide and Show are not the same thing as Enable and Disable. If you want to see if something is shown or not, you have to use the [Visible](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.visible(v=vs.71).aspx) property. – Robert Harvey Feb 26 '13 at 17:02
  • Are you trying to say that my if-else condition i.e. (textLabel.Enabled == true) is not accurate? – Lolu Feb 26 '13 at 17:04
  • Well noted Robert, Thanks. I just made that little change in my code but it's still stuck at the picture. – Lolu Feb 26 '13 at 17:07
  • See KingCronus's answer below. – Robert Harvey Feb 26 '13 at 17:08

2 Answers2

1

Perhaps you could keep the state in a variable that you can switch

private bool state = false; 

private void moveTimer_Tick(object sender, System.EventArgs e)
{
    //Move text to new Location
    //textLabel.Left = rand.Next(Math.Max(1, Bounds.Width - textLabel.Width));
    //textLabel.Top = rand.Next(Math.Max(1, Bounds.Height - textLabel.Height));

    if (state)
    {
        pictureBox1.Hide();
        textLabel.Show();
    }
    else
    {
        textLabel.Hide();
        pictureBox1.Show();
    }
    state = !state;
}

How about something like this?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
KingCronus
  • 4,509
  • 1
  • 24
  • 49
  • WOW! Just tried it and it worked! Thank you so much KingCronus and Robert!! – Lolu Feb 26 '13 at 17:11
  • With this, I intend to have various texts and images on the form and hide/show them randomly. Thanks alot! – Lolu Feb 26 '13 at 17:12
  • Another issue I am having is getting the textlabel and picturebox to align at the center of the screen. Right now, its at the top left corner. I have tried to use the properties window in my IDE (Visual C# 2010 Express) to change the TextAlign to Center, but it still stays at the top left corner. Can you please guide me on what to do? – Lolu Feb 26 '13 at 17:20
  • Check you don't have any anchors set. If you still have trouble best thing is to open a new question. – KingCronus Feb 26 '13 at 17:21
  • OK. I'l check that. Thanks King. – Lolu Feb 27 '13 at 15:21
1

Enabled says whether the object can receive input. Visible is what says if its visible or not.

You see it change once and only once because all the objects are enabled. The first if succeeds, hiding the picture and showing the text. But then the second if also succeeds, showing the picture and hiding the text. Since this is all in one event callback, you never see the first if happen, because the second overrides it.

As you're realizing in the comments, the answer is to not check enabled. Instead, check Visible. Make sure to use an else as well, otherwise you may still get the same issue of both being true.

private void moveTimer_Tick(object sender, System.EventArgs e)
{
    //Move text to new Location
    //textLabel.Left = rand.Next(Math.Max(1, Bounds.Width - textLabel.Width));
    //textLabel.Top = rand.Next(Math.Max(1, Bounds.Height - textLabel.Height));

    if (pictureBox1.Visible == true)
        {
            pictureBox1.Hide();
            textLabel.Show();
        }
    else
        {
            textLabel.Hide();
            pictureBox1.Show();
        }
}
Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
  • Thanks Scott! I have noted the difference between Enabled and Visible. And I've made that correction. AT least, I wont make that mistake anymore. Thank you. – Lolu Feb 26 '13 at 17:15
  • It's an easy mistake to make, in part because `Show` and `Hide` obscure what gets changed. (Incidentally, instead of calling those functions, you could just change the member, e.g. `textLabel.Visible = false` and get the same result.) Good luck, and have fun! – Scott Mermelstein Feb 26 '13 at 17:19