Francesco Geri

Il blog di Francesco Geri
posts - 94, comments - 165, trackbacks - 2

Stranezze delle ComboBox: impostare il ToUpper di un suo elemento

Oggi mi sono imbattuto in quella che a me pare una stranezza.

Ho una windows form con una ComboBox in stile DropDownList, definita ad esempio nel modo seguente:

 

    1 Me.ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList

    2 Me.ComboBox1.FormattingEnabled = True

    3 Me.ComboBox1.Items.AddRange(New Object() {"valore 1", "valore 2", "valore 3"})

    4 Me.ComboBox1.Location = New System.Drawing.Point(43, 34)

    5 Me.ComboBox1.Name = "ComboBox1"

    6 Me.ComboBox1.Size = New System.Drawing.Size(149, 21)

    7 Me.ComboBox1.TabIndex = 0

 

Vorrei modificare un suo item, impostando l'uppercase del valore. Il problema è che se si assegna a ComboBox1.Items(index) un valore che differisce da quello corrente solo per il case dei caratteri allora l'operazione non ha effetti (se non quello di generare un evento di SelectedIndexChanged)...

Per provare aggiungo un pulsante nella mia form:

image

E scrivo il seguente codice (senza l'istruzione alla riga 7 non funzionerebbe!):

 

    1 Public Class Form1

    2 

    3   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    4     If Me.ComboBox1.SelectedIndex < 0 Then Return

    5     Dim v As String = Me.ComboBox1.Items(Me.ComboBox1.SelectedIndex)

    6     ' Aggiunge un carattere fittizio per forzare la modifica

    7     Me.ComboBox1.Items(Me.ComboBox1.SelectedIndex) = "~" & v.ToUpper

    8     ' Esegue la modifica desiderata

    9     Me.ComboBox1.Items(Me.ComboBox1.SelectedIndex) = v.ToUpper

   10   End Sub

   11 

   12 End Class

Print | posted on lunedì 8 settembre 2008 00:48 | Filed Under [ Tips .Net ]

Powered by:
Powered By Subtext Powered By ASP.NET