Enhanced ComboBox / Listbox

Rioven

Active Member
Licensed User
Longtime User
Hi Dragonspank,
This was my old trick, I use textbox infont of the combobox...:)
see sample attached...
 

Attachments

  • Cbi1.sbp
    960 bytes · Views: 560

klaus

Expert
Licensed User
Longtime User
Hi Rioven,

I had a look at your trick, and have two comments.

- In my programs I use too a separate TextBox, but not overlapping the ComboBox, and two buttons, one to add the TextBox.Text at the end of the list and the second one to modify the selected item. I miss the last function in your trick.

- Your code:
B4X:
[FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]Combobox1_SelectionChanged(Index,Value)[/SIZE][/FONT]
[SIZE=2][FONT=Courier New]textbox1.Text=Combobox1.Item(Combobox1.SelectedIndex)[/FONT][/SIZE]
[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]End Sub[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
can be simplified by this one:
B4X:
[FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]Combobox1_SelectionChanged(Index,Value)[/SIZE][/FONT]
[SIZE=2][FONT=Courier New]textbox1.Text=Value[/FONT][/SIZE]
[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]End Sub[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]

Best regards.
 

Rioven

Active Member
Licensed User
Longtime User
Thanks Klaus! I've missed that code!
Sure, we could do many tricks/features on textbox and combobox combination on what you want to achieve. My sample is just adding items but cannot edit. You may post yours.

Regards,
 

klaus

Expert
Licensed User
Longtime User
Hi Rioven,

Here is an example how I use ComboBoxes and or ListBoxes in some of my programs.
I added also a ListBox, the princilpe is exactly the same as for the ComboBox but it shows better what happens.

You can:
- move items up and down
- insert an item on the selected position
- add an item at the end
- delete an item

Best regards.

EDIT: Removed the code, a new one is in post #7
 
Last edited:

Dragonspank

Member
Licensed User
Nice! Thanks Rioven! Managed an implementation into my application. :)

Klaus, your code looks sweet, but I get an error in line 14 with the index value of 5 being not valid. So what is amiss with your ComboBoxTest?

Thanks again for the great help!
 

Dragonspank

Member
Licensed User
Excellent!!!!
In my humble newbie opinion:
I think this should make it to a Sticky somewhere on this forum!

Thank you all for this fantastic support!!
:sign0060::sign0087:
 

Rioven

Active Member
Licensed User
Longtime User
Here is the correct code.

I added the listbox later on and didn't test every thing again.

Sorry for that and best regards.

Hello Klaus,
I've tested your code...pretty NICE!
When you make the listbox empty and press the delete or insert button it triggers error.

I have just added the conditions something like this, then it works...or you can have better solutions.

B4X:
Sub btnDelete_Click
   [B]If cbxTest.Count>0 Then[/B]
      cbxTest.RemoveAt(SelI)
      lbxTest.RemoveAt(SelI)
      If SelI>cbxTest.Count-1 Then
         SelI=SelI-1
      End If
      cbxTest.SelectedIndex=SelI
      lbxTest.SelectedIndex=SelI
   [B]End If   [/B]
End Sub

Sub btnInsert_Click
   [B]If cbxTest.Count>0 Then[/B]
      cbxTest.Insert(SelI,txtTest.Text)
      lbxTest.Insert(SelI,txtTest.Text)
      cbxTest.SelectedIndex=SelI
      lbxTest.SelectedIndex=SelI
   [B]End If   [/B]
End Sub

Regards,
 

klaus

Expert
Licensed User
Longtime User
Hi Rioven,

Thank's for the bug report.

I modified the btnInsert_Click routine that way:
B4X:
[FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Sub [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]btnInsert_Click[/SIZE][/FONT]
[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] SelI=-[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1 [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New]SelI=[/FONT][/SIZE][/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]cbxTest.Insert(SelI,txtTest.Text)[/SIZE][/FONT]
[SIZE=2][FONT=Courier New]lbxTest.Insert(SelI,txtTest.Text)[/FONT][/SIZE]
[SIZE=2][FONT=Courier New]cbxTest.SelectedIndex=SelI[/FONT][/SIZE]
[SIZE=2][FONT=Courier New]lbxTest.SelectedIndex=SelI[/FONT][/SIZE]
[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]End Sub[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]

Attached the modified code.

Besr regards.

EDIT: Removed code, new one in post #15.
 
Last edited:

Rainer

New Member
Licensed User
Longtime User
Entering text in combo boxes

Entering text in combo boxes is not supported. Using the Door library this can be achieved. Here is the code:

ocmb.New1(False)
ocmb.FromControl("cmbCategory")
ocmb.SetProperty("DropDownStyle", "DropDown")

That's it.
Rainer
 

agraham

Expert
Licensed User
Longtime User
To make it really useful lets add some events. Obj1 is a Door library Object, cboKeyPress and cboTextChanged are Door library events
B4X:
   Obj1.FromControl("Combobox1")
   Obj1.SetProperty("DropDownStyle", "DropDown")
   cboKeyPress.New1(Obj1.Value, "KeyPress")
   cboTextChanged.New1(Obj1.Value, "TextChanged")

Sub cboKeyPress_NewEvent
   Obj1.Value = cboKeyPress.Data
   keychar = Obj1.GetProperty("KeyChar")
   'Msgbox(keychar, "Key press")
   If Asc(keychar) = 13 Then
      Obj1.FromControl("Combobox1")
       Msgbox(Obj1.GetProperty("Text"), "Enter!")
   End If   
End Sub

Sub cboTextChanged_NewEvent
   Obj1.FromControl("Combobox1")
   combotext = Obj1.GetProperty("Text")
End Sub
 

klaus

Expert
Licensed User
Longtime User
Hallo Rainer,
Very good !
I remembered that in VB there was a choice for ComboBox styles, but didn't find your 'trick'.

Hello Andrew,
I am also wondering why the default style is different between desktop and device.

Just as a reminder, the ComboBox.Text property does exist in Basic4PPC even though it is not listed in the help file. Even in DropDownList style it is redable in on both desktop and device.

I have updated the example code, with the choice of the 2 styles.

Best regards.
 

Attachments

  • ComboBoxTest.zip
    2 KB · Views: 425
Top