Solved

How to Set Combobox selectedIndex property to -1?

Posted on 2004-09-17
7
203 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 125 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
 
LVL 10

Expert Comment

by:prakash_prk
ID: 12119820
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now