none
clear combobox RRS feed

  • Question

  • Hello, 

    I have a form with a combobox. I have several names in it. 

    I wish after selecting a name, had a button that clears the combobox and allows me to make another selection. 

    What is the code that lets you do this?
    Friday, February 14, 2014 10:30 PM

Answers

  • Hello, 

    I have a form with a combobox. I have several names in it. 

    I wish after selecting a name, had a button that clears the combobox and allows me to make another selection. 

    What is the code that lets you do this?

    Clearing it will remove all of the items - I presume that you mean to set it so that none of the items are selected, right?

    If so then just set the combo's .SelectedIndex = -1


    Please call me Frank :)

    • Proposed as answer by IronRazerz Friday, February 14, 2014 10:43 PM
    • Marked as answer by Vitor Patrício Sunday, February 16, 2014 1:01 AM
    Friday, February 14, 2014 10:35 PM
  • So what would I put the code in boton1 to ComboBox1?

     If you want Button1 to clear the selected item then

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ComboBox1.SelectedIndex = -1
        End Sub
    

    Friday, February 14, 2014 11:24 PM

All replies

  • Hello, 

    I have a form with a combobox. I have several names in it. 

    I wish after selecting a name, had a button that clears the combobox and allows me to make another selection. 

    What is the code that lets you do this?

    Clearing it will remove all of the items - I presume that you mean to set it so that none of the items are selected, right?

    If so then just set the combo's .SelectedIndex = -1


    Please call me Frank :)

    • Proposed as answer by IronRazerz Friday, February 14, 2014 10:43 PM
    • Marked as answer by Vitor Patrício Sunday, February 16, 2014 1:01 AM
    Friday, February 14, 2014 10:35 PM
  • Hi,

     You can also set its text to nothing like this.

    ComboBox1.Text = ""


    EDIT : I should add that if the DropDown style is set to DropDownList then this won`t work. You would be better off using what Frank has shown you.

    • Edited by IronRazerz Friday, February 14, 2014 10:49 PM
    • Proposed as answer by Frank L. Smith Friday, February 14, 2014 10:55 PM
    Friday, February 14, 2014 10:44 PM
  • Hi,

     You can also set its text to nothing like this.

    ComboBox1.Text = ""

    That won't change the .SelectedIndex though.


    Please call me Frank :)

    Friday, February 14, 2014 10:49 PM
  • Hi,

     You can also set its text to nothing like this.

    ComboBox1.Text = ""

    That won't change the .SelectedIndex though.


    Please call me Frank :)


     Yea, i know. I guess it depends on what he is doing i guess but, as i added to my edit he should use the method you showed.
    Friday, February 14, 2014 10:51 PM
  • Yea, i know. I guess it depends on what he is doing i guess but, as i added to my edit he should use the method you showed.

    I'm wrong!

    According to the MSDN documention:

    "Setting the Text property to Nothing or an empty string ("") sets the SelectedIndex to -1. Setting the Text property to a value that is in the Items collection sets the SelectedIndex to the index of that item. Setting the Text property to a value that is not in the collection leaves the SelectedIndex unchanged."


    Please call me Frank :)

    Friday, February 14, 2014 10:54 PM
  • I'm wrong!

     I thought it kept its selected index so i guess that makes 2 of us. Doh!! It is probably better to get use to using the SelectedInex = -1 because, as i said it will not work setting the text to nothing if the style is a DropDownList.   :)

    Friday, February 14, 2014 11:00 PM
  • I'm wrong!

     I thought it kept its selected index so i guess that makes 2 of us. Doh!! It is probably better to get use to using the SelectedInex = -1 because, as i said it will not work setting the text to nothing if the style is a DropDownList.   :)

    Well I knew that if you set the text to something not in the list (and that has to be done before it's disabled ... if that's part of the routine), then the .SelectedIndex wouldn't change. The text would, but it'd be meaningless relative to the internal list of items (which don't have to be strings but normally are).

    So I assumed that an empty string, not being in the list, would likewise do the same thing but alas, it sees that as special and sets the .SelectedIndex to -1.

    Eh, go figure. Now I know ;-)


    Please call me Frank :)

    Friday, February 14, 2014 11:05 PM
  • So what would I put the code in boton1 to ComboBox1?
    Friday, February 14, 2014 11:17 PM
  • So what would I put the code in boton1 to ComboBox1?

    Let me explain in terms of what needs to be done rather than real code. You should be able to figure it out from there.

    You'll want to handle three events: The form's .Load event, the combobox's .SelectedIndexChanged event, and the button's .Clicked event.

    In the form's .Load event, populate your combo and set the .Enabled property of the button to false.

    In the combo's .SelectedIndexChanged event, look to see IF the .SelectedIndex is greater than -1 and if it is, enable the button.

    In the button's .Click event, first set its own .Enabled property to false, then set the combo's .SelectedIndex to -1.

    Make sense?


    Please call me Frank :)

    Friday, February 14, 2014 11:23 PM
  • So what would I put the code in boton1 to ComboBox1?
    Friday, February 14, 2014 11:24 PM
  • So what would I put the code in boton1 to ComboBox1?

     If you want Button1 to clear the selected item then

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ComboBox1.SelectedIndex = -1
        End Sub
    

    Friday, February 14, 2014 11:24 PM
  • Friend, this part of the Code ...

    Vitor,

    Who are you talking to?

    Please be specific - we don't all see this forum the same way.


    Please call me Frank :)

    Friday, February 14, 2014 11:40 PM
  • Friend, this part of the Code conflicts with this part of my program. 

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                 Handles ComboBox1.SelectedIndexChanged
            If ComboBox1.Text = String.Empty Then
                ComboBox1.SelectedIndex = -1
                ComboBox1.Select()
            End If
            If ComboBox1.Text = ComboBox_Names.Text Then
                MessageBox.Show("Por favor escolha outro nome", "Designar irmão", MessageBoxButtons.OK)
                ComboBox1.SelectedIndex = -1
                ComboBox1.Select()
            End If
            If ComboBox1.Text = ComboBox2.Text Then
                MessageBox.Show("Por favor escolha outro nome", "Designar irmão", MessageBoxButtons.OK)
                ComboBox1.SelectedIndex = -1
                ComboBox1.Select()
            End If
        End Sub

    Can you build another code?

    Friday, February 14, 2014 11:50 PM
  • This is what I meant (based on your original question):

    Option Strict On Option Explicit On Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load With ComboBox1 .Items.AddRange(New String() {"Apples", "Oranges", "Pears"}) .AutoCompleteMode = AutoCompleteMode.SuggestAppend .AutoCompleteSource = AutoCompleteSource.ListItems End With Button1.Enabled = False End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles ComboBox1.SelectedIndexChanged If ComboBox1.SelectedIndex > -1 Then Button1.Enabled = True Else Button1.Enabled = False End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button1.Click Button1.Enabled = False ComboBox1.SelectedIndex = -1 End Sub End Class



    Please call me Frank :)

    Friday, February 14, 2014 11:51 PM
  • Frank, 

    Will can help me resolve the error? Because as I show in my project code, also the message every time I click the button to clear appears: 

      If ComboBox1.Text = ComboBox_Names.Text Then 
                 MessageBox.Show ("Please choose another name", "Brother Designate", MessageBoxButtons.OK) 
                 ComboBox1.SelectedIndex = -1 
                 ComboBox1.Select () 

    Ie have two overlapping functions. I can put the button to clean without writing? 

    ComboBox1.SelectedIndex = -1
    Friday, February 14, 2014 11:56 PM
  • Friend, this part of the Code conflicts with this part of my program.

     I can`t understand why your checking if an item is an empty string so i removed that from your code. I can only guess that the conflict is because, every time you set the selected index to -1 the SelectedIndexChanged event is raised again. Try this

        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            If ComboBox1.SelectedIndex > -1 Then
                If ComboBox1.Text = ComboBox_Names.Text Then
                    MessageBox.Show("Por favor escolha outro nome", "Designar irmão", MessageBoxButtons.OK)
                    ComboBox1.SelectedIndex = -1
                    ComboBox1.Select()
                End If
                If ComboBox1.Text = ComboBox2.Text Then
                    MessageBox.Show("Por favor escolha outro nome", "Designar irmão", MessageBoxButtons.OK)
                    ComboBox1.SelectedIndex = -1
                    ComboBox1.Select()
                End If
            End If
        End Sub



    Friday, February 14, 2014 11:57 PM
  • Frank, 

    Will can help me resolve the error? Because as I show in my project code, also the message every time I click the button to clear appears: 

      If ComboBox1.Text = ComboBox_Names.Text Then 
                 MessageBox.Show ("Please choose another name", "Brother Designate", MessageBoxButtons.OK) 
                 ComboBox1.SelectedIndex = -1 
                 ComboBox1.Select () 

    Ie have two overlapping functions. I can put the button to clean without writing? 

    ComboBox1.SelectedIndex = -1

    Vitor,

    You're off topic for what was asked. Think about it here - you asked about "clearing" (reseting) the combo based on a button press, right?

    You have that answer.

    *****

    It looks like you have another event (a button press?) that is supposed to add an item from one collection to the next.

    That can be done any of several ways and one would be to disable the button (assuming that's what it is) if it's already in that other collection.

    The other way would be to show a messagebox like you're doing now.


    Please call me Frank :)

    Saturday, February 15, 2014 12:00 AM
  • Friend, I have my project set up so that when you close the form, the final selection will be stored. 
    Now I want via a button to clear the various combobox, so you can make further selections. 
    I can not make the function -1 because the message appears so I have configured

    Saturday, February 15, 2014 12:14 AM
  • How can clearing, without writing this code? 

    ComboBox1. SelectedIndex = - 1
    Saturday, February 15, 2014 12:21 AM
  • How can clearing, without writing this code? 

    ComboBox1. SelectedIndex = - 1

    Vitor,

    I am completely guessing at what you want but maybe this is close enough that it'll give you some ideas?

    Option Strict On Option Explicit On Public Class Form1 Private itemsList As New List(Of String) Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load itemsList.AddRange(New String() {"Apples", "Oranges", "Pears"}) PopulateCombo() End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles ComboBox1.SelectedIndexChanged If ComboBox1.SelectedIndex > -1 Then Dim selectedFruit As String = ComboBox1.Items(ComboBox1.SelectedIndex).ToString If Not ListBox1.Items.Contains(selectedFruit) Then ListBox1.Items.Add(selectedFruit) PopulateCombo() End If End If If ComboBox1.Items.Count = 0 Then ComboBox1.Enabled = False End If End Sub Private Sub PopulateCombo() With ComboBox1 .Items.Clear() For Each item As String In itemsList If Not ListBox1.Items.Contains(item) Then .Items.Add(item) End If Next End With End Sub End Class


    The idea here is that once an item has been selected in the combo, it's added to the Listbox then taken out of the combo so it can't be selected again.

    Is that close at all?


    Please call me Frank :)

    Saturday, February 15, 2014 12:29 AM
  • Friend Frank, 

    I appreciate your work. 

    However try to help me on the following: 

    How to Clean a combobox without using this: 

      ComboBox1. SelectedIndex = - 1
    Saturday, February 15, 2014 12:37 AM
  • Friend Frank, 

    I appreciate your work. 

    However try to help me on the following: 

    How to Clean a combobox without using this: 

      ComboBox1. SelectedIndex = - 1

    ComboBox1.Items.Clear

    Please call me Frank :)

    • Proposed as answer by IronRazerz Saturday, February 15, 2014 12:45 AM
    Saturday, February 15, 2014 12:39 AM
  • Friend Frank, 

    This function is interesting! 

    The only problem is that the list is empty. 

    Is it possible to clear and reset the list?

    For Each personName As String In listOfNames

    Saturday, February 15, 2014 12:50 AM
  • Friend Frank, 

    This function is interesting! 

    The only problem is that the list is empty. 

    Is it possible to clear and reset the list?

    For Each personName As String In listOfNames

    Sure,

    You could create a method (sub) which does exactly what I have shown in the .Load event (but if so, then in the .Load event, call the sub rather than duplicating the code).

    The only change would be to also clear the items from the ListBox also. I didn't do that in the .Load event because at that point in time, nothing is in the Listbox, but other than that, it'd be the same thing.


    Please call me Frank :)

    Saturday, February 15, 2014 12:53 AM
  • Friend Frank, 

    Can you give me examples of this? 

    grateful
    Saturday, February 15, 2014 1:00 AM
  • Friend Frank, 

    Can you give me examples of this? 

    grateful

    I taking off for the night but c'mon Vitor, we've been at it for over two YEARS now.

    This is easy.

    Create a new method like this:

    Private Sub Reset()
        ' Now your code
    End Sub

    Think it through. It's there, you're just not thinking of it yet, but you know what goes there.

    :)

    I'm taking off for the night ...


    Please call me Frank :)

    Saturday, February 15, 2014 1:03 AM
  • Friend Frank, 

    I apologize, but I do not understand this part.
    Saturday, February 15, 2014 1:11 AM
  • Friend Frank, 

    I apologize, but I do not understand this part.

    I don't really get what you want. Your explanations are continually changing from your original question to other things happening and yet you don't show the entire program so everybody can see all the code and figure out what you are talking about.

    This code will not change the selected index. It seems to me you don't want to change the selected index. Or maybe you do but who knows?

    When I select Frank L. Smith in the ComboBox the message box from the ComboBox1.SelectedIndexChanged event launches showing the selected index is 1.

    Then when I use the Button1 click event the combobox text field is cleared, the message box in that event launches and shows the selected index is still 1.

    Is that what you want?

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.CenterToScreen()
            ComboBox1.Items.Add("Vitor Patricio")
            ComboBox1.Items.Add("Frank L. Smith")
            ComboBox1.Items.Add("IronRazerz")
        End Sub
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ComboBox1.ResetText()
            MessageBox.Show(ComboBox1.SelectedIndex.ToString)
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            MessageBox.Show(ComboBox1.SelectedIndex.ToString)
        End Sub
    
    End Class


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.

    Saturday, February 15, 2014 2:42 AM
  •  Hi Vitor,

     You have asked several similar questions about saving, adding, and removing items in a ComboBox over the last 2 years and it seems like you should have learned some of the basic things needed to work with ComboBox controls and there item collections. Perhaps you should study the Properties, Methods, and Events of the ComboBox control in the msdn documents.

    ComboBox Properties

    ComboBox Methods

    ComboBox Events 

    Saturday, February 15, 2014 4:17 AM
  • Friend, I have my project set up so that when you close the form, the final selection will be stored. 
    Now I want via a button to clear the various combobox, so you can make further selections. 
    I can not make the function -1 because the message appears so I have configured

    This thread is really getting lengthy for something simple.

    You have been given the solution, so if this other message is the problem, change the way you handle it.  It seems to me you have conflicting logic and since the routines shown in this thread are correct, I would suggest you tackle the other routine you made that makes you think you cannot use these solutions.  For that you should start a new thread.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Saturday, February 15, 2014 4:45 AM
    Moderator
  • Friend Frank, 

    I apologize, but I do not understand this part.

    I do truly wish I understood what you want. I'm not holding out, I just don't understand what you're trying to do.

    Please call me Frank :)

    Saturday, February 15, 2014 1:34 PM
  • Saturday, February 15, 2014 10:46 PM
  • Vitor,

    I'm sure that makes complete sense to you - but I have no idea what you're doing here.

    You have TWO comboboxes - that wasn't explained at first, so please explain what these two combo's are and how they're related to each other, then maybe we can hone things down to figure out the issue.


    Please call me Frank :)

    Saturday, February 15, 2014 11:00 PM
  • Saturday, February 15, 2014 11:25 PM
  • Saturday, February 15, 2014 11:26 PM
  • Oh that makes it so much easier. A bunch of code in images that anybody trying to resolve the issue will manually have to type up instead of copying and pasting it into a Form. Thanks.

    Why don't you copy and paste all the code into a code block? Not just some of the code. All of the code. Is that too much to expect?


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.

    Saturday, February 15, 2014 11:30 PM
  • friend 

    I just wanted to highlight the part of the conflict, to better understand

    Option Strict On Option Explicit On Imports System.ComponentModel Imports System.Runtime.InteropServices Public Class Form5 Public listOfNames As New List(Of String) Public textToShow As String = "" Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Label1.Text = textToShow Dim Temp As String = ComboBoxEx1.Text Dim Temp1 As String = ComboBox1.Text For Each personName As String In listOfNames ComboBox_Names.Items.Add(personName) ComboBoxEx1.Items.Add(personName) ComboBox2.Items.Add(personName) ComboBox1.Items.Add(personName) Next ComboBoxEx1.Text = Temp ComboBox1.Text = Temp1 End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load ComboBox_Names.DropDownStyle = ComboBoxStyle.DropDownList ComboBoxEx1.DropDownStyle = ComboBoxStyle.DropDownList ComboBox2.DropDownStyle = ComboBoxStyle.DropDownList ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList If listOfNames.Count = 0 Then MessageBox.Show("The list is empty -- returning to Form1", "Nothing To Show!", _ MessageBoxButtons.OK, MessageBoxIcon.Stop) Me.Close() Else For Each personName As String In listOfNames ComboBox_Names.Items.Add(personName) ComboBoxEx1.Items.Add(personName) ComboBox2.Items.Add(personName) ComboBox1.Items.Add(personName) Next End If End Sub Private Sub Button_OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button_OK.Click Me.Close() End Sub Private Sub ComboBox_Names_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox_Names.SelectedIndexChanged If ComboBox2.Text = String.Empty Then ComboBox2.SelectedIndex = -1 ComboBox2.Select() End If If ComboBox2.Text = ComboBox_Names.Text Then MessageBox.Show("Por favor escolha outro nome", "Designar irmão", MessageBoxButtons.OK) ComboBox2.SelectedIndex = -1 ComboBox2.Select() End If End Sub Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles ComboBox2.SelectedIndexChanged If ComboBox2.Text = String.Empty Then ComboBox2.SelectedIndex = -1 ComboBox2.Select() End If If ComboBox2.Text = ComboBox_Names.Text Then MessageBox.Show("Por favor escolha outro nome", "Designar irmão", MessageBoxButtons.OK) ComboBox2.SelectedIndex = -1 ComboBox2.Select() End If End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles ComboBox1.SelectedIndexChanged If ComboBox1.Text = String.Empty Then ComboBox1.SelectedIndex = -1 ComboBox1.Select() End If If ComboBox1.Text = ComboBox_Names.Text Then MessageBox.Show("Por favor escolha outro nome", "Designar irmão", MessageBoxButtons.OK) ComboBox1.SelectedIndex = -1 ComboBox1.Select() End If If ComboBox1.Text = ComboBox2.Text Then MessageBox.Show("Por favor escolha outro nome", "Designar irmão", MessageBoxButtons.OK) ComboBox1.SelectedIndex = -1 ComboBox1.Select() End If End Sub Dim F7 As Form7 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click F7 = New Form7(DateTimePicker1.Value) If ComboBox2.SelectedIndex = 0 Then F7.Label7.Visible = True End If If ComboBox_Names.SelectedIndex = 0 Then F7.Label8.Visible = True Else End If If ComboBox_Names.SelectedIndex = 0 Then F7.Label2.Text = ComboBox_Names.SelectedItem.ToString End If If ComboBox2.SelectedIndex = 0 Then F7.Label2.Text = ComboBox2.SelectedItem.ToString End If F7.ShowDialog() End Sub Private Sub DateTimePicker1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.ValueChanged End Sub Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) End Sub Private Sub Button2_Click_1(sender As System.Object, e As System.EventArgs) Handles Button2.Click ComboBox_Names.DropDownStyle = ComboBoxStyle.Simple End Sub Private Sub ComboBoxEx1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBoxEx1.SelectedIndexChanged End Sub Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click ComboBox1.SelectedIndex = -1 End Sub End Class Public Class ComboBoxEx Inherits ComboBox Sub New() MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed End Sub Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs) MyBase.OnHandleCreated(e) Helpers.CenterComboText(Me) End Sub Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs) Helpers.CenterComboText(Me) MyBase.OnTextChanged(e) End Sub Protected Overrides Sub OnDropDown(ByVal e As System.EventArgs) Dim ddsize As Integer = Me.DropDownWidth Using g As Graphics = Me.CreateGraphics For Each itm As String In Me.Items Dim sw As Integer = CInt(g.MeasureString(itm, Me.Font).Width) If sw > ddsize Then ddsize = sw + 10 Next End Using Me.DropDownWidth = ddsize MyBase.OnDropDown(e) End Sub <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)> _ Public Shadows ReadOnly Property DrawMode() As DrawMode Get Return MyBase.DrawMode End Get End Property Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs) If e.Index > -1 Then Dim txt As String = MyBase.Items(e.Index).ToString e.DrawBackground() Dim sf As New StringFormat sf.Alignment = StringAlignment.Center e.Graphics.DrawString(txt, e.Font, New SolidBrush(e.ForeColor), e.Bounds, sf) e.DrawFocusRectangle() End If End Sub End Class Friend Class Win32API <StructLayout(LayoutKind.Sequential)> _ Public Structure RECT Public Left As Integer Public Top As Integer Public Right As Integer Public Bottom As Integer End Structure Public Const EM_SETMARGINS As Integer = &HD3 Public Const EC_LEFTMARGIN As Integer = &H1 Public Declare Function FindWindowEx Lib "User32.dll" Alias "FindWindowExA" (ByVal hWnd As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr Public Declare Function GetWindowRect Lib "User32.dll" (ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean Public Declare Function SendMessage Lib "User32.dll" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As Integer End Class Friend Class Helpers Public Shared Sub CenterComboText(ByVal cbo As ComboBox) If Not cbo.DropDownStyle = ComboBoxStyle.DropDownList Then Dim cboEdit_hWnd As IntPtr = Win32API.FindWindowEx(cbo.Handle, IntPtr.Zero, "Edit", Nothing) If Not cboEdit_hWnd = IntPtr.Zero Then Dim textWidth As Integer Using g As System.Drawing.Graphics = cbo.CreateGraphics() textWidth = CInt(g.MeasureString(cbo.Text, cbo.Font).Width) End Using Dim rct As Win32API.RECT Win32API.GetWindowRect(cboEdit_hWnd, rct) Dim leftMargin As Integer = ((rct.Right - rct.Left) - textWidth) \ 2 Win32API.SendMessage(cboEdit_hWnd, Win32API.EM_SETMARGINS, Win32API.EC_LEFTMARGIN, leftMargin) End If End If End Sub End Class



    Saturday, February 15, 2014 11:33 PM
  • Vitor,

    I'll ask again: You have two comboboxes. Tell me what each of them are for and how they interact.

    NOT code, not "code talk", just explain in your own words what these things are and how they're related to each other.

    I suspect that "we" on the recieving end are taking pure guesses at what you have, what you're trying to do, and what might be the problem in you accomplishing that. I know on my end that's the case.

    Take your time and just explain it as best you can then we can get to why it's not working like you think it should.


    Please call me Frank :)

    Saturday, February 15, 2014 11:40 PM
  • Friend Frank, 

    This combobox has a list of the same names. 

    What the program does is: If I select two of the same name, a message saying that you can not and deletes the last selection appears. 

    When I close the form teams of the last combobox that did appear after opening. 

    Now I want to put a button that clears the selections. The problem is that when I click the message always appears (the same as above) saying I can not repeat selections. Hence I have put the two combobox I want to understand how to adapt the code in my project. 

    Thanks, my good friend and eternal.
    Saturday, February 15, 2014 11:59 PM
  • Friend Frank, 

    This combobox has a list of the same names. 

    What the program does is: If I select two of the same name, a message saying that you can not and deletes the last selection appears. 

    When I close the form teams of the last combobox that did appear after opening. 

    Now I want to put a button that clears the selections. The problem is that when I click the message always appears (the same as above) saying I can not repeat selections. Hence I have put the two combobox I want to understand how to adapt the code in my project. 

    Thanks, my good friend and eternal.

    I can tell that a lot of the issue here is due to translation between your language (Portugese?) and English.

    Do I understand correctly that normally you'd only have one ComboBox - but to try to overcome the problem you've added a second one?

    Let's go with that for now. Let me know please.


    Please call me Frank :)

    Sunday, February 16, 2014 12:03 AM
  • Frank, 

    I thought that giving an example of just one combobox, I could adapt the code to the project, but unfortunately the problem arises with two combobox. 

    With this button: 

    ComboBox1.SelectedIndex = -1 


    When I click, the message also appears that does not allow duplicate names and appear at this time is inconvenient: 

    If ComboBox1.Text = ComboBox2.Text Then 
                 MessageBox.Show ("Please choose another name", "Brother Designate", MessageBoxButtons.OK) 
                 ComboBox1.SelectedIndex = -1 
                 ComboBox1.Select () 
             end If 


    I did not want this message to appear when I click the button. 

    Did that just clear click.
    Sunday, February 16, 2014 12:15 AM
  • Frank, 

    I thought that giving an example of just one combobox, I could adapt the code to the project, but unfortunately the problem arises with two combobox. 

    With this button: 

    ComboBox1.SelectedIndex = -1 


    When I click, the message also appears that does not allow duplicate names and appear at this time is inconvenient: 

    If ComboBox1.Text = ComboBox2.Text Then 
                 MessageBox.Show ("Please choose another name", "Brother Designate", MessageBoxButtons.OK) 
                 ComboBox1.SelectedIndex = -1 
                 ComboBox1.Select () 
             end If 


    I did not want this message to appear when I click the button. 

    Did that just clear click.

    Ok so there are two combo's here.

    When you select a name in the first one, it's supposed to be added to the internal list of .Items in the second combo but it can't be because it's already in that combo's list, right? Or do I still have it wrong?


    Please call me Frank :)

    Sunday, February 16, 2014 12:18 AM
  • Friend Frank, 

    In fact what I wanted I write code or create a rule that is not used to clean it: 

      ComboBox1.SelectedIndex = -1 


    Because I have already written this elsewhere and then I expression appears, click on two things.
    Sunday, February 16, 2014 12:26 AM
  • Friend Frank, 

    In fact what I wanted I write code or create a rule that is not used to clean it: 

      ComboBox1.SelectedIndex = -1 


    Because I have already written this elsewhere and then I expression appears, click on two things.

    I think I've jumped from the griddle into the fire...

    I have no idea what you just said there Vitor, I'm sorry that I don't get it.

    *****

    If the issue is that there's a duplicate in both collections (that is, of both ComboBoxes) then one way to eliminate that would be to something like I suggested last night.

    I don't know if this is helpful or not but you might construct a sub that would remove items from combo1 if they're already in combo2. Something like this:

    Private Sub RemoveDuplicates
        For i As Integer = ComboBox1.Items.Count - 1 To 0 Step -1
            If ComboBox2.Items.Contains(ComboBox1.Items(i))
                ComboBox1.Items.RemoveAt(i)
            End If
        Next

        If ComboBox1.Items.Count = 0
            ComboBox1.Enabled = False
        End If
    End Sub

    Obviously that's written by memory but it'd be something like that.

    The idea is that you iterate through the list of items in the first combo in reverse order and IF that item is in the list of items in the second combo, then remove it.

    Am I close or not at all?


    Please call me Frank :)

    Sunday, February 16, 2014 12:38 AM
  • Frank, 


    I realize now, because it gave error or duplicated situations. Simply because they did not meet some combobox.  Designated a bug. 

    Therefore one of its intermediate responses along with all the rest were valuable my dear friend. 

    I also appreciate all the work and effort of everyone involved here. 

    Big hug my friend and good night.
    • Proposed as answer by Frank L. Smith Sunday, February 16, 2014 1:01 AM
    Sunday, February 16, 2014 12:59 AM
  • Frank, 


    I realize now, because it gave error or duplicated situations. Simply because they did not meet some combobox.  Designated a bug. 

    Therefore one of its intermediate responses along with all the rest were valuable my dear friend. 

    I also appreciate all the work and effort of everyone involved here. 

    Big hug my friend and good night.

    Great then!

    I'm glad you got it resolved. :)


    Please call me Frank :)

    Sunday, February 16, 2014 1:01 AM
  • Frank, 


    I realize now, because it gave error or duplicated situations. Simply because they did not meet some combobox.  Designated a bug. 

    Therefore one of its intermediate responses along with all the rest were valuable my dear friend. 

    I also appreciate all the work and effort of everyone involved here. 

    Big hug my friend and good night.


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.

    Sunday, February 16, 2014 1:30 AM