Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2008 > Visual Basic 2008 Professionals
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 22nd, 2010, 04:02 AM
Authorized User
 
Join Date: Aug 2010
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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!
Reply With Quote
  #2 (permalink)  
Old August 22nd, 2010, 06:40 AM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #3 (permalink)  
Old August 22nd, 2010, 10:35 PM
Authorized User
 
Join Date: Aug 2010
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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.
Reply With Quote
  #4 (permalink)  
Old August 23rd, 2010, 05:43 AM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #5 (permalink)  
Old August 23rd, 2010, 08:07 AM
Authorized User
 
Join Date: Aug 2010
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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!
Reply With Quote
  #6 (permalink)  
Old August 23rd, 2010, 08:18 AM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #7 (permalink)  
Old August 23rd, 2010, 10:22 PM
Authorized User
 
Join Date: Aug 2010
Posts: 19
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Imar View Post
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!!!
Reply With Quote
  #8 (permalink)  
Old August 24th, 2010, 05:12 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to be able to re-initialize a form kscase Visual Basic 2005 Basics 14 May 10th, 2007 09:54 AM
Form events pavel VB How-To 3 April 22nd, 2004 07:06 AM
"Delete Events" in Form Do Not Execute tcarnahan Access VBA 3 November 12th, 2003 09:06 PM
capturing form closed events alexferrie C# 2 June 17th, 2003 10:53 AM



All times are GMT -4. The time now is 08:47 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.