Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / March 2008

Tip: Looking for answers? Try searching our database.

combobox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
enrico - 06 Mar 2008 01:45 GMT
how can i make a combobox that can store or add additional fields?
Trammel - 06 Mar 2008 01:57 GMT
> how can i make a combobox that can store or add additional fields?

ComboBox1.Items.Add("blah")
ComboBox1.Items.Add("1234")
Trammel - 06 Mar 2008 02:08 GMT
>> how can i make a combobox that can store or add additional fields?
>
> 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 - 06 Mar 2008 02:15 GMT
>>> how can i make a combobox that can store or add additional fields?
>>
[quoted text clipped - 14 lines]
>    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
enrico - 07 Mar 2008 03:00 GMT
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?
Trammel - 07 Mar 2008 04:25 GMT
> thanks for the help trammel. i plainly use the code:
>
[quoted text clipped - 12 lines]
> 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
SurturZ - 06 Mar 2008 02:29 GMT
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

Signature

David Streeter
Synchrotech Software
Sydney Australia

> how can i make a combobox that can store or add additional fields?
enrico - 06 Mar 2008 02:49 GMT
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...
Trammel - 06 Mar 2008 03:59 GMT
> 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
Cor Ligthert[MVP] - 07 Mar 2008 04:46 GMT
Enriico,

That depends completely from how you this combined box use.

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

:-)

Cor

> how can i make a combobox that can store or add additional fields?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.