combobox

  • Thread starter Thread starter enrico via DotNetMonster.com
  • Start date Start date
E

enrico via DotNetMonster.com

how can i make a combobox that can store or add additional fields?
 
enrico via DotNetMonster.com said:
how can i make a combobox that can store or add additional fields?

ComboBox1.Items.Add("blah")
ComboBox1.Items.Add("1234")
 
Trammel said:
ComboBox1.Items.Add("blah")
ComboBox1.Items.Add("1234")

This can be expanded-on using a button labeled "+" or however you submit
changes to the form/box:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim iLoop As Long
Dim bFound As Boolean = False
For iLoop = 0 To ComboBox1.Items.Count - 1
If UCase(ComboBox1.Text) = UCase(ComboBox1.Items.Item(iLoop)) Then
bFound = True
Next
If bFound = False Then ComboBox1.Items.Add(ComboBox1.Text)
End Sub
 
Trammel said:
This can be expanded-on using a button labeled "+" or however you submit
changes to the form/box:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim iLoop As Long
Dim bFound As Boolean = False
For iLoop = 0 To ComboBox1.Items.Count - 1
If UCase(ComboBox1.Text) = UCase(ComboBox1.Items.Item(iLoop)) Then
bFound = True
Next
If bFound = False Then ComboBox1.Items.Add(ComboBox1.Text)
End Sub

Also, if you not bothered about people adding multiple words in different
cases (Aka: Blah, blah, bLaH, etc) then it can be shortened to one line,
like:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If Not ComboBox1.Items.Contains(ComboBox1.Text) Then
ComboBox1.Items.Add(ComboBox1.Text)
End Sub
 
The .Item() array in the combobox will accept any type, so you can create a
custom class to hold your additional properties - just make sure you override
..ToString() to show what you want each entry to display in the combobox
 
i'm sorry, i can barely grasp what you meant about the .tostring() part. i'm
not yet familiar on this language. can you elaborate more... tnx...
 
enrico via DotNetMonster.com said:
i'm sorry, i can barely grasp what you meant about the .tostring() part.
i'm
not yet familiar on this language. can you elaborate more... tnx...


Example of a custom class overriding the .tostring function
(overriding isnt something you will probably need, but...)
(if you get confused... dont worry - it doesnt look like you needed custom
classes anyway)

Description:
Button1 puts the custom object into the combobox (and uses its .tostrong
override), Button2 copies the selected-object from the combobox to the
picturebox... this could be any of the objects in its list, although the
example only shows adding one object derived from the custom class. The
objects in the combobox could be a mixture of different custom objects,
strings, images, other comboboxes, etc.

Controls needed on form:
*ComboBox1
* Button1
* Button2
* Picturebox1

Code:
Public Class Form1
Private myImg As New cMyImg("C:\smiley.jpg")

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
ComboBox1.Items.Add(myImg)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
PictureBox1.Image = ComboBox1.SelectedItem
End Sub
End Class


Public Class cMyImg
Public Img As Bitmap
Private ImagePath As String = ""

'Here is an overriding function
Overrides Function ToString() As String
If ImagePath = "" Then
ToString = "{NoImageSet}"
Else
ToString = "{" & ImagePath & ")"
End If
End Function

Public Sub SetImage(ByVal filepath As String)
Try
Img = New Bitmap(filepath)
ImagePath = filepath
Catch
End Try
End Sub

Public Sub New(ByVal filepath As String)
Try
Img = New Bitmap(filepath)
ImagePath = filepath
Catch
End Try
End Sub
End Class
 
thanks for the help trammel. i plainly use the code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If Not ComboBox1.Items.Contains(ComboBox1.Text) Then
ComboBox1.Items.Add(ComboBox1.Text)
End Sub

and it worked.. but is there a way that those new fields that were entered in
the combobox will be part of the combobox's list? for example: the fields in
the combobox are B, C, D then i entered A. every time i close my form and
then reopen there are still 3 fields in the combobox. i want A be be part of
that list, is it possible?
 
enrico via DotNetMonster.com said:
thanks for the help trammel. i plainly use the code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If Not ComboBox1.Items.Contains(ComboBox1.Text) Then
ComboBox1.Items.Add(ComboBox1.Text)
End Sub

and it worked.. but is there a way that those new fields that were entered
in
the combobox will be part of the combobox's list? for example: the fields
in
the combobox are B, C, D then i entered A. every time i close my form and
then reopen there are still 3 fields in the combobox. i want A be be part
of
that list, is it possible?

Store all the values in a global array and add them when the form is
created. Its not the best or most modular way but should be Ok for you.
Below is some untested code as a demo:


Module Globals
Public cboEntries As New ArrayList
End Module

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If Not ComboBox1.Items.Contains(ComboBox1.Text) Then
ComboBox1.Items.Add(ComboBox1.Text)
cboEntries.Add(ComboBox1.Text)
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim iLoop As Long
For iLoop = 0 To cboEntries.Count - 1
cboEntries.Add(ComboBox1.Text)
Next
End Sub
End Class
 
Enriico,

That depends completely from how you this combined box use.

That combobox means in a way the same a multi tool.

:-)

Cor
 
Back
Top