|
-
Oct 28th, 2004, 05:54 AM
#1
Thread Starter
Addicted Member
VB.NET: Using String to compare with the Combo Box...[Resolved]
Hi...
Say i have this string call "data" in Form1, this string contains number "5" value....
how do i pass this string to the Form2 and compare with the combo box items...
The combo box DropDownStyle is set to DropDownList...
which means when i pass, the SelectedIndex must be subtract or add by 1 to get the correct items to display...
The Combobox items contain:
One, Two, Three, Four and Five...
How to i actually pass this string to Form2 and Form2 will compare the number 5 with the items and display Five instead in the combo box correctly..
Thanks
Last edited by toytoy; Oct 28th, 2004 at 10:17 AM.
-
Oct 28th, 2004, 08:35 AM
#2
is form2 already open when you make this comparison? or do you have data=5 already, and then you open form2 and want to select the value from the combo?
-
Oct 28th, 2004, 08:57 AM
#3
Thread Starter
Addicted Member
No....Form2 will be open only after i click on another button in Form1..
data "5" is already in the string and is waiting to be passed to Form2....
The "5" will be comapre accordingly with the items in one of the combobox in Form2.....
then the combox box in Form2 will display accordingly to the compare items...In this case which is the "Five" in the items instead of "5"..
That means the combo box will be able to check the items with the string and display accordingly...
Thanks
Last edited by toytoy; Oct 28th, 2004 at 09:02 AM.
-
Oct 28th, 2004, 09:28 AM
#4
create an additional constructor for the second form and pass in the value
for example, every form in your project has a Sub New, but its in that region of "Form Designer Generated Code" you see in code view...
make your own Sub New with a param... but don't forget to call the other Sub New so the base class is constructed as well.. here is an example
VB Code:
'IN FORM2
'MAKE A PRIVATE VARIABLE AT THE TOP TO HOLD THE VALUE
Dim mstrNumber as string = string.empty
'NOW MAKE A SUB NEW
Public Sub New(ByVal strMyVal as String)
me.New 'this will call the forms default constructor (and base class)
mstrNumber = strMyVal 'this will put your value in the variable
End Sub
see now in form2 you will have access to mstrNumber which will contain the passed in value
in form1 you pass it by doing this
VB Code:
Dim MyForm2 as New Form2(data)
'where data is the variable name that contains "5"
-
Oct 28th, 2004, 09:42 AM
#5
Thread Starter
Addicted Member
Hmm....I still don't get it...
Now the problem is say I pass "5" from Form1 to Form2...
the combo box will display six instead of five...
I used some method in between to check if "5" is equal to Five..
Inside combo box items:
One
Two
Three
Four
Five
six
My codng:
Code:
' FORM 1
Dim two As New Form2
Dim No As String
'
'
'
No = ' Some number ....in this case is the "5" value
two.cboNumber.SelectedIndex = No
two.Show()
I cannot used:
Code:
two.cboNumber.SelectedIndex - 1 = No
It gives an error....
Last edited by toytoy; Dec 3rd, 2004 at 07:48 AM.
-
Oct 28th, 2004, 09:48 AM
#6
then pass in an integer instead of a string... and use the int as the index...
VB Code:
'IN FORM2
'MAKE A PRIVATE VARIABLE AT THE TOP TO HOLD THE VALUE
Dim mintNumber as Integer = 0
'NOW MAKE A SUB NEW
Public Sub New(ByVal intMyVal as Integer)
me.New 'this will call the forms default constructor (and base class)
mintNumber = intMyVal 'this will put your value in the variable
End Sub
two.cboNumber.SelectedIndex = mintNumber - 1
-
Oct 28th, 2004, 10:01 AM
#7
Thread Starter
Addicted Member
But i only give one example for the combo box...
Say i have 10 combox boxes...
Is it mean that I have to do it 10 times in the sub New()...
Secondly, I thought this coding is for Form1...
Code:
two.cboNumber.SelectedIndex = mintNumber - 1
because I passed it in from form1 not form2..
Last edited by toytoy; Dec 3rd, 2004 at 07:48 AM.
-
Oct 28th, 2004, 10:13 AM
#8
Thread Starter
Addicted Member
oh.....I solve it..
I found out with this code:
Code:
' FORM 1
two.cboNumber.SelectedIndex = CInt(No) - 1
Anyway, kleinma can you explain deeper for your solution
I want to learnt new things...
I have never done it before in the Sub New()
Thanks
Last edited by toytoy; Dec 3rd, 2004 at 07:49 AM.
-
Oct 28th, 2004, 10:37 AM
#9
Thread Starter
Addicted Member
oh...kleinma
I figure it out what you means...
Last edited by toytoy; Oct 28th, 2004 at 10:46 AM.
-
Oct 28th, 2004, 10:39 AM
#10
its just a way to pass parameters from one form to another without having to directly access a form from another form
lets say you have 3 forms
form1, form2, and form3
form3 is a popup form that might be used in both form1 and form2
instead of hard coding links between the forms controls like you have done, you can have form3 accept a parameter in its constructor (the Sub New) so that form3 can act independant of both form1 and form2, IE it makes it much more reusable... it kind of uses more encapsulation so that you are not worrying about what the objects on form3 are called when you are working in form1 or form2, you just know you can pass a parameter to form3 when you create it, and it will work how you need it to...
-
Oct 28th, 2004, 10:47 AM
#11
Thread Starter
Addicted Member
but...
What should i do in Form1 to pass to Form2 for your coding since the cboNumber and Sub New() are all declare in Form2...
How do i create that pass to Form2 from Form1...
Last edited by toytoy; Oct 28th, 2004 at 10:53 AM.
-
Oct 28th, 2004, 10:56 AM
#12
you do it when you create the variable that IS form2
for example
this is the code in form1
VB Code:
Dim MyForm2 as New Form2(VariableIAmPassing)
MyForm2.ShowDialog
this is the code in form2
VB Code:
Private mMyVariable as string
Public Sub New(ByVal PassedVariable as string)
Me.New()
mMyVariable = PassedVariable
End Sub
so now when you do this.. if you do
Dim MyForm2 asn new Form2.... you will get 2 options in intellisense for the New constructor.. you will get the default New() that accepts no params, and you will get the one you made New(PassedVariable as string) and you can use either, depending on the situation... it is called overloading, when you have 2 subs with the same name but different params...
so you can still create form2 without passing a variable, or you can create it WITH passing a variable.. you lose no functionality.. only gain..
-
Oct 28th, 2004, 11:27 AM
#13
Thread Starter
Addicted Member
ok...
I have learnt new things..
How a wonderful day..
Thanks
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
|