I use a combobox wherein the user may type his value or select from the dropdownlist. In this schenario, everytime when the user opens the form, I want the combobox control to reset/refresh (I mean no value should be displayed). Now I have my first value selected always. Please help me out. Thnx.
Am posting my sample code below. Please check that out.
-----------------------------------------------------------------------------------------------
Public Class Form2
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.MyTableTableAdapter.Fill(Me.MyDatabaseDataSet.MyTable)
ComboBox.Text = ""
End Sub
Private Sub ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox.SelectedIndexChanged
'am doing some actions here.....
End Sub
End Class
-----------------------------------------------------------------------------------------------
I would comment out everything except the Form Load event and see how that effects the result
Just leave this as an active event
Code:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.MyTableTableAdapter.Fill(Me.MyDatabaseDataSet.MyTable)
ComboBox.Text = ""
End Sub
It works fine. But after I type some value close the form and then reopen the form then the first value from the dropdownlist is getting displayed. Actually I want this control to be blank whenever I close and open the form. Hope am clear.
Private Sub Form2_Activated (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.MyTableTableAdapter.Fill(Me.MyDatabaseDataSet.MyTable)
ComboBox.Text = ""
End Sub
By doing this it should load and clear the combobox each time the form is activated (rather than just once - only when it is loaded the first time)
No luck! It still displays the first value from the dropdownlist. Is it because of the actions that am doing inside the 'ComboBox_SelectedIndexChanged'. I have given below the complete and final code. I kindly request you to check out. Thank you.
************************************************************
Public Class Form2
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'This is Cancel button
Me.Close()
End Sub
Private Sub Form2_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.MyTableTableAdapter.Fill(Me.MyDatabaseDataSet.MyTable)
ComboBox.Text = ""
End Sub
Private Sub ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox.SelectedIndexChanged
If (ComboBox.Text = "xxxx xxxx xxxx xxxx xxxx") Then
' .... am doing some actions here.....
End If
Use selected index property as combobox style is dropdownlist.
Code:
Public Class Form1
Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\attendence.mdb;Persist Security Info=False")
Dim dt As New DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim adapter As New OleDb.OleDbDataAdapter("SELECT * FROM student", conn)
conn.Open()
adapter.Fill(dt)
dt.TableName = "Student"
conn.Close()
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "rno"
ComboBox1.SelectedIndex = -1
End Sub
End Class
Private Sub ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox.SelectedIndexChanged
If (ComboBox.Text = "xxxx xxxx xxxx xxxx xxxx") Then
' .... am doing some actions here.....
End If
You tell me that if this event is not in the code then everything is fine -
So .... why do you need THIS event - I'm sure there are other ways to do what you are doing in THIS event
Also, you may like to explain what you are trying to achieve overall - there may be a better way to deal with your problem.
The fact you are having big problems with a rather basic control (ComboBox) indicates that you just may be trying to do something it was not designed to do
Bookmarks