combobox mdi load selectedIndex

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kalvin

    combobox mdi load selectedIndex

    I have seen this question raised, but I cannot find an answer.

    I have an MDI app, when I load an child form with a combobox being
    bound in the load event, it won't allow me to set selectedindex = -1.
    If it isn't an mdi child, then there isn't a problem. I have tried
    setting it to -1 two times. I have tried setting the selecteditem
    property to -1 and also to nothing. None of it seems to make a
    difference.

    Any help will be appreciated.
    Kalvin

  • Bernie Yaeger

    #2
    Re: combobox mdi load selectedIndex

    Hi Kalvin,

    Can we see some code? I have literally hundreds of such child forms and
    selectedindex = -1 (twice) works fine.

    Bernie Yaeger

    "Kalvin" <ktuel@streck.c om> wrote in message
    news:1108055538 .440071.157720@ c13g2000cwb.goo glegroups.com.. .[color=blue]
    >I have seen this question raised, but I cannot find an answer.
    >
    > I have an MDI app, when I load an child form with a combobox being
    > bound in the load event, it won't allow me to set selectedindex = -1.
    > If it isn't an mdi child, then there isn't a problem. I have tried
    > setting it to -1 two times. I have tried setting the selecteditem
    > property to -1 and also to nothing. None of it seems to make a
    > difference.
    >
    > Any help will be appreciated.
    > Kalvin
    >[/color]


    Comment

    • Kalvin

      #3
      Re: combobox mdi load selectedIndex

      When the form is not an MDI child form, this works fine and there is
      not a value in the combobox. When the form is an MDI child form, and
      this is in the Load event, then there will be a value in the combobox.

      Thank you for looking at this.

      /****** CODE STARTS HERE *******/
      Private Sub frmTest_Load(By Val sender As System.Object, ByVal e As
      System.EventArg s) Handles MyBase.Load
      Dim LotServer As New LotServer
      Dim Lots As LotCollection

      Try
      Dim conn As SqlConnection = New SqlConnection({ your
      connection})
      conn.Open()

      Dim dt As New DataTable
      Dim cmd As SqlCommand = New SqlCommand
      cmd.CommandText = {your query}
      cmd.CommandType = CommandType.Tex t
      cmd.Connection = conn

      Dim da As SqlDataAdapter = New SqlDataAdapter( cmd)

      da.Fill(dt)

      box1.DataSource = dt
      box1.ValueMembe r = "ID"
      box1.DisplayMem ber = "Name"
      box1.SelectedIn dex = -1
      box1.SelectedIn dex = -1

      conn.Close()

      Catch exc As Exception
      MessageBox.Show (exc.Message.To String)
      End Try

      End Sub

      Comment

      • Bernie Yaeger

        #4
        Re: combobox mdi load selectedIndex

        Hi Kalvin,

        OK, there are two 'answers'. When I followed your code, I experienced the
        same condition you describe. However, when I loaded the data through a
        dataset created by the vs .net wizard, it worked fine with selectedindex
        = -1 twice. Also, when I load a combobox in additem mode, using a datatable
        as you created, it was also fine.

        HTH,

        Bernie Yaeger

        "Kalvin" <ktuel@streck.c om> wrote in message
        news:1108134279 .131457.13950@z 14g2000cwz.goog legroups.com...[color=blue]
        > When the form is not an MDI child form, this works fine and there is
        > not a value in the combobox. When the form is an MDI child form, and
        > this is in the Load event, then there will be a value in the combobox.
        >
        > Thank you for looking at this.
        >
        > /****** CODE STARTS HERE *******/
        > Private Sub frmTest_Load(By Val sender As System.Object, ByVal e As
        > System.EventArg s) Handles MyBase.Load
        > Dim LotServer As New LotServer
        > Dim Lots As LotCollection
        >
        > Try
        > Dim conn As SqlConnection = New SqlConnection({ your
        > connection})
        > conn.Open()
        >
        > Dim dt As New DataTable
        > Dim cmd As SqlCommand = New SqlCommand
        > cmd.CommandText = {your query}
        > cmd.CommandType = CommandType.Tex t
        > cmd.Connection = conn
        >
        > Dim da As SqlDataAdapter = New SqlDataAdapter( cmd)
        >
        > da.Fill(dt)
        >
        > box1.DataSource = dt
        > box1.ValueMembe r = "ID"
        > box1.DisplayMem ber = "Name"
        > box1.SelectedIn dex = -1
        > box1.SelectedIn dex = -1
        >
        > conn.Close()
        >
        > Catch exc As Exception
        > MessageBox.Show (exc.Message.To String)
        > End Try
        >
        > End Sub
        >[/color]


        Comment

        • Kalvin

          #5
          Re: combobox mdi load selectedIndex



          That makes sense. I need to be able to bind it to a user defined
          collection. I have six combo boxes. The first one loads when the form
          opens, and then the others load depending on the user choice in the
          first box. What I did was is just manually load the box. Then it isn't
          bound and doesn't automatically pick the first item. I would rather be
          able to bind it like the others though.

          Thank you for your help and input, I really appreciate it. Sometimes
          it's nice just to know I'm not the only one it doesn't work right for.

          /**** CODE STARTS HERE ****/
          Friend Function LoadCategories( ByRef DropDown As ComboBox) As
          CategoryCollect ion

          'Written(By) : KTuel
          'Parameters: DropwDown - A combobox to be filled with the
          categories
          '
          'Task: Load all product categories
          '
          'Revision(Histo ry)
          '02/09/2005 - Created kTuel

          Dim CategoryServer As New CategoryServer
          Dim Categories As CategoryCollect ion

          Categories = CategoryServer. LoadAllCategori es()

          '.NET has a bug in MDI applications that when a combo box is
          bound to a source and
          'the combobox is populated in the load event of a child form,
          you can't set the
          'selectedindex to -1. Here we are populating the combobox
          without binding it
          Dim Category As Category


          If Categories.Coun t > 0 Then
          DropDown.Suspen dLayout()

          For Each Category In DirectCast(Cate gories,
          IDictionary).Va lues()
          DropDown.Items. Add(Category)
          Next Category
          DropDown.Displa yMember = "Name"
          DropDown.ValueM ember = "ID"

          DropDown.Select edIndex = -1
          DropDown.Select edIndex = -1

          DropDown.Resume Layout()

          End If

          Return Categories

          End Function

          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          Working...