hog Posted October 17, 2003 Posted October 17, 2003 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? 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)) Quote My website
Moderators Robby Posted October 17, 2003 Moderators Posted October 17, 2003 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? Quote Visit...Bassic Software
hog Posted October 17, 2003 Author Posted October 17, 2003 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?? Quote My website
_SBradley_ Posted October 17, 2003 Posted October 17, 2003 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] Quote
hog Posted October 17, 2003 Author Posted October 17, 2003 This looks impressive, but I don't understand it:( Can anyone put it into VB?? Quote My website
_SBradley_ Posted October 17, 2003 Posted October 17, 2003 Regular Expressions in VB My first ever attempt at VB. Hope it's OK! ;) 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 Quote
Moderators Robby Posted October 17, 2003 Moderators Posted October 17, 2003 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 Quote Visit...Bassic Software
hog Posted October 18, 2003 Author Posted October 18, 2003 (edited) 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 :) Edited October 18, 2003 by hog Quote My website
hog Posted October 18, 2003 Author Posted October 18, 2003 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:) Quote My website
wyrd Posted October 18, 2003 Posted October 18, 2003 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. 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; } } Quote Gamer extraordinaire. Programmer wannabe.
Moderators Robby Posted October 18, 2003 Moderators Posted October 18, 2003 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... myCombo.ValueMember = "SomeIDColumn" myCombo.DisplayMember = "SomeFullNameColumn" 'Then to retrieve the ID when a user selects an item... someVar = myCombo.SelectedValue Quote Visit...Bassic Software
hog Posted October 19, 2003 Author Posted October 19, 2003 OK I'm obviously missing something really basic here:( This is how I populate my combobox. 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:) Quote My website
Moderators Robby Posted October 19, 2003 Moderators Posted October 19, 2003 That's why in my second post I mentioned Binding, creat a Dataset with the following SQL Select.... 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 Quote Visit...Bassic Software
hog Posted October 19, 2003 Author Posted October 19, 2003 DOH!!!!! Can u all hear that penny drop:):):) Thnx Robby:) Quote My website
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.