12 Replies - 393 Views - Last Post: 10 February 2018 - 07:03 AM Rate Topic: -----

#1 Mallard8   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 05-February 18

Labels 1 and 2?

Posted 09 February 2018 - 04:05 AM

I have a program that has 2 labels the idea is that when the button is pressed one is visible the other isn't.
The problem is one label only shows a grey box ? they switch on and off as they should
   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Label1.Visible = False
        Label2.Visible = True
        Label2.Location = New Point(287, 124) 'Place label in same location as Label 1

        'For / Next Loop here 

        
        Label2.Visible = False
        Label1.Visible = True
        Label1.Text = "Winner"



Is This A Good Question/Topic? 0
  • +

Replies To: Labels 1 and 2?

#2 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,527
  • Joined: 12-December 12

Re: Labels 1 and 2?

Posted 09 February 2018 - 04:56 AM

Rather than substituting one label for another in the same position, why not just alternate their Text?
Was This Post Helpful? 0
  • +
  • -

#3 Mallard8   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 05-February 18

Re: Labels 1 and 2?

Posted 09 February 2018 - 08:02 AM

Thanks for looking, I did try that but still no luck? Below is the code fr the button
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim i As Integer = LstNames.Items.Count 'Gets number of entries in Listbox
        Dim chosenItem As System.Object = LstNames.Items.Item(RandomName.Next(i)) 'Pick a random name from listbox
        Dim index As Integer = LstNames.FindString(chosenItem)
        Dim x As Integer
        Lblwinner.Text = "Selecting" 'Change Text of Label
        'These nested For Next loops are for decoration only the winner has already been picked
        For y As Integer = 1 To 3

            For x = 0 To LstNames.Items.Count - 1 'Loop round so highlighted selection moves down Listbox
                LstNames.SetSelected(x, True) 'Highlights name in Listbox
                Thread.Sleep(125) 'This enables you to see highlight moving
            Next x

            For x = LstNames.Items.Count - 1 To 1 Step -1  'Loop round so highlighted selection moves up Listbox
                LstNames.SetSelected(x, True)
                Thread.Sleep(125)
            Next x
        Next y

        LstNames.SetSelected(index, True)  'Highlight the wining name in Listbox
        Lblwinner.Text = chosenItem.ToString 'Place the selected name in Label.text

    End Sub


Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,527
  • Joined: 12-December 12

Re: Labels 1 and 2?

Posted 09 February 2018 - 08:06 AM

"still no luck" is not a useful description. What happens, or doesn't happen, with that new code? Is there an error message?
Was This Post Helpful? 0
  • +
  • -

#5 Mallard8   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 05-February 18

Re: Labels 1 and 2?

Posted 09 February 2018 - 08:11 AM

Yes, you're right 'Still no luck' doesn't explain much!
What happens is the program runs no problem and with each press of the button the random name is highlighted in the Listbox and the name appears on the label. But the first change
 Lblwinner.Text = "Selecting" 'Change Text of Label' 
is ignored?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,527
  • Joined: 12-December 12

Re: Labels 1 and 2?

Posted 09 February 2018 - 08:33 AM

That's probably because you are using Thread.Sleep to block the UI thread, and it doesn't manage to update the text in time. Thread.Sleep should be avoided, use a Timer to fashion some animation.
Was This Post Helpful? 1
  • +
  • -

#7 Mallard8   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 05-February 18

Re: Labels 1 and 2?

Posted 09 February 2018 - 08:42 AM

Thanks will go and have a look at timers.
Was This Post Helpful? 0
  • +
  • -

#8 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1492
  • View blog
  • Posts: 3,786
  • Joined: 01-February 13

Re: Labels 1 and 2?

Posted 09 February 2018 - 12:28 PM

You can call the Label's Refresh method right after setting it's Text property. The Refresh method will force the label to immediately repaint itself before continuing to the loops that call the Sleap method. Don't get me wrong, i am not saying the Sleep method is a good way to do what you want, just that it should only be used if you understand what it does and don't care about the side effect of it basically "freezing up" your application's form until after the last Sleep time has expired.

