0

Good day! I have a code that import my table on a listbox and able to add on it.

i was stock on some codes when i double click a selected value on my listbox to open another dialogbox to edit (the selected value) it as well as on my table.

Heres my code...

Include "Mapbasic.def"

Declare Sub Main
Declare Sub AddItemDialog
Declare Sub AlterMainDialog
declare sub editlistDialog
declare sub Nodelist(byval sId as string)                                   
dim listboxValues() as string
declare sub listbox_edit


'---------------------------------------------------------------------------
Sub Main

   Dialog Title "Main Dialog" Width 150 Height 80 calling editlistDialog
      Control ListBox       Position 13, 7      Width 100 Height 50 ID 1 Title from variable listboxValues calling listbox_edit
      Control Button Title "Add Item" ID 2 Calling AddItemDialog
      Control OkButton
      Control CancelButton

End Sub
'---------------------------------------------------------------------------
Sub AddItemDialog

   Dim strAddItem as String

   Dialog Title "Add Item Dialog"
      Control EditText Width 100 Into strAddItem
      Control OkButton Title "Add" 
      Control CancelButton

   If CommandInfo(CMD_INFO_DLG_OK) Then
      Dim i as Integer
      i = Ubound(listboxValues)
      Redim listboxValues( i + 1)
      i = Ubound(listboxValues)
      listboxValues(i) = strAddItem

      insert into subn(lname)
      values(strAddItem)      

      Call AlterMainDialog
   End If

End Sub
'---------------------------------------------------------------------------
Sub AlterMainDialog

   Alter Control 1 Title from variable listboxValues

End Sub
'---------------------------------------------------------------------------

sub editlistDialog
    call Nodelist("5350")                                   
    alter control 1 title from variable listboxValues

End Sub
'---------------------------------------------------------------------------
sub Nodelist(byval sId as string)
    dim i as integer
    dim nRows as integer

    select * from subn where site=sId into lstValues noselect
    nRows=tableinfo("lstValues",tab_info_nrows)
    redim listboxValues(nrows)
    for i = 1 to tableinfo("lstValues",tab_info_nrows)
        fetch rec i from lstValues
        listboxValues(i)=lstValues.lname
    Next
End Sub
'---------------------------------------------------------------------------
Sub listbox_edit
    Dim i as string
    If CommandInfo(CMD_INFO_DLG_DBL) = TRUE Then 
    ' ...then the user DOUBLE-clicked. 
    ' see which list item the user clicked on. 
        i = ReadControlValue(1)     
        print i
        'Should try to look the table and edit list

    End If

End Sub

br,

Ivan

1

Here are some suggestions:

At the top add another array variable to hold the rowids from the query you get your values from. Just add the last line. The first line helps you to see where to add it:

dim listboxValues() as string
dim arrRecordRowIDs() as Integer

You have to fill this array with the rowids of the records in the NodeList procedure:

 redim listboxValues(nrows)
 Redim arrRecordRowIDs(nrows)
 for i = 1 to tableinfo("lstValues",tab_info_nrows)
      fetch rec i from lstValues
      listboxValues(i)      = lstValues.lname
      arrRecordRowIDs(i)    = lstValues.ROWID
 Next

Next step is to change your current way to insert a new record slightly in the AddItemDialog procedure. Instead of calling AlterMainDialog when you have inserted the new record, you call editlistDialog to reload all the elements and create a new query. When you do this you donøt have to add the element to the array of values first. The reason for doing this is that you need to be able to update it later on, potentially, and you can only do this if the value is in the query:

If CommandInfo(CMD_INFO_DLG_OK) Then
    insert into subn (lname)
        values (strAddItem)     

    Call editlistDialog
End If

Now we need to get you listbox_edit procedure finished:

Sub listbox_edit
    Dim sValue as String
    Dim nItem As Integer

    If CommandInfo(CMD_INFO_DLG_DBL) = TRUE Then
        '**Get the currently selected record
        nItem   = ReadControlValue(1)
        sValue  = listboxValues(nItem)    
        print nItem & " = " & sValue

        '**Shop the value in the dialog and read what the user changed it to
        Dialog Title "Change Item Dialog"
            Control EditText Width 100 
                Value sValue 
                Into sValue
            Control OkButton 
                Title "Update" 
            Control CancelButton

        If CommandInfo(CMD_INFO_DLG_OK) Then
            '**Update the array
            listboxValues(nItem) = sValue

            '**Update the record in the query (and so in the base table)
            Update lstValues
                Set lname = sValue
                Where ROWID = arrRecordRowIDs(nItem)

            '**Refresh the list in the dialog
            Call AlterMainDialog
        End If
    End If

End Sub

Deleting a record is basically done so:

Sub listbox_delete
    Dim sValue as String
    Dim nItem As Integer

    '**Get the currently selected record
    nItem   = ReadControlValue(1)
    sValue  = listboxValues(nItem)    
    print "Deleting " & nItem & " = " & sValue

    '**Update the record in the query (and so in the base table)
    Delete From lstValues 
        Where ROWID = arrRecordRowIDs(nItem)

    '**Refresh the list in the dialog
    Call AlterMainDialog

End Sub

You might want to add a Ask() function to ask the user if he really want to delete the record. Also you need to add a button to the dialog that will call this procedure

  • Hello again Sir Peter. How about if i wanted to delete the item on the list box? Thanks. – Ivan May 20 '15 at 19:53
  • See edited answer – Peter Horsbøll Møller May 22 '15 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.