Results 1 to 7 of 7

Thread: Combobox Store Values

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    10

    Combobox Store Values

    Hey all.

    i think its hard to explain, but here we go.

    I have a combobox, where you should be able to add item (in this case colors/Hexcode selected from colordialog).
    So lets say we have 3 items in that combobox:
    Red
    Green
    Blue

    and if you pick blue, the color blue should apper in a background color.
    I can only get names to work, but i cant find out how to store the hex value data.

    Heres a pic. might help a little.
    Attatched...
    Attached Images Attached Images  

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Combobox Store Values

    Given a color name

    Code:
    Me.BackColor = ColorTranslator.FromHtml("Blue")
    http://msdn.microsoft.com/en-us/libr...ranslator.aspx

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    10

    Re: Combobox Store Values

    I need to save a name together with a hex value, but the value should be hidden. Save the Data along with the name.

    Im new to this sry.

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Combobox Store Values

    I need to save a name together with a hex value, but the value should be hidden. Save the Data along with the name.
    You could create a DataTable with two columns, one for what to display, one as you call it hidden. Add rows to the DataTable, to show first set the DisplayMember to the column which will display and the ValueMember for the hidden value, set the DataSource of the control to the DataTable. On Selection Changed event of the control you can set the display item via the Text Property and the hidden value from SelectedValue

    To read back data use a DataSet.ReadXml, point to the first Table in the DataSet to load the control. To save back to disk use DataTable.WriteXml.

    Setup
    Code:
    Dim ds As New DataSet
    ds.ReadXml(FileName)
    
    ds.Tables(0).TableName = "SavedItems" ' must have a table name set
    
    MyComboBox.DisplayMember = "DisplayColumn"
    MyComboBox.ValueMember = "HiddenColumn"
    MyComboBox.DataSource = ds.Tables(0)
    Write back to disk
    Code:
    DirectCast(MyComboBox.DataSource, DataTable).WriteXml(FileName)

    How you might create the intial DataTable
    Code:
    Dim table As New DataTable()
    table.Columns.Add("DisplayColumn", GetType(String))
    table.Columns.Add("HiddenColumn", GetType(String))
    How to add rows to a DataTable
    http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx


    Here is some more conversions

    Code:
    Dim StringColor As [String] = "#FF00FF"
    Dim Converter As New ColorConverter()
    Dim MyColor As Color = DirectCast(Converter.ConvertFromString(StringColor), Color)
    Console.WriteLine(MyColor.ToString)
    Console.WriteLine(GetHexColor(MyColor))
    
    Private Function GetHexColor(ByVal sender As System.Drawing.Color) As String
        Return "#" & Hex(sender.R) & Hex(sender.G) & Hex(sender.B)
    End Function

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    10

    Re: Combobox Store Values

    As i said, im very new to this program, cloud be nice with some exsample.
    I Dont know what to add and where?

    Heres my code so far:
    vb Code:
    1. Private Sub Button1_Click_2(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    2.         If WCComboColors.Text.Length = 0 Then
    3.             MsgBox("Please enter a name.")
    4.         ElseIf WCComboColors.Items.Contains(WCComboColors.Text) Then
    5.             MsgBox("The name: " & "'" & WCComboColors.Text & "'" & " already exsist.")
    6.         Else
    7.             MsgBox("'" & WCComboColors.Text & "'" & " was added to the list.")
    8.             ' THIS WILL SAVE WHATEVER THE USERS WAS WRITTEN IN THE COMBOBOX.
    9.             ' NOW I JUST NEED TO SAVE THE VALUES (HEX VALUE, FROM A BACKGROUND COLOR OF A PANEL), ALONG WITH THE NAME.
    10.             ' SO IF A USER PRESSES ORANGE, THE ORANGE HEX VALUE COLOR APPEARS IN A BACKGROUND COLOR OF A PANEL.
    11.             ' THE USER SHOULD ALSO BE ABLE TO DELETE VALUES/NAMES.
    12.             ' I DONT KNOW HOW TO SAVE/LOAD THIS DATA?
    13.             WCComboColors.Items.Add(WCComboColors.Text)
    14.  
    15.         End If
    16.     End Sub
    17.     Private Sub Button2_Click_1(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    18.         MsgBox("'" & WCComboColors.Text & "'" & " was deleted from the list.")
    19.         WCComboColors.Items.Remove(WCComboColors.SelectedItem)
    20.         WCComboColors.Text = ""
    21.         ColorBox.BackColor = Color.Black
    22.     End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    10

    Re: Combobox Store Values

    bump

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Combobox Store Values

    Quote Originally Posted by crusher View Post
    bump
    The only thing bumping will get you is this. Do not bump

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width