Using a Timer would keep your application's Form responsive while it is randomly selecting the name, Sleep will not.

As said, this would force the label to repaint itself to reflect the new text before the loops start calling the Sleep method.
        Lblwinner.Text = "Selecting" 'Change Text of Label
        Lblwinner.Refresh()


Was This Post Helpful? 0
  • +
  • -

#9 Mallard8   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 05-February 18

Re: Labels 1 and 2?

Posted 09 February 2018 - 01:09 PM

Thanks for the reply, I looked at timers but couldn't figure out how they delay the program.
Just to try something else I tried this.
    Private Sub btnRandomise_Click(sender As Object, e As EventArgs) Handles btnRandomise.Click
        Dim showinfo As Integer
        Dim showinfo As Integer
        showinfo = 0
        If showinfo = 0 Then
            lblWinner.Text = "Selecting" 'Place the selected name in Label.text
        End If



So the button is pressed, the program enters the If/End If, the timer waits 3 seconds but the label doesn't show any text?

This post has been edited by Mallard8: 09 February 2018 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

#10 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,527
  • Joined: 12-December 12

Re: Labels 1 and 2?

Posted 10 February 2018 - 01:04 AM

That code doesn't have any Timer code.

It declares showinfo twice. It sets this integer to 0 and then immediately checks if the value is 0, which it will be.

Please post details of your attempt to use a Timer that you are struggling with.
Was This Post Helpful? 0
  • +
  • -

#11 Mallard8   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 05-February 18

Re: Labels 1 and 2?

Posted 10 February 2018 - 04:08 AM

I removed the timers from the code as I wanted to get it to work with the thread.sleep first.
Well good news is that I have now got it to work, when the button is pressed first Label shows 'Selecting a Winner' the second Label shows nothing. After a bit of decoration (watching the highlight move up and down the Listbox) the first Label changes to 'The Winner is' and the second label shows the name highlighted in the Listbox. Phew!!
I have now realised I need to pop up a message box in case you press the button and no names have been entered.
This programme will only be used by myself, it is to pick a name from a list of people who have correctly solved a puzzle in our local news sheet. But even so I want it to work as it should as though others were using it.
    Private Sub btnRandomise_Click(sender As Object, e As EventArgs) Handles btnRandomise.Click

        Dim i As Integer = lstNames.Items.Count 'Gets number of entries in Listbox
        Dim chosenItem As System.Object = lstNames.Items.Item(RandomName.Next(i)) 'Pick a random name from listbox
        Dim index As Integer = lstNames.FindString(chosenItem)
        Dim x As Integer

        lblMessages.Text = "Selecting a Winner"
        lblWinner.Text = ""

        'These nested For Next loops are for decoration only the winner has already been picked
        For y As Integer = 1 To 3
            lblWinner.Refresh() 'Redraw Label
            lblMessages.Refresh()


Was This Post Helpful? 0
  • +
  • -

#12 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1492
  • View blog
  • Posts: 3,786
  • Joined: 01-February 13

Re: Labels 1 and 2?

Posted 10 February 2018 - 05:03 AM

As i said in my last post, you can call the Refresh method right after setting the text of the Label(s). You don't want it inside the loop or you will force the Label(s) to repaint themselves over and over again needlessly.
    lblMessages.Text = "Selecting a Winner"
    lblWinner.Text = ""
    lblWinner.Refresh() 'Redraw Label
    lblMessages.Refresh()

    'These nested For Next loops are for decoration only the winner has already been picked
    For y As Integer = 1 To 3


Was This Post Helpful? 1
  • +
  • -

#13 Mallard8   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 05-February 18

Re: Labels 1 and 2?

Posted 10 February 2018 - 07:03 AM

Thanks for the reply I thought I had put them after setting the text but I've tried that many combinations before I got it to work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1