VB.NET: Label doesn't refresh - vb.net

I think that these line of code should work properly, but the label never updates it's value... Any idea what am I missing?
Timer is Enabled from properties...
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim out As Integer = 13
Dim s As String
s = Format(out, "0#")
Label1.Text = s
Label1.Refresh()
out -= 1
End Sub

Related

How I can change the text of a single label from a string array vb.net?

so I want to display a string array in a single label that change the contents with a period of time ,
I ve tried every thing the timer, the background worker every thing , the problem when I use a loop inset a timer the interval in the start should be so long if the array items was so many so I tried the background worker but it not works
this is the code :
Dim array() As String = {"so", "nb", "de", "rty", "dcds"}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
Dim delay As Integer = 2000
Dim interval As Integer = 100
Dim elapsed As Integer = 0
Dim pos As Integer = array.Length
While Not worker.CancellationPending
If (elapsed >= delay) Then
worker.ReportProgress(pos)
' change label text in the Progress event handler
pos = (pos + 1)
elapsed = 0
If (pos = array.Length) Then
Exit While
End If
End If
Thread.Sleep(interval)
End While
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Dim j As Integer
For j = 0 To array.Length
Label1.Text = array(j)
Next
End Sub
Your for loop is overwriting the .Text property of the lable on each iteration. You will also get an Index Out of Range exception because the indexes of an array are zero based. The highest index will be 1 less than the .Length.
Private arStr() As String = {"so", "nb", "de", "rty", "dcds"}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim LabelText = String.Join(Environment.NewLine, arStr)
Label1.Text = LabelText
End Sub
EDIT
Private arStr() As String = {"so", "nb", "de", "rty", "dcds"}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Static index As Integer
If index < arStr.Length Then
Label1.Text &= arStr(index) & Environment.NewLine
index += 1
End If
End Sub
With asynchronous approach you can display array values one after another without BackgroundWorker and explicitly created Timer.
Private _values As New String() From {"so", "nb", "de", "rty", "dcds"}
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each value In _values
await Task.Delay(2000)
Next
End Sub
With asynchronous approach you can prevent button click before all values are displayed in the simple way as you would do in synchronous code.
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim button = DirectCast(sender, Button);
button.Enabled = False
For Each value In _values
await Task.Delay(2000)
Next
button.Enabled = True
End Sub

How Do I total all the variables? in Vb.net

How do I add all the variables in VB.net? I do what I was instructed to increment it by placing it outside. Now that I have done it whenever I press the button the output is only 95 and whenever I click multiple times on the picturebox with the incrementation the output is still the same from last time. Nothing is really happening
Private BibimbapQuantity = 1
Private BulgogiQuantity = 1
Private BibimbapPrice As Integer = 45 * BibimbapQuantity
Private BulgogiPrice As Integer = 50 * BulgogiQuantity
Private TotalPriceInt As Integer = BibimbapPrice + BulgogiPrice
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles buttonBibimbap.Click
BibimbapQuantity += 1
Me.DataGridView2.Rows.Add("Bibimbap", "1", BibimbapPrice)
End Sub
Private Sub buttonBulgogi_Click(sender As Object, e As EventArgs) Handles buttonBulgogi.Click
BulgogiQuantity += 1
Me.DataGridView2.Rows.Add("Bulgogi", "1", BulgogiPrice)
End Sub
Private Sub totalPrice_Click(sender As Object, e As EventArgs) Handles totalPrice.Click
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
totalPrice.Text = TotalPriceInt
End Sub
You might've written the formula at the top but it's only executed once. You need to execute the formula every time.
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles buttonBibimbap.Click
BibimbapQuantity += 1
BibimbapPrice = 45 * BibimbapQuantity ' <------
Me.DataGridView2.Rows.Add("Bibimbap", "1", BibimbapPrice)
End Sub
You'll need to do the same with the total.
When you are done, put your code in https://codereview.stackexchange.com/ they will help you a lot with refactoring it. Only post on codereview when the code is actually working. They will help you find better way of writing your logic and find possible problems. Great learning opportunity.

How can I make a button move up in size (by going vertically up), but not in total size?

I'm having a slight conflict with a button which I've been working with in Visual Basic NET.
My first code sample is for my Button_Height_Tick, which controls changing the button's height:
Dim ChangeHeight As Boolean = False
Private Sub Button_Height_Tick(sender As Object, e As EventArgs) Handles Button_Height.Tick
If Not ChangeHeight Then
Do Until FlatButton1.Height = 63
FlatButton1.Height += 1
System.Threading.Thread.Sleep(1)
Loop
ChangeHeight = True
Else
End If
End Sub
And for my FlatButton1_MouseHover.
Private Sub FlatButton1_MouseHover(sender As Object, e As EventArgs) Handles FlatButton1.MouseHover
Button_Height.Enabled = True
Button_Height.Start()
End Sub
Now, as you can see in the Button_Height_Tick sub, the code changes the height of the button to 63, however, when this code is ran, the buttons total height is changed.
Here are some photos in-case I haven't explained it well.
What my original button looks like
What I want it to do
What it's doing (going up in size vertically going down, when I want it to go up)
Please comment below if you don't understand this question.
You need to change the 'Top' position and also I notice you have a timer then just go in a do a loop. In your example there's no need for a timer.
I'll give an example using a timer and hopefully you'll understand it and can use it for what you want. I've changed 'hover' to 'enter' and 'leave'.
If it's too slow just change the increment amount.
Dim ChangeHeight As Boolean = False
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If ChangeHeight Then
FlatButton1.Height += 2
FlatButton1.Top -= 2
If FlatButton1.Height < 63 Then Exit Sub
FlatButton1.Height = 63
Timer1.Enabled = False
Else
FlatButton1.Height -= 2
FlatButton1.Top += 2
If FlatButton1.Height > 31 Then Exit Sub
FlatButton1.Height = 31
Timer1.Enabled = False
End If
End Sub
Private Sub FlatButton1_MouseEnter(sender As Object, e As EventArgs) Handles FlatButton1.MouseEnter
ChangeHeight = True
If Timer1.Enabled Then Exit Sub
Timer1.Enabled = True
Timer1.Start()
End Sub
Private Sub FlatButton1_MouseLeave(sender As Object, e As EventArgs) Handles FlatButton1.MouseLeave
ChangeHeight = False
If Timer1.Enabled Then Exit Sub
Timer1.Enabled = True
Timer1.Start()
End Sub
Hello and welcome to StackOverflow. I did a little example of how to achieve what you are looking for.
Code:
Public Class Form1
Dim buttonXCoordinate As Integer
Dim buttonYCoordinate As Integer
Dim buttonOriginalHeight As Integer
Dim buttonOriginalLocation As Point
Private Sub GetButtonCoordinate()
buttonXCoordinate = testBtn.Left
buttonYCoordinate = testBtn.Top
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
buttonOriginalHeight = testBtn.Height
buttonOriginalLocation = testBtn.Location
GetButtonCoordinate()
End Sub
Private Sub testBtn_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles testBtn.MouseEnter
Dim buttonLocation As Point = Nothing
GetButtonCoordinate()
buttonLocation.X += buttonXCoordinate
buttonLocation.Y += buttonYCoordinate - buttonOriginalHeight
testBtn.Height += buttonOriginalHeight
testBtn.Location = buttonLocation
End Sub
Private Sub testBtn_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles testBtn.MouseLeave
testBtn.Height = buttonOriginalHeight
testBtn.Location = buttonOriginalLocation
End Sub
End Class
I did it really fast but it's enough to give you an idea to how to achive your goal.
In my example there is a button called testBtn, when you go over it with the mouse it the button's height is increased and it returns back to normal when you move your mouse out of it

read line by line from richtextbox and show in label (vb.net)

I would like to read line by line from richtextbox and show each line every a second in label.
I have this code blocks.
and I think I need a timer but I couldnt make it.
can you help me ?
Remarks :
If I use this code , I can only see the last line in label.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim RotateCount As String()
For i As Integer = 0 To RichTextBox1.Lines.Length - 1
Label1.Text = RichTextBox1.Lines(i)
Next
End Sub
I mean, assume that we have lines in richtextbox like..
a1
b2
c3
d4
e5
and I would like to show label1 in for each second like..
a1
(after 1 sec.)
b2
(after 1 sec.)
c3
(after 1 sec.)
like this...
You seems to expect that, because you set the Text property, the label repaints itself immediately with the new text. This doesn't happen until you exit from the event handler and the system could repaint the label. Of course, with this code, only the last text is shown.
To reach your goal, you could use a Timer set to 1 second interval and a counter that keeps track of the current line dispayed:
Dim tm As System.Windows.Forms.Timer = new System.Windows.Forms.Timer()
Dim counter As Integer = 0
At this point your button click just start the timer and exits
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
tm.Interval = 1000
AddHandler tm.Tick, AddressOf onTick
tm.Start()
' Don't allow to click again this button until
' the timer is stopped
Button1.Enabled = False
Button2.Enabled = True
End Sub
When the Tick event is raised you change the label text to the line indexed by the counter, increment it and check if you have reached the last line restarting from the first one if this is the case. Note that the button is disabled before exiting. This is required to avoid a second/third/fourth/etc click on the same button while the timer runs..... More on Button2 later....
Sub onTick(sender as Object, e as EventArgs)
Label1.Text = RichTextBox1.Lines(counter)
counter += 1
if counter >= RichTextBox1.Lines.Count Then
counter = 0
End If
End Sub
Of course, now you need another button to stop the Timer run and reenable the first button
' This button stops the timer and reenable the first button disabling
' itself - It should start as disabled from the form-designer
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
tm.Stop
RemoveHandler tm.Tick, AddressOf onTick
Button1.Enabled = True
Button2.Enabled = False
End Sub
You're almost there. Your problem is that you keep setting the text, not adding to it. Label1.Text = ... sets the text, if you want to keep adding to it you'd use Label1.Text &= ...
Also note that you need to include something like Environment.NewLine in order to include line breaks.
For i As Integer = 0 To RichTextBox1.Lines.Length - 1
Label1.Text &= RichTextBox1.Lines(i) & If(i < RichTextBox1.Lines.Length - 1, Environment.NewLine, "")
Next
thank you for your help !!!
I solved with this code ;
Public Class Form1
Dim tm = New System.Windows.Forms.Timer()
Dim counter As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Sub onTick(sender As Object, e As EventArgs)
Label1.Text = RichTextBox1.Lines(counter)
counter += 1
If counter >= RichTextBox1.Lines.Count Then
counter = 0
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For i = 0 To RichTextBox2.Lines.Count - 1
TextBox1.Text = RichTextBox2.Lines(i)
wait(2000)
Next
End Sub
Private Sub wait(ByVal interval As Integer)
Dim sw As New Stopwatch
sw.Start()
Do While sw.ElapsedMilliseconds < interval
' Allows UI to remain responsive
Application.DoEvents()
Loop
sw.Stop()
End Sub
End Class
It is very simple.
Declare one more string variable and load all string to this variable .
Improved code is given below.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As system.EventArgs) Handles Button1.Click
Dim a1 as int32
a1=0
'get number of lines of the rich text box content
a1=RichTextBox1.Lines.Count()
Dim str As String
For i As Int32 = 0 To a1-1
str = str + RichTextBox1.Lines(i)
Label1.Text= str
Next
End Sub

Panel Location Sliding Effect in VB.NET

what's wrong in my code? my syntax is right, the logic and concept is right.
but when i run the program and pressed the button(that make the panel slides UPWARD) the panel doesn't stop. even though I put a condition inside the timer.
here's the code;
Private Sub btn_existing_Click(sender As Object, e As EventArgs) Handles btn_existing.Click
timerPanelSlider3.Start()
pnl_info1.Enabled = False
pnl_options.Enabled = False
End Sub
Private Sub timerPanelSlider3_Tick(sender As Object, e As EventArgs) Handles timerPanelSlider3.Tick
yy -= 5
pnl_existing.Location = New Point(4, yy)
'pnl_existing.BringToFront()
If pnl_existing.Location.Y = 225 Then
timerPanelSlider3.Stop()
End If
End Sub
Your Location.Y is not hitting 225 you can check if it gets below 225. Or change it's starting position to 625.
Private Sub btn_existing_Click(sender As Object, e As EventArgs) Handles btn_existing.Click
timerPanelSlider3.Start()
pnl_info1.Enabled = False
pnl_options.Enabled = False
End Sub
Private Sub timerPanelSlider3_Tick(sender As Object, e As EventArgs) Handles timerPanelSlider3.Tick
yy -= 5
pnl_existing.Location = New Point(4, yy)
'pnl_existing.BringToFront()
If pnl_existing.Location.Y < 225 Then
timerPanelSlider3.Stop()
End If
End Sub