PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Help (loading array into a ComboBox)-VBForums
Results 1 to 18 of 18

Thread: Help (loading array into a ComboBox)

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    12

    Help (loading array into a ComboBox)

    Hi,

    I have this array:
    Dim myarray() As String = {"Ann,9,1", "Len,James,5", "Kim,8,Jennifer", "San, 2,9"}

    How can I load only the first word of each element? For example I want my ComboBox to contain only those:
    Ann
    Len
    Kim
    San

    Using a While or Do loop, how can I do that? Thanks.

  2. #2
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Help (loading array into a ComboBox)

    You can do;

    ComboBox1.Items.AddRange(myarray)

    Sorry I misread the question. You can do;

    Code:
            For Each s As String In myarray
                Dim i As String() = s.Split(New Char() {","c})
                ComboBox1.Items.Add(i(0))
            Next
    Last edited by Bulldog; Mar 4th, 2015 at 06:21 PM.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    12

    Re: Help (loading array into a ComboBox)

    Quote Originally Posted by Bulldog View Post
    You can do;

    ComboBox1.Items.AddRange(myarray)
    Thanks! but that adds the whole array. How can I make it only display the first word before (,) of each element ?
    for example Ann instead of Ann,9,1 ?

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    12

    Re: Help (loading array into a ComboBox)

    it probably has something to do with "substring" command but I am not sure how to use it in this case

  5. #5
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Help (loading array into a ComboBox)

    I misread your question, see edit in post #2


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,335

    Re: Help (loading array into a ComboBox)

    vb Code:
    1. Me.ComboBox1.Items.AddRange(Me.myarray.Select(Function(n) n.Split(","c).First).ToArray)

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    12

    Re: Help (loading array into a ComboBox)

    Thank you so much!
    One more question please, if I wanted to perform calculations based on the third element:
    for example:
    If myarray(2) > 3 Then Msgbox("Good")
    Assuming 2 means the third element in my array, would that work?
    Because I don't want to write many IF statements. How to do that?

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,705

    Re: Help (loading array into a ComboBox)

    Quote Originally Posted by Aneesha1 View Post
    Thank you so much!
    One more question please, if I wanted to perform calculations based on the third element:
    for example:
    If myarray(2) > 3 Then Msgbox("Good")
    Assuming 2 means the third element in my array, would that work?
    Because I don't want to write many IF statements. How to do that?
    If you read the title of your thread, which describes the topic of the thread, you'll see that that question is not related and therefore does not belong in this thread. Please keep each thread to a single topic and each topic to a single thread.

    To address your question now that it's here though, the code you have provided can't work because it's comparing the String "Kim,8,Jennifer" with the number 3, which is basically meaningless. Please provide a FULL and CLEAR description of EXACTLY what it is that you want to achieve. Is it comparing the part between the two commas as a number to another number? Is it for all elements of the array or just one element? The more you make us guess, the more likely we are to guess wrongly.

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    12

    Re: Help (loading array into a ComboBox)

    I am so sorry! I didn't want to spam topics in the forum so I just wanted to ask in the same topic! Didn't know the rules, will make sure to follow next time.

    What I am asking about is:
    Suppose I have this array:
    Dim myarray() As String = {"Ann,9,1", "Len,James,5", "Kim,8,4", "San, 2,9", "Eren,3,2"}
    and I loaded the first field of each element into a ComboBox using the codes the members provided.
    Suppose the third field of each element is a maximum number for that element, the fields are "1,5,4,9,2"
    I also have a TextBox beside the ComboBox.
    What I want:
    If "Len", for example, was selected in the ComboBox, and the user wrote 6 in that TextBox, I want a MsgBox to appear because the user wrote a number that exceeds the limit of the third field of Lee's element, which is 5 according to "Lee's element.

    Sorry my English is not my first language, hopefully I made it clear. Thanks a lot!

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,705

    Re: Help (loading array into a ComboBox)

    Quote Originally Posted by Aneesha1 View Post
    I am so sorry! I didn't want to spam topics in the forum so I just wanted to ask in the same topic! Didn't know the rules, will make sure to follow next time.

    What I am asking about is:
    Suppose I have this array:
    Dim myarray() As String = {"Ann,9,1", "Len,James,5", "Kim,8,4", "San, 2,9", "Eren,3,2"}
    and I loaded the first field of each element into a ComboBox using the codes the members provided.
    Suppose the third field of each element is a maximum number for that element, the fields are "1,5,4,9,2"
    I also have a TextBox beside the ComboBox.
    What I want:
    If "Len", for example, was selected in the ComboBox, and the user wrote 6 in that TextBox, I want a MsgBox to appear because the user wrote a number that exceeds the limit of the third field of Lee's element, which is 5 according to "Lee's element.

    Sorry my English is not my first language, hopefully I made it clear. Thanks a lot!
    That's more clear. I think we need to clear up something else before proceeding though. Looking at the original data you posted:
    "Ann,9,1", "Len,James,5", "Kim,8,Jennifer", "San, 2,9"
    if we look at the parts of each element, the first and last elements contain text,number,number while the second is text,text,number and the third is text,number,text. Is the data really going to be that potentially inconsistent or have you represented it incorrectly? You have to treat data differently if it might not always be numeric.

  11. #11

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    12

    Re: Help (loading array into a ComboBox)

    Thanks for your response. That data is just an example, my array is like this: Text,number,number.

    Note: Although I do have some text in SECOND field, but the first and third are always text, number.
    Thanks again.

  12. #12

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    12

    Re: Help (loading array into a ComboBox)

    Please jmcilhinney help me with this problem!

  13. #13
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,705

    Re: Help (loading array into a ComboBox)

    There are basically two options here. You could break all the data up first and then use that broken up data for everything, or you can break up the specific data you need when the user makes a selection. I prefer the first option, particularly given that you're breaking the data up to load the ComboBox anyway. I'll explain that option and then, if you want to go the other way, i can explain that then.

    Firstly, you need a type that can store the three data items for a single record. You can define your own type or you can just use Tuples, which are general purpose types that can store an arbitrary number of data items of arbitrary types. I'll show that but, if you prefer, you can define a class with just the three specific properties you need. Once you have converted your array to a list of instances of your chosen type, you bind that to the ComboBox. You can expose the numeric value you want via the SelectedValue and validate the TextBox input against that, e.g.
    vb.net Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.     Dim myarray() As String = {"Ann,9,1", "Len,James,5", "Kim,Jennifer,8", "San, 2,9"}
    3.     Dim items = (From s In myarray
    4.                  Let parts = s.Split(","c)
    5.                  Select Tuple.Create(parts(0),
    6.                                      parts(1),
    7.                                      CInt(parts(2)))).ToArray()
    8.  
    9.     'Bind the split items to the ComboBox, displaying the first data
    10.     'part and exposing the third data part via the SelectedValue.
    11.     With ComboBox1
    12.         .DisplayMember = "Item1"
    13.         .ValueMember = "Item3"
    14.         .DataSource = items
    15.     End With
    16. End Sub
    17.  
    18. Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    19.     Dim number As Integer
    20.     Dim max = CInt(ComboBox1.SelectedValue)
    21.  
    22.     'Validate the entry against the selection.
    23.     If Not Integer.TryParse(TextBox1.Text, number) Or number > max Then
    24.         MessageBox.Show("Please enter a number not greater than " & max)
    25.         e.Cancel = True
    26.     End If
    27. End Sub

  14. #14

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    12

    Re: Help (loading array into a ComboBox)

    Oh my God, that couldn't be more helpful! Thank you so much!
    I also did not know that it would take some steps to achieve this at I am new to programming.
    Appreciate your help and valuable time jmcilhinney!

  15. #15
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,335

    Re: Help (loading array into a ComboBox)

    Johns example is miles away from what i thought you originally asked :/

  16. #16
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: Help (loading array into a ComboBox)

    Just one slight correction to JMC's code (actually not a true correct; see my Edit note below)... In the If-statement of the verification, it should use the short-circuiting OrElse:
    Code:
    If Not Integer.TryParse(TextBox1.Text, number) OrElse number > max Then
    With a traditional Or, the program will evaluate both sides to a boolean value, and then applies the Or to determine whether the entire condition is true. With the short-circuiting OrElse (as well as with AndAlso), the program evaluates the first part of the condition (before the OrElse) and if it can determine what the logic of the entire condition has to be based on that, it treats the entire condition that way without evaluating the 2nd part of the condition. Because the first part of the condition is trying to parse the user's input in the TextBox, if that parsing fails then we want to alert the user before attempting to use the "number" variable. So using OrElse will cause the program to continue inside the If-block to alert the user without checking the last part of the condition.

    Edit (ok not really a true edit since I caught myself before posting): I just realized that Or will also work... I forgot that declaring a value-type of variable (like Integer) will set the variable to a default instead of Nothing as would happen with reference-types. Because of that, you wouldn't necessarily have to short-circuit the condition in the If-statement since number = 0 if the parsing failed, and the 2nd part of the condition will still function properly. I was thinking more that without the OrElse, the program might toss an error trying to compare Nothing to an actual number.

  17. #17
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,705

    Re: Help (loading array into a ComboBox)

    Quote Originally Posted by ident View Post
    Johns example is miles away from what i thought you originally asked :/
    Read the first paragraph of post #8.

  18. #18
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,705

    Re: Help (loading array into a ComboBox)

    Quote Originally Posted by Pyth007 View Post
    Just one slight correction to JMC's code (actually not a true correct; see my Edit note below)... In the If-statement of the verification, it should use the short-circuiting OrElse:
    Code:
    If Not Integer.TryParse(TextBox1.Text, number) OrElse number > max Then
    With a traditional Or, the program will evaluate both sides to a boolean value, and then applies the Or to determine whether the entire condition is true. With the short-circuiting OrElse (as well as with AndAlso), the program evaluates the first part of the condition (before the OrElse) and if it can determine what the logic of the entire condition has to be based on that, it treats the entire condition that way without evaluating the 2nd part of the condition. Because the first part of the condition is trying to parse the user's input in the TextBox, if that parsing fails then we want to alert the user before attempting to use the "number" variable. So using OrElse will cause the program to continue inside the If-block to alert the user without checking the last part of the condition.

    Edit (ok not really a true edit since I caught myself before posting): I just realized that Or will also work... I forgot that declaring a value-type of variable (like Integer) will set the variable to a default instead of Nothing as would happen with reference-types. Because of that, you wouldn't necessarily have to short-circuit the condition in the If-statement since number = 0 if the parsing failed, and the 2nd part of the condition will still function properly. I was thinking more that without the OrElse, the program might toss an error trying to compare Nothing to an actual number.
    You're quite right. Whether or not Or would work, you should always use OrElse by default and only use Or if you specifically need to avoid short-circuiting. I always do so myself so it was only that I was writing that code in a hurry that I made the mistake. I appreciate the correction because I'd hate for my mistake to be the cause for others writing suboptimal code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width