Solved

How to Set Combobox selectedIndex property to -1?

Posted on 2004-09-17
7
206 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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