What's with this split?

hog

Senior Contributor
Joined
Mar 17, 2003
Messages
984
Location
UK
Right I have a combo box which has entries like thie;

[3] DOE JOHN

I want to use spilt to extract the 3. My code below when using the combobox selecteditem method gives me a blank message box, but if I hard code the text I get the 3?

Am I missing something here?

Visual Basic:
Dim strString As String = Me.cboFullName.SelectedItem

'Dim strString As String = "[3] DOE JOHN"

Dim strDelimiterList As String = "[] "
Dim strDelimiter As Char() = strDelimiterList.ToCharArray()
Dim strSplit As String() = Nothing

strSplit = strString.Split(strDelimiter, 3)
        
MessageBox.Show(strSplit(1))
 
Your delimiter should be the right bracket ] , because you don't have any [] without numbers in between, do you? Then remove the left bracket once in the array.

Why not use DataValueField and DataTextField so that the Selected Item is the ID number?
 
OK thnx I'll look at this:)

Why not use DataValueField and DataTextField so that the Selected Item is the ID number?

The string in the combobox is a concatenation of database fields id, lastname and first name.

I'm doing it this way to extract the id from the string.

I build the combobox using the add items method but couldn't figure out how to include the id field after the concatenation and be able to return this as the users choice if you know what I mean??
 
Regular Expressions

I have to say, I would use a Regular Expression for this purpose. I've written a little example in C#, because I don't know VB; but converting back to VB should be easy enough for you. :)

[CS]
using System.Text.RegularExpressions;
using System.Windows.Forms;

class IDExtractor
{
static void Main()
{
string str = "[3] DOE JOHN";
Regex r = new Regex(@"\[(?<id>([0-9]+))\]");
Match m = r.Match(str);

if (m.Success)
MessageBox.Show(m.Groups["id"].Value, "ID");
}
}
[/CS]
 
This looks impressive, but I don't understand it:(

Can anyone put it into VB??
 
Regular Expressions in VB

My first ever attempt at VB. Hope it's OK! ;)

Visual Basic:
Imports System.Text.RegularExpressions
Imports System.Windows.Forms

Public Module IDExtractor
    Sub Main
        Dim str As String
        str = "[3] DOE JOHN"

        Dim r As Regex
        r = New Regex("\[(?<id>([0-9]+))\]")

        Dim m As Match
        m = r.Match(str)

        If m.Success
            MessageBox.Show(m.Groups("id").Value, "ID")
        End If
    End Sub
End Module
 
Hog, you're make things too complicated for yourself, if all you want is to get the ID field from the selelcted combo then do something like this during binding...

myCombo.DataValueField = "SomeIDColumn"
myCombo.DataTextField = "SomeFullNameColumn"

Then to retrieve the ID when a user selects an item...

someVar = myCombo.SeletectedItem.Value
 
Right I know this is overly complicated but I couldn't figure out how to get the ID value.

If I used a datasource, displaymember,value member approach this would be easy. But I am building the combobox values on the fly. I don't know how to add multiple columns to a combobox and then how to retrieve a particular column.

Although I'm off to have a look now :)
 
Last edited:
Robby, my combo boxes do not hve properties datavaluefield or datatext field?

Thnx _SBradley_ I'll have to read up on regx as the Regex("\[(?<id>([0-9]+))\]" entry seems odd:)
 
Combobox is nothing but an ArrayList of objects. Toss in custom objects that define your object (Person for example) and override the ToString() method (Combobox uses the ToString() for displaying data). Then when you retrieve items from your Combobox, you can just cast it to your custom Person object.

C#:
class Person
{
   private string _name;
   private int _id;

   // properties etc here.

   // Combobox will use this method to display your data.
   override public string ToString() 
   {
      return "[" + _id + "] " + _name;
   }
}
 
IF you don't DataValueField or DataTextField then you must be using a Win Form, then you can achieve the same thing with the following...

Visual Basic:
myCombo.ValueMember = "SomeIDColumn"
myCombo.DisplayMember = "SomeFullNameColumn"

'Then to retrieve the ID when a user selects an item...

someVar = myCombo.SelectedValue
 
OK I'm obviously missing something really basic here:(

This is how I populate my combobox.

Visual Basic:
Dim strSQL As String = "SELECT '[' + str(CandidateID) + ']' + ' ' + LastName + ' ' + FirstName AS " & _
"FullName, LastName + ' ' + FirstName AS SortName " & _
"FROM tblCandidates ORDER BY LastName + ' ' + FirstName"

            While odrReader.Read()
                Me.cboFullName.Items.Add(odrReader.GetString(0))
            End While

I've omitted all the other code for clarity.

1. How do I add multiple columns?

2. I only have this problem cos I read that a datareader was better used in this read only situation as opposed to a dataadapter? I always used dataadapters before and so never had this problem??

3. Wryd, are you saying I should inherit a combobox control and modify it some how?

cheers:)
 
That's why in my second post I mentioned Binding, creat a Dataset with the following SQL Select....
Visual Basic:
Dim strSQL As String =  "SELECT SomeIdColumn, '[' + str(CandidateID) + ']' + ' ' + LastName + ' ' + FirstName AS " & _
"FullName, LastName + ' ' + FirstName AS SortName " & _
"FROM tblCandidates ORDER BY LastName, FirstName"

With cboFullName
    .Datasource = myDataSet.Tables(0).DefaultView 'This uses the dataview of the first table of your dataset
    .ValueMember = "SomeIdColumn"
    .DisplayMember = "FullName"
End With
 
Back
Top