Results 1 to 13 of 13

Thread: VB.NET: Using String to compare with the Combo Box...[Resolved]

  1. #1

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230

    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.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    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?

  3. #3

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    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.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    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:
    1. 'IN FORM2
    2.  
    3. 'MAKE A PRIVATE VARIABLE AT THE TOP TO HOLD THE VALUE
    4. Dim mstrNumber as string = string.empty
    5.  
    6. 'NOW MAKE A SUB NEW
    7. Public Sub New(ByVal strMyVal as String)
    8.     me.New 'this will call the forms default constructor (and base class)
    9.     mstrNumber = strMyVal 'this will put your value in the variable
    10. 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:
    1. Dim MyForm2 as New Form2(data)
    2. 'where data is the variable name that contains "5"

  5. #5

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    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.

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    then pass in an integer instead of a string... and use the int as the index...
    VB Code:
    1. 'IN FORM2
    2.  
    3. 'MAKE A PRIVATE VARIABLE AT THE TOP TO HOLD THE VALUE
    4. Dim mintNumber as Integer = 0
    5.  
    6. 'NOW MAKE A SUB NEW
    7. Public Sub New(ByVal intMyVal as Integer)
    8.     me.New 'this will call the forms default constructor (and base class)
    9.     mintNumber = intMyVal 'this will put your value in the variable
    10. End Sub
    11.  
    12. two.cboNumber.SelectedIndex = mintNumber - 1

  7. #7

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    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.

  8. #8

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    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.

  9. #9

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    oh...kleinma

    I figure it out what you means...
    Last edited by toytoy; Oct 28th, 2004 at 10:46 AM.

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    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...

  11. #11

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    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.

  12. #12
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    you do it when you create the variable that IS form2

    for example

    this is the code in form1
    VB Code:
    1. Dim MyForm2 as New Form2(VariableIAmPassing)
    2. MyForm2.ShowDialog
    this is the code in form2
    VB Code:
    1. Private mMyVariable as string
    2.  
    3. Public Sub New(ByVal PassedVariable as string)
    4.     Me.New()
    5.     mMyVariable = PassedVariable
    6. 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..

  13. #13

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    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
  •  



Click Here to Expand Forum to Full Width