The IndexOf method of the Items property of an UltraComboEditor is not finding an item that is obviously in the ComboEditor. An index of -1 is being returned.
Here is some test code:
Form File:
Public Class Form1
Private Sub UltraButton1_Click(sender As Object, e As EventArgs) Handles UltraButton1.Click
Const f As String = "first"
Const s As String = "second"
Me.UltraComboEditor1.Items.Add(f)
Me.UltraComboEditor1.Items.Add(s)
Console.WriteLine(Me.UltraComboEditor1.Items.Count)
Console.WriteLine("Item-1: " + Me.UltraComboEditor1.Items(1).DataValue.ToString() + " " + Me.UltraComboEditor1.Items(1).DisplayText)
Try
Dim index As String = Me.UltraComboEditor1.Items.IndexOf(s)
Console.WriteLine("Index: " + index.ToString())
Catch ex As Exception
Console.WriteLine(e.ToString())
End Try
End Sub
End Class
---------------------------------------
Designer File:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.UltraComboEditor1 = New Infragistics.Win.UltraWinEditors.UltraComboEditor()
Me.UltraButton1 = New Infragistics.Win.Misc.UltraButton()
CType(Me.UltraComboEditor1,System.ComponentModel.ISupportInitialize).BeginInit
Me.SuspendLayout
'
'UltraComboEditor1
Me.UltraComboEditor1.Location = New System.Drawing.Point(57, 32)
Me.UltraComboEditor1.Name = "UltraComboEditor1"
Me.UltraComboEditor1.Size = New System.Drawing.Size(144, 21)
Me.UltraComboEditor1.TabIndex = 0
'UltraButton1
Me.UltraButton1.Location = New System.Drawing.Point(126, 172)
Me.UltraButton1.Name = "UltraButton1"
Me.UltraButton1.Size = New System.Drawing.Size(75, 23)
Me.UltraButton1.TabIndex = 1
Me.UltraButton1.Text = "UltraButton1"
'Form1
Me.AutoScaleDimensions = New System.Drawing.SizeF(6!, 13!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 262)
Me.Controls.Add(Me.UltraButton1)
Me.Controls.Add(Me.UltraComboEditor1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.UltraComboEditor1,System.ComponentModel.ISupportInitialize).EndInit
Me.ResumeLayout(false)
Me.PerformLayout
Friend WithEvents UltraComboEditor1 As Infragistics.Win.UltraWinEditors.UltraComboEditor
Friend WithEvents UltraButton1 As Infragistics.Win.Misc.UltraButton
Output from the code:
2
Item-1: second second
Index: -1
Hi Andrew,
I'm glad you found a solution. Just in case you are wondering, the reason that your original code did not work is that the Items collection contains a collection of ValueListItem objects. When you add an item and pass in a string, this creates a new ValueListItem with that string as it's DataValue. The list does not contains strings, so the IndexOf method looks for a ValueListItem that matches the object you passed in. Since you are passing in a string, and the list does not contain any objects of type String, that will never find a match because a string is not a ValueListItem.
FindString should work fine in this case, but keep in mind that FindString searches the DisplayText of the items. In your case, you are not setting the DisplayText, so it will fall back to the DataValue.ToString(), which in this case makes no difference. But if you decide, in the future, to add items that have both a DataValue and a DisplayText, FindString will be searching the DisplayText.
I found my own answer. To find the index I needed to use Me.UltraComboEditor1.Items.ValueList.FindString(s).