|
-
Feb 18th, 2013, 07:49 AM
#1
[RESOLVED] Label does not refresh on load
I'm trying to help someone on another forum it I can't see what the issue is here. The text changed event fires on the form load but the Label4 does not refresh. Label4.text is set at "Start" in the form design. TextBox2_TextChanged fires on the form load, it is set a "Start" in the form design, but the label stays at "Start". I'm guessing it is because the form isn't loaded yet. I tried doEvents and Label4.Refresh and that doesn't work. Anyone know why Label4 does not display "4444444444444"?
Code:
Public Class Form1
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
TextBox2.Text = "222222222222"
Label4.Text = "4444444444444"
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Application.DoEvents()
Label4.Refresh()
End Sub
End Class
-
Feb 18th, 2013, 08:58 AM
#2
Re: Label does not refresh on load
Does it not strike you as wrong to be changing the Text of TextBox2 in the TextChanged event handler of TextBox2? What is the actual aim here because that code is obviously not useful as it is? What real-world behaviour is desired here?
-
Feb 18th, 2013, 09:03 AM
#3
Re: Label does not refresh on load
I don't know what the actual aim is. I'm trying to answer the question\behavior for someone else. It seems like the label should update but since the form isn't even loaded I think that is why. It is just an anomaly, I'm using that term loosely, that I wondered about. This isn't a "real life" issue, just a why doesn't the label populate question.
-
Feb 18th, 2013, 09:15 AM
#4
Re: Label does not refresh on load
Calling DoEvents in the Load event handler is pointless and calling Refresh on any control is pointless unless you're using GDI+ to draw on it. The fact that that code is changing the Text of a control in its own TextChanged event handler is diabolical so, to be honest, it's not a problem worth solving.
-
Feb 18th, 2013, 09:36 AM
#5
Re: Label does not refresh on load
 Originally Posted by jmcilhinney
Calling DoEvents in the Load event handler is pointless and calling Refresh on any control is pointless unless you're using GDI+ to draw on it. The fact that that code is changing the Text of a control in its own TextChanged event handler is diabolical so, to be honest, it's not a problem worth solving.
There is some truth to that but I'm still curious. Maybe someone that really knows the nuts and bolts of VB .Net can answer (I'm absolutely not saying you don't, your expertise is impressive I see it's not worth looking at to you).
I did notice in this version:
Code:
Public Class Form1
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
TextBox2.Text = "222222222222"
Label4.Text = "4444444444444"
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Label4_TextChanged(sender As Object, e As EventArgs) Handles Label4.TextChanged
Debug.Print(Label4.Text)
End Sub
End Class
When the Label4 is set to "4444444444444" the debug print displays that. Then right before the form load fires it hits again and in sender (sender As Object) the original value is there and it sets it back. So it is changing in the text change event but the old value is being put back on the label change event.
-
Feb 18th, 2013, 12:22 PM
#6
Hyperactive Member
Re: Label does not refresh on load
In your original code nothing will ever happen on the load event. As jmcilhinney has already pointed out, it's a pointless exercise. The obvious reason your code is doing nothing is because form_load will take place before Text2_Changed. However, you are asking form_load to refresh the label with the output you're expecting Text2_Changed to provide. I hope my answer confuses you, not to be horrible but for you to see how the logic of the original code is confusing the matter.
-
Feb 18th, 2013, 12:43 PM
#7
Re: Label does not refresh on load
TextChanged fires when text is changed. Text isn't changed. It's actually irrelevant what order FormLoad comes in. There's no instruction in it that changes text (actually there's no instruction in it that does anything but that is also irrelevant).
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Feb 18th, 2013, 12:44 PM
#8
Re: Label does not refresh on load
I don't consider being curious about it to be pointless but I certainly wouldn't pay anybody to research it 
What is appears to be happening is if you populate a label in the form design, which I do all the time, the label text changed event will fire when the form is loading. If you set up a label with nothing in it the event will not fire. So in the text change event the label is populated as expected and displays on the form. If there is text in the label at design time it fires again after the label was changed in the text change event but has stored the original value from the screen and that is what gets put on the form.
You can see that by using this code:
Code:
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
TextBox2.Text = "222222222222"
Label4.Text = "4444444444444"
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Label4_TextChanged(sender As Object, e As EventArgs) Handles Label4.TextChanged
Debug.Print(Label4.Text)
End Sub
Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click
End Sub
End Class
1. Run it with nothing in the label on the form and the label will be populated with what is loaded in the text change event.
2. On the form add some text in the label then run it. What is in that will overlay what was loaded in the text change event.
It seems like starnge behavior behavior to me but I don't hold a candle to many of the .Net programmers here. But...now I know what is happening on the surface.
-
Feb 18th, 2013, 12:53 PM
#9
Hyperactive Member
Re: Label does not refresh on load
Firstly what will happen regardless of what you put on the form at design time is loaded when the form loads. The reason the label is not populating with the "44444444444" is because in your text2_textchanged is you're changing the contents of text2. So as soon as the user triggers that event by putting some text in text2 it changes text2's output, as soon as that happens it will trigger the text2_textchanged event again, and again and again. The reason it's not refreshing label4's output is because it is getting stuck before reaching that line of code.
To test what I mean take the text2.text code out of the text2_textchanged event and see what happens to label4's output
-
Feb 18th, 2013, 12:55 PM
#10
Re: Label does not refresh on load
Your Form1.Designer Code will contain something like:
Code:
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(34, 217)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(100, 22)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "start"
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(-3, 113)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(51, 17)
Me.Label1.TabIndex = 1
Me.Label1.Text = "start"
'
'Form1
This code is run before your Form_Load event handler code is run.
Code execution will hit the line in red. This will fire the TextBox1.TextChanged event and its event handler code will execute. Here you set the Label's Text to "4444444444444".
Execution returns to the above code and the line in green is executed. This sets the Label's Text back to the value you assigned in the Designer, i.e. "start".
If you'd added the Label to the Form before the TextBox, you probably wouldn't have noticed a problem.
If you clear the Label's Text in the Designer, the above code becomes
Code:
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(34, 217)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(100, 22)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "start"
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(-3, 113)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(51, 17)
Me.Label1.TabIndex = 1
'
'Form1
Notice the green line has disappeared ... the Label's Text never gets rewritten.
-
Feb 18th, 2013, 12:59 PM
#11
Re: Label does not refresh on load
I tried it here and it did exactly as I expected. With no breakpoints the form loaded up and displayed the 4s in the label and the 2s in the textbox
With breakpoints you could see that the changed event fired twice once from the other. so the actual execution order was
TextBox2.Text = "222222222222"
TextBox2.Text = "222222222222"
Label4.Text = "4444444444444"
Label4.Text = "4444444444444"
You should not use the change event in this way
btw I tested in VB 2008
-
Feb 18th, 2013, 01:13 PM
#12
Re: Label does not refresh on load
Heed the Note in the code provided by inferrd.
-
Feb 18th, 2013, 01:15 PM
#13
Re: Label does not refresh on load
Great explanation inferrd...I should have been looking there. Thanks!
Just to reiterate...this is not my code. Someone on another post was asking and I was trying to help. When I couldn't figure it out I came "home" to here. This is indeed the best forum that I'm aware of.
Last edited by TysonLPrice; Feb 18th, 2013 at 01:21 PM.
-
Feb 18th, 2013, 01:16 PM
#14
Re: Label does not refresh on load
What? Geez, it's really not complicated!
The default value of the text property in all controls is either "" or the Control name. If you set it to something else at design time then when the control is initialised there is a change of text, firing the TextChanged event. If you leave it as it is then there is no change and no event.
If you have code in place for the TextChanged event of two controls and there is actually a change of text there is no way to predict the order in which the events fire so what appears to be one value overlaying another could just as easily end up being another overlaying one! Linking values of text in two controls according to each other's TextChanged event is just loony! At best it's pointless, at worst it's an infinite loop waiting to happen.
Edit: I see Inferrd types faster than me. Oh well!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Feb 18th, 2013, 01:20 PM
#15
Re: Label does not refresh on load
Yes very good info in post #10, my controls were likely added in a different order so that problem did not appear in my test.
I wonder why anyone would write such code in changed event. The textbox would not be changeable at all since every keystroke would fire the changed event twice and set the text back to a string of 2s would be better to just use a label and set the text in the designer or at startup and have no code in the event.
-
Feb 18th, 2013, 01:29 PM
#16
Re: [RESOLVED] Label does not refresh on load
-
Feb 18th, 2013, 02:06 PM
#17
Hyperactive Member
Re: [RESOLVED] Label does not refresh on load
 Originally Posted by TysonLPrice
You know why you got everybody confused when you posted your question don't you? When you tried to answer the other thread's OP you changed his code.
Here is the original question
greathope123 (Programmer)
18 Feb 13 3:58
Hi,
When start my VB.Net program, the behaviou of Textbox and Lablel are different:
Code:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
TextBox2.Text = "222222222222"
Label4.Text = "4444444444444"
End Sub
TextBox2 will show "222222222222" but Label4 wont show anything!
What I am doing wrong?
The original OP wasn't putting his code in the TextBox2_TextChanged, he was putting it in TextBox1_TextChanged!!
-
Feb 18th, 2013, 02:38 PM
#18
Re: [RESOLVED] Label does not refresh on load
 Originally Posted by norman_bates
You know why you got everybody confused when you posted your question don't you? When you tried to answer the other thread's OP you changed his code.
Here is the original question
The original OP wasn't putting his code in the TextBox2_TextChanged, he was putting it in TextBox1_TextChanged!!
I see what you mean by different and I did blow that but I don't see how that confused anyone, they didn't know about it then. The behavior is the same using the OP's scenario and inferred's explanation is actually what is happening in both cases.
On Tek-Tips the text in Texbox1 fires the change event. Textbox2 will get values because it is empty. Label4 will populate or not based on if it has a value. Textbox2 behaves the same way in that if it has text at design time it will not load either.
So the reason for the behavior is what inferred posted and was arrived at from my post.
All the same I did translate the issue from one post to the other incorrectly
-
Feb 18th, 2013, 02:45 PM
#19
Hyperactive Member
Re: [RESOLVED] Label does not refresh on load
 Originally Posted by TysonLPrice
I see what you mean by different and I did blow that but I don't see how that confused anyone, they didn't know about it then. The behavior is the same using the OP's scenario and inferred's explanation is actually what is happening in both cases.
On Tek-Tips the text in Texbox1 fires the change event. Textbox2 will get values because it is empty. Label4 will populate or not based on if it has a value. Textbox2 behaves the same way in that if it has text at design time it will not load either.
So the reason for the behavior is what inferred posted and was arrived at from my post.
All the same I did translate the issue from one post to the other incorrectly 
If you look at your OP you will see that you made the same mistake again. What you was inferring with your code was that you wanted to change the text of text2 and label4 in the text2_textchanged event. That would create an infinite loop. That is why it was questioned as to why you were changing the text of a textbox inside its own text_change event.
The question now makes sense after seeing the other forum post.
Just another thing, as mentioned above, if the label was put on the form first then the problem wouldn't have occurred. However there is a way to get around it without having to empty the contents of label4
-
Feb 18th, 2013, 02:55 PM
#20
Re: [RESOLVED] Label does not refresh on load
 Originally Posted by norman_bates
If you look at your OP you will see that you made the same mistake again. What you was inferring with your code was that you wanted to change the text of text2 and label4 in the text2_textchanged event. That would create an infinite loop. That is why it was questioned as to why you were changing the text of a textbox inside its own text_change event.
The question now makes sense after seeing the other forum post.
Just another thing, as mentioned above, if the label was put on the form first then the problem wouldn't have occurred. However there is a way to get around it without having to empty the contents of label4
I'll just quit while I'm behind...
-
Feb 18th, 2013, 03:13 PM
#21
Re: [RESOLVED] Label does not refresh on load
 Originally Posted by norman_bates
If you look at your OP you will see that you made the same mistake again. What you was inferring with your code was that you wanted to change the text of text2 and label4 in the text2_textchanged event. That would create an infinite loop.
I would just like to point out that this is not actually the case.
The text_changed event would fire when the form loads. When it fires the code would change the text and cause it to fire again this time the code tries to change the text but it is not actually changing it because it is setting the text to what the text was already and the change even does not fire again.
So there is no infinite loop here just 1 repeated call to the change event when changing the text of the same box which fires the event.
You can easily confirm this by adding such code and setting a break point in the changed event. You should see it hit twice and then stop.
-
Feb 18th, 2013, 06:12 PM
#22
Hyperactive Member
Re: [RESOLVED] Label does not refresh on load
 Originally Posted by DataMiser
I would just like to point out that this is not actually the case.
The text_changed event would fire when the form loads. When it fires the code would change the text and cause it to fire again this time the code tries to change the text but it is not actually changing it because it is setting the text to what the text was already and the change even does not fire again.
So there is no infinite loop here just 1 repeated call to the change event when changing the text of the same box which fires the event.
You can easily confirm this by adding such code and setting a break point in the changed event. You should see it hit twice and then stop.
I've learnt something new
-
Feb 18th, 2013, 07:48 PM
#23
Member
Re: [RESOLVED] Label does not refresh on load
Hello,
I am the original person who asked this question. Thanks to TysonLPrice who helped me in the other forum and pointed me this forum.
(1) I would like to explain why I asked such question:
I use TextBox1 to display a folder name which can be changed when I double click TextBox1. When TextBox1_TextChanged, all files in the folder (with some condition) will be added to ComboBox1 and Label1 will display the total number of the files (Do you think it is a stupid code):
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
ComboBox1.Items.Clear()
Dim di As New IO.DirectoryInfo(TextBox1.Text)
Dim fi() As IO.FileInfo = di.GetFiles()
Dim file As IO.FileInfo, nFiles As Long
For Each file In fi
If IsNumeric(Replace(file.Name, ".txt", "")) Then
ComboBox1.Items.Add(file.Name)
nFiles = nFiles + 1
End If
Next
di = Nothing
Label1.Text = "nFiles = " & nFiles
End Sub
Private Sub TextBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
FolderBrowserDialog1.SelectedPath = TextBox1.Text
FolderBrowserDialog1.ShowDialog()
TextBox1.Text = FolderBrowserDialog1.SelectedPath()
End Sub
Above code was working when I double clicked TextBox1. When the program started, TextBox1_TextChanged() also fired but Label1.Text keeped the default "Label1" instead of the total number of the files.
(2) I don't know where to find Form1.Designer Code mentioned by Inferrd. Can Inferrd tell me?
(3) A guess for any control with Text property:
If we or VB.Net set a non-empty string for the Text property of a control in design time, after the Class Form constructed, the control will show this string and ignore the new setting for it in TextBox1_TextChanged().
If we or VB.Net set an empty string for the Text property of a control in design time, after the Class Form constructed, the control will show the new setting for it in TextBox1_TextChanged().
-
Feb 18th, 2013, 08:22 PM
#24
Re: [RESOLVED] Label does not refresh on load
 Originally Posted by wwhelp
Hello,
I am the original person who asked this question. Thanks to TysonLPrice who helped me in the other forum and pointed me this forum.
(1) I would like to explain why I asked such question:
I use TextBox1 to display a folder name which can be changed when I double click TextBox1. When TextBox1_TextChanged, all files in the folder (with some condition) will be added to ComboBox1 and Label1 will display the total number of the files (Do you think it is a stupid code):
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
ComboBox1.Items.Clear()
Dim di As New IO.DirectoryInfo(TextBox1.Text)
Dim fi() As IO.FileInfo = di.GetFiles()
Dim file As IO.FileInfo, nFiles As Long
For Each file In fi
If IsNumeric(Replace(file.Name, ".txt", "")) Then
ComboBox1.Items.Add(file.Name)
nFiles = nFiles + 1
End If
Next
di = Nothing
Label1.Text = "nFiles = " & nFiles
End Sub
Private Sub TextBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
FolderBrowserDialog1.SelectedPath = TextBox1.Text
FolderBrowserDialog1.ShowDialog()
TextBox1.Text = FolderBrowserDialog1.SelectedPath()
End Sub
Above code was working when I double clicked TextBox1. When the program started, TextBox1_TextChanged() also fired but Label1.Text keeped the default "Label1" instead of the total number of the files.
(2) I don't know where to find Form1.Designer Code mentioned by Inferrd. Can Inferrd tell me?
(3) A guess for any control with Text property:
If we or VB.Net set a non-empty string for the Text property of a control in design time, after the Class Form constructed, the control will show this string and ignore the new setting for it in TextBox1_TextChanged().
If we or VB.Net set an empty string for the Text property of a control in design time, after the Class Form constructed, the control will show the new setting for it in TextBox1_TextChanged().
There is no ignoring going on. The simple fact is that, if the Text property of a control is set in the designer then that property is going to be set in code during the form constructor. Exactly what that means depends on the order you added the controls. If you added the TextBox first then its Text will get set first, so it's TextChanged event will be handled before the Text of the Label is set so your change in the event handler is overwritten. If you added the Label first then the Label's Text gets set first, then the TextBox's Text gets set and so your change in the event handler is done last, so you will see it when the form is displayed.
Now that we know the actual circumstances it is obvious that there are two big mistakes:
1. Why leave the default "Label1" value in the Text of the Label? At no point do you ever want that value displayed so that value should have been cleared from the outset. If there was no value for the Text of the Label in the designer then there never would have been an issue in the first place.
2. You should not be handling the TextChanged event of the TextBox. You are setting the file path in the DoubleClick event handler of the TextBox and that's where you should be setting the Label Text too. That way you know that you'll only ever get an event when the user changes the path. If you want to do the same thing when the form loads first up then that's what the Load event handler of the form is for.
Apart from that, it's also very bad that you have controls named TextBox1, Label1, ComboBox1, etc. All those controls have a purpose so give them names that describe those purposes. It takes very little effort to give all your variables meaningful names but it makes a big difference to anyone reading your code, including yourself.
-
Feb 19th, 2013, 05:50 AM
#25
Member
Re: [RESOLVED] Label does not refresh on load
Thanks a lot for your explaination. Things are more complex than what I thought.
Last edited by wwhelp; Feb 19th, 2013 at 06:20 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|