?
Solved

How to Set Combobox selectedIndex property to -1?

Posted on 2004-09-17
7
Medium Priority
?
238 Views
Last Modified: 2012-05-05
Hello Guys,

I am creating 1 windows appl. and in that i had created onw function as follows

    Public Sub PopComboBox(ByVal objCombo As ComboBox, ByVal sTableName As String, ByVal sDisplayFieldName As String, ByVal sReferFieldName As String, Optional ByVal sAnyOtherField As String = "", Optional ByVal blnAddAll As Boolean = False)
        Try
            Dim SqlQuery As String = "Select " & Trim(sReferFieldName) & "," & Trim(sDisplayFieldName)
            If sAnyOtherField <> "" Then
                SqlQuery = SqlQuery & "," & Trim(sAnyOtherField)
            End If
            SqlQuery = SqlQuery & " FROM " & Trim(sTableName)
            Dim sSortOrder As String = sDisplayFieldName & " ASC"
            Dim SqlGenCmd As New SqlCommand(SqlQuery)
            Dim sqlDAGenCode As New SqlDataAdapter(SqlGenCmd)
            Dim dsForCombo As New DataSet()
            Dim dvForCombo As DataView
            If HasConnected = False Then
                OpenConnection(sDBName)
            End If
            SqlGenCmd.Connection = sqlConn
            sqlDAGenCode.Fill(dsForCombo, sTableName)

            dvForCombo = New DataView(dsForCombo.Tables(sTableName), "", sSortOrder, DataViewRowState.OriginalRows)
            objCombo.DataSource = dvForCombo
            objCombo.DisplayMember = sDisplayFieldName
            objCombo.ValueMember = sReferFieldName
            If blnAddAll = True Then
                Dim dRow As DataRow = dsForCombo.Tables(sTableName).NewRow
                dRow = dsForCombo.Tables(0).NewRow()
                dRow(sDisplayFieldName) = "-----ALL-----"
                dRow(sReferFieldName) = 0
                dsForCombo.Tables(0).Rows.InsertAt(dRow, 0)
            End If
            sqlDAGenCode.Dispose()
            dsForCombo.Dispose()
        Catch SqlEx As SqlException
            MsgBox(SqlEx.Message)
        Catch Proex As Exception
            MsgBox(Proex.Message)
        End Try
    End Sub

...... This function is called from the windows show events with the combobox and other parameters.

Now at the load event i written the code as
cmbMemberName.SelectedIndex=-1 .... but it is not working


please suggest me.

Thankx in advance.

Sanjeev
0
Comment
Question by:svgharmode
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Expert Comment

by:Kaarthick
ID: 12083662
tru this code instead of yours for the selection

cmbMemberName.Items.FindByValue(-1).Selected = True,

if it throws error then try this, cmbMemberName.Items.FindByValue("-1").Selected = True
0
 
LVL 10

Expert Comment

by:prakash_prk
ID: 12084257

put you rcode in form activate event

regards
prakash
0
 

Author Comment

by:svgharmode
ID: 12090726
Hello

Thankx for reply , but i dont want to select any vaue from combo box. rather i want to make combo box balnk when form is activatevated.

i add foolwing code to my form show Event()

PopComboBox (cmbMName,"MembeMaster","MName","MCode",,True)

It will fill the combobox and at time of load it will show the 0th record of combobox . i want there -1

cmbMName.SelectedIndex = -1 But it will not work.
it displays cmbMName's first record.

Please help for cmbMName.SelectedIndex = -1.....

Sanjeev
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Expert Comment

by:prakash_prk
ID: 12090767
set the selected index property to two times

cmbMemberName.SelectedIndex=-1
cmbMemberName.SelectedIndex=-1

regrds
prakash
0
 
LVL 4

Accepted Solution

by:
Kaarthick earned 375 total points
ID: 12090781
You try the following method. to select the default value in a combo box,

You add a default entry to combo, like '-----Chooose selection-----' and then assign the value for it as "", now make that item as the default selection when the page loads.

For example,

cmbMemberName.Items.Add("----Choose selection----")
cmbMemberName.Items(cmbMemberName.Items.Count - 1).Value = ""
cmbMemberName.Items(cmbMemberName.SelectedIndex).Selected = False
cmbMemberName.Items.FindByValue("").Selected = True

NOTE: Place the above code after you have loaded the actual values into the combobox. i.e. after binding the database values.
0
 

Author Comment

by:svgharmode
ID: 12119797
Hello Prakash,kaarthic

Thank u for replying,

But u r giving solution still not working. not able to set selectedindex = -1. Ok let me try again then if i got the solution i will tell u.

Sanjeev.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question