combobox - vb.net - set default value to show in dropdown when loaded

34386
12
Jump to solution
01-03-2013 04:53 AM
maxsteinbrenner
New Contributor II
so i have a add-in combobox and would like it to have the text "select storm" in the (currently blank) dropdown when the combo box is displayed. is this possible?

thanks!

max
0 Kudos
12 Replies
RichardMoussopo
Occasional Contributor III
Does anyone ever find a solution to this post? it's been an hour I am trying to figure out how to set the default selected value to the first item in the comboBox. Also, on the Update event, check if we have layers in toc to disable or enable the comboBox.


  Public Sub New()
        Dim mymap As IMap = mxDoc.FocusMap

        Try

            If mymap.LayerCount > 0 Then

                Me.Enabled = True

                For i As Integer = 0 To mymap.LayerCount - 1
                    Me.Add(mymap.Layer(i).Name)
                Next

                'Set the default selected value in the comboBox

                Me.Select(0) 'This line here won't work. I wish we could have the selectedIndex instance as on a normal window's form

            Else
                Me.Enabled = False

            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

0 Kudos
by Anonymous User
Not applicable

I had a hard time with the add-in combox box also.

Programmatically selecting the item was easy enough but it wouldn't show the value of that item in the textbox unless I explicitly set the value property.

Here's some c# code from 10.0 that creates a combo box containing the three z capture types for the editor. The actual z capture type is then set when the mxd is opened. This behavior may have changed in later versions but I haven't tried it recently.


    public ZCaptureModeCombo()
    {
      _cz = this.Add("CurrentZ");
      _iz = this.Add("InterpolateZ");
      _sz = this.Add("SurfaceZ");


      _editor = ArcMap.Editor;
      _editorZ = _editor as IEditorZ;


      _docEvents = ArcMap.Document as IDocumentEvents_Event;
      _docEvents.OpenDocument += new IDocumentEvents_OpenDocumentEventHandler(_docEvents_OpenDocument);
      _docEvents.NewDocument += new IDocumentEvents_NewDocumentEventHandler(_docEvents_NewDocument);


    }


    void switchCaptureType(esriZCaptureType ezt)
    {
      switch (ezt)
      {
        case esriZCaptureType.esriCaptureCurrentZ:
          this.Value = "CurrentZ";
          this.Select(_cz);
          break;
        case esriZCaptureType.esriCaptureInterpolateZ:
          this.Value = "InterpolateZ";
          this.Select(_iz);
          break;
        case esriZCaptureType.esriCaptureSurfaceZ:
          this.Value = "SurfaceZ";
          this.Select(_sz);
          break;
        default:
          this.Value = "CurrentZ";
          this.Select(_cz);
          break;
      }
    }


    void _docEvents_NewDocument()
    {
      switchCaptureType(_editorZ.ZCaptureType);
    }

by Anonymous User
Not applicable

Hey Sol,

I took a closer look at this and have a cookie'less solution for you.

The int's were working fine for me but I can see how it gets unwieldy with large or dynamic numbers.


Imports ESRI.ArcGIS.Desktop.AddIns
Imports ESRI.ArcGIS.Framework


Public Class Cbt
  Inherits ESRI.ArcGIS.Desktop.AddIns.ComboBox


  Public Sub New()
    'Populate the combo box with the years e.g. 1999-2014
    For i As Integer = 2014 To 1999 Step -1
      Me.Add(i.ToString())
    Next


    'Set the default value for the combo
    Dim globalDefault As String = "2000"
    SelectYear(globalDefault)


    'Get the def query year value from the layer. Assume a value.
    Dim defQueryYear As String = "2003"


    'If defquery year is in the combo box list then use that otherwise the default has been set above
    SelectYear(defQueryYear)


  End Sub


  Protected Overrides Sub OnSelChange(cookie As Integer)
    'The user has selected a year
    Dim newYear = Me.GetItem(cookie).Caption
  End Sub
  Public Sub SelectYear(year As String)
    'Select the item in the combo from the string
    For Each item As ComboBox.Item In Me.items
      If item.Caption = year Then
        Me.Select(item.Cookie)
        Exit For
      End If
    Next
  End Sub


In this example I populate the combo box with the range. You'll get the max value from your sql table.

I then set the combo box to a default value of 2000, an example of your global default.

You would then select a value based on your def query if applicable.

If the user selects a year, you can pick it up from the newYear variable in OnSelChange.

Hope this helps

Sean.