Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2008 > Visual Basic 2008 Professionals
|
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
 
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!
 
Old August 22nd, 2010, 06:40 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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!
 
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.
 
Old August 23rd, 2010, 05:43 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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!
 
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!
 
Old August 23rd, 2010, 08:18 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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!
 
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!!!
 
Old August 24th, 2010, 05:12 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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!





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 10:06 PM
capturing form closed events alexferrie C# 2 June 17th, 2003 10:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.