 |
| Visual Basic 2008 Professionals For advanced Visual Basic coders working in version 2008. Beginning-level questions will be redirected to other forums, |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Visual Basic 2008 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 22nd, 2010, 04:02 AM
|
|
Authorized User
|
|
Join Date: Aug 2010
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
How to Initialize form without stimulating any events?
Hello.I got a sample below:
I got a form(name is form2) with a combobox(name is comboBox1) and two textboxes(textbox1 and textbox2) on it.I just want "100+200=300". But the code below can not run.
Code:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = 100
ComboBox1.Text = 200
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
TextBox2.Text = CSng(ComboBox1.Text) + CSng(TextBox1.Text)
End Sub
I know the cause is the event "ComboBox1_SelectedIndexChanged" has been stimulated when the form2 is initialized. Can anybody tell me what will i do?
Thanks for reading, and Thanks more for posting!
|
|

August 22nd, 2010, 06:40 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
It should work fine when the combo box contains a number of predefined items.
When it's empty, then ComboBox1_SelectedIndexChanged won't fire because there is no change of index (of the selected item).
BTW: you're likely to get more attention to questions like this in the Essentials forum: http://p2p.wrox.com/visual-basic-2008-essentials-358/
Cheers,
Imar
|
|

August 22nd, 2010, 10:35 PM
|
|
Authorized User
|
|
Join Date: Aug 2010
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Thank you very much ,Imar
I think the problem is in textBox1.The code below can run.
Code:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Text = 200
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
TextBox2.Text = CSng(ComboBox1.Text) + CSng(ComboBox1.Text)
End Sub
The textBox1 is not initialized when it is quoted in the event ComboBox1_SelectedIndexChanged.But when I add textBox1 to the form2,I just assign 100 to it in the property panel. Really confused!
I get another way ,it works
Code:
Dim Text1 As Single
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Text = 200
Text1 = TextBox1.Text
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
TextBox2.Text = CSng(ComboBox1.Text) + Text1
End Sub
But I do not want to define an extra variant(It is Text1 here).Is there any other way ?
Thank you again
BTW: I love your books ,Best I ever read.
|
|

August 23rd, 2010, 05:43 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
The problem is: I am not sure what you're trying to accomplish. The simple code makes it difficult to see if this is eventually a real app, or that you're just learning VB. I also don't get the problem and don't see why you would need that variable.
Can you provide more details on what it is you're doing and why?
Cheers,
Imar
|
|

August 23rd, 2010, 08:07 AM
|
|
Authorized User
|
|
Join Date: Aug 2010
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Sorry about my poor description.My motherlanguage is not English.
It's a problem of my application.I write my application with vb6.0 before,now it is updated to vb2008 by my VS2008.The problem occured after updating.
I just want to click the ComboBox1 ,choose an item, add it to Textbox1.Text, and show the result in TextBox2.It's just an simple arithmetic Calculation,get the sum of two numbers.What I post here is just a simple sample to show the problem.
I add a form(form2),a comboBox(ComboBox1,Items are added in property panel),two TextBoxes(TextBox1 and TextBox2).Then double click the form2,add the code below:
Code:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Text = 200
TextBox1.Text = 100
End Sub
Then double click the ComboBox1,add the code below:
Code:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
TextBox2.Text = CSng(ComboBox1.Text) + CSng(TextBox1.Text)
End Sub
The press F5, an error occur:"Untreated InvalidCastException:From string "" to type "Single" is invalid"
Please take a try as what I said, It won't take your a minute.And you will see the problem yourself.
BTW:This problem is discriped in MSDN:ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="B530EFF2-3132-48F8-B8BC-D88AF543D321
Thank you very much for your time.Best wishes! 
|
|

August 23rd, 2010, 08:18 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
Sorry about my poor description.My motherlanguage is not English.
|
It's not mine either. ;-)
Anyway, I did try it before, and as I said it worked fine when I added some items to the DropDownList on the design surface. I didn't get an error; but SelectedIndexChanged din't fire at Load without any items.
Why do you have to do this in _Load? Why not add the items at Design Time? Alternatively you could assign them in an earlier event such as Init.
Finally, instead of the Text property, you can access the drop down's Items collection. Feels a bit more natural.
Cheers,
Imar
|
|

August 23rd, 2010, 10:22 PM
|
|
Authorized User
|
|
Join Date: Aug 2010
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by Imar
It's not mine either. ;-)
|
Well,nice to hear that then,haha.
I find the problem.I change the ComboBox1's"DropDownStyle" property to "DropDownList" at the property panel, and then "No Problem anymore"!Haha.The default one is "DropDown".But It does not work when the "DropDownStyle" property is set to "DropDown".I don't know why,my specialized field is not computer science.I am a civil engineer.
I use vb6.0 before,I just start to use vb2008,I am not familiar with vb2008.I find the solution under your guide,Thank you!!!
Is there an _Init event? I don't find it!
I prefer the code rather than the property panel.That's why I use code to assign the contrls.It's just a personal habit.
Appreciate you for your time!!! 
|
|

August 24th, 2010, 05:12 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
Is there an _Init event? I don't find it!
|
No, sorry. Thinking too much in terms of Web Forms.... ;-)
Glad to hear you got it working.
Cheers,
Imar
|
|
 |