ComboBox 'empty' row

Discussion in 'Code Samples & Tips' started by nl1007, Jul 13, 2009.

  1. nl1007

    nl1007 Member Licensed User

    I wanted to use a series of ComboBoxes, to hold option values for a series of parameters for rows in a database (originally an Excel Sheet, but I have saved it as a CSV, and also copied it to a SQLite db).
    I load a selected CSV file into a Table control, and save it back after updates..
    In order to indicate null fields, I wanted to have an 'empty' row ("") - since you can't set the Combo index to -1..(without re-loading the list).
    I edited the lists in the designer, but it seems that empty strings are not saved, so I added an 'Insert(0,"")' statement in App_Start.
    I also had to write a Sub to select the appropriate option (or 0) when reading from a Table Cell. Also, because you can't have multiple columns, I include the value and description in the list - like "1 (Good)","2 (Fair)","3 (Poor)","4 (Bad)" - then strip off the 'help' text when saving the value:
    (I realise I could have used arrays/ArrayLists to hold the 'codes' and only display the description, but this way users could select an option using the 'code', if they want).
    Code:
    cboType.Insert(0,""' add 'null' option
    ...
    sTmp = tblData.Cell(
    "CType",Row) ' Get value
    Find(sTmp,cboType.Name) ' Pass value and Control name..
    ...
    sTmp = cboType.Item(cboType.SelectedIndex) 
    ' value + description, e.g. "c (Concrete)"
    sTmp=RTrim(sTmp) ' I know these lines could be combined, but it was useful to step through/over during debug..
    tblData.Cell("CType",Row)=sTmp
    ...
    Sub Find(sValue, sControl)
    '   Dim oCombo As Control ' this doesn't work!
    '   oCombo = Control(sControl) ' I wanted to re-use a reference..
       iIndex = 0 ' default (not found)
            sValue = sValue & " " ' add space to avoid matching sub-strings
       If StrCompare(ControlType(sControl),"ComboBox") <> 0 Then Return
       iLen = StrLength(sValue) 
    ' number of characters to compare
       For iLoop=0 To Control(sControl,"ComboBox").Count - 1
           sTmp = 
    Control(sControl,"ComboBox").Item(iLoop)
          sTmp = SubString(sTmp,
    0,iLen) ' extract 'value' (+ " ")
          If StrCompare(sTmp, sValue)=0 Then ' ignore case
             iIndex = iLoop
             
    Exit
          
    End If
       
    Next
       
    Control(sControl,"ComboBox").SelectedIndex= iIndex
    End Sub
    ...
    Sub RTrim(sTmp)
       
    ' return first word (up to space)
       itmp = StrIndexOf(sTmp," ",0' first space
       If itmp > 0 Then sTmp = SubString(sTmp,0,itmp)
       
    Return sTmp ' returns whole string, if 'value only'
    End Sub
     
  2. derez

    derez Expert Licensed User

    I may have misunderstood the problem but :

    "I edited the lists in the designer, but it seems that empty strings are not saved"

    while in the designer, put "" as the first line in the combobox and you have a null string which gives the index of 0 and the value of "".
    When running it shows a blank !

    See the attached example.
     

    Attached Files:

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice