bonjour tous les monds,
how can i Refresh ComboBox After Adding New Values via a Seperate Form in vbnet
any ideas
mercie en avance ;

Recommended Answers

All 15 Replies

Hi,

1. In order to refresh the combobox automatically just call the form_Load() method after adding values to ur combobox, where the combobox gets loaded already in form_Load() method.

2. Or else assign the datasource property of combobox once again and use invalidate method.

'refresh the datasource
combobox.datasource=?datasource
'select the new field from the table to populate the combobox with
combobox.displaymember = "New Display Member"
'redraw the combobox to reflect the values
combobox.invalidate()

:)

or possibly

combobox.refreshitems

thanks for replay
but no way the same problem
i noticed that refresh items dosnt affect the comboBox in any way.

hello !
can you please tell me are you populating combo box from db using any method ? if yes then call that method at the load event of you form having combo box , if no then can you please rephrase your prob ,

Regards
M.Waqas Aslam

ChrisPadgham
combobox.refreshitems

AFAIK there isn't such a method in VB.NET.

how can i Refresh ComboBox After Adding New Values via a Seperate Form in vbnet

Could you post some relevant code how you reference the combobox, call the form, exit form and how you add a new value.

OK THANKS FOR REPLAY
I ATTACHED IT WITH BY A PIC PLZ OPEN IT

U WILL FIND THE MAIN FORM WITH TEXTBOXS AND (COMBOBOX TO INTER THE NAME)
IF I DIDN'T FIND THE NAME I OPEN ANOTHER FORM BY BUTTON 'F' AND ENTR THE NEW NAME , WHEN I CLOSE THAT FORM I WANT TO FIND THAT NAME I ADDED IN COMBOBOX,
THIS COMBO ATTACHED TO THE TABLE BY BINDING SOURCE AND TAHT IS THE CODE I HAVE ADDED IN LOAD FORM EEVENT
Texstn.DataSource = CombosteDS.Tables("fourni")
Texstn.DisplayMember = ("STENAME")
I TRIED TO ADD THE SAME CODE IN EVENT OF FINALIZE THE CHILD FORM BUT NO WAY
I TRIED ALSO 'THE COMBO REFRESH METHOD' BUT NO WAY

THANKS ALOT

commented: CAPS :( -3

hello !
you can do this

'use this sub to populate your combobox
sub ComboValues()
dim mycon as new sqlconnection("connection string")
dim dt as datatable
mycon.open()
dim da as new sqldataadapter("select displayfields,valuefield from table",mycon)
da.fill(dt)
combobox.datasource = dt
with combobox
 .DisplayMember = "displayfield"
 .ValueMember = "valuefields"
End Sub

call this on the load event of your form , or if you just want to open another form and then insert a new value and then want to show that value in combo then call this function on the GOtFocus event of your form having combo box

Hope this will helps you

Regards
M.Waqas Aslam

No way my friend the only way to make it ... to close the Form and reopen it !!!!!!

then just call the sub on the load event of your form

Not sure what parameters you pass between the forms, but here's one way

This is "child form"

Public Class Form2

    ' Public index for the new item
    Public comboIndex As Integer = -1
    ' Public reference to main form's combobox
    Public nameComboBox As ComboBox

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Add new name

        ' Grab the index of the new item
        comboIndex = nameComboBox.Items.Add(TextBox1.Text)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ' Close child form button

        ' Hide the form, do not destroy it
        Me.Hide()

    End Sub

End Class

and the main points are passing combo box in to form, returning comboboxes index and hiding the form.

Here's the code to create a new child form, passing parameter in, getting output parameter, disposing child form and displaying the new item in the combobox

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    '
    Dim oForm As Form2 ' Replace Form2 with your child form's type
    Dim newIndex As Integer ' Get the index of new item in the combobox

    ' Create a new child form instance
    oForm = New Form2()
    ' Pass the combo box as input
    oForm.nameComboBox = ComboBox1 'Texstn
    ' Display child form (as modal form)
    oForm.ShowDialog()
    ' At this point execution returns from the child form, get the index
    newIndex = oForm.comboIndex
    ' Now dispose form
    oForm = Nothing
    ' Validate returned index and if valid, show the item
    If newIndex >= 0 AndAlso newIndex < ComboBox1.Items.Count Then
        ComboBox1.SelectedIndex = newIndex
    End If

End Sub

HTH

thank alot teme you are the best
its v good way,, i got it and the proplem got solved
thnks

you can use the property of the combobox... you can use refresh or update

i tried it cyberdaemon but it without any affect

Solution is very simple. This is what I done in my code :

                    'Set datasource to Nothing 
                    Me.cmbWarehouse.DataSource = Nothing
                    Me.cmbWarehouse.Items.Clear()

                   'Set datasource again
                    Me.cmbWarehouse.DataSource = CurrentWarehouseColl
                    Me.cmbWarehouse.DisplayMember = "WarehouseName"
                    Me.cmbWarehouse.ValueMember = "ID"
                    Me.cmbWarehouse.SelectedValue = CurrentWarehouse.ID

DataSource in my case was class collection named "CurrentWarehouseColl"

This is what i did and it worked. Maybe is not the best way, but...

frmCompra is the form where i have my combobox with items
frmMantProducto is the form where i can create o delete items.

frmMantProducto after creating or deleting an item.

Private Sub CargarProductos_Compra()    
        Dim da_productos As New SqlDataAdapter("SELECT * FROM PRODUCTO ORDER BY DESCRIPCION", cn)
        Dim dtb_productos As New DataTable
        da_productos.Fill(dtb_productos)
        frmCompra.cboProducto.DataSource = dtb_productos
        frmCompra.cboProducto.DisplayMember = "DESCRIPCION"
        frmCompra.cboProducto.ValueMember = "DESCRIPCION"
        frmCompra.cboProducto.SelectedIndex = -1
        End Sub

    Private Sub frmMantProducto_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

        If MsgBox("¿Desea salir del formulario?", MsgBoxStyle.OkCancel, "Advertencia") = MsgBoxResult.Ok Then
            Me.Dispose()
            If frmCompra.Visible = True Then
                frmCompra.cboProducto.DataSource = Nothing
                CargarProductos_Compra()
            End If
        Else
            e.Cancel = True
        End If

    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.