ComboBox and TextChanged again (or still)

Hi,

I see a couple of posts around this topic but oddly, not this exact question, at least not with an answer.

I created two projects, both Windows Application, one in VB 2005, one in C#. I have .NET 2.0 installed.

Each project consists of a form with a single ComboBox, and the DropDownStyle of the ComboBox is DropDown.

I need to be able to catch both changes to the selection in the list or keyboard edits in the box.

In both programs, I add an item to the drop down list when I load the form. Then, I try selecting and then editing. In VB, selecting the item first fires the ComboBox1_TextChanged sub and then the ComboBox1_SelectedIndexChanged sub. Editing fires only the
ComboBox1_TextChanged. This is fine.

In C#, however, selecting the item fires only the ComboBox1_SelectedIndexChanged method, while editing via keyboard fires nothing.

I'd like to continue to use C# here but I can't if this dowsn't work. Why isn't this behavior working in C# when it works fine in VB?

TIA.

Gabriel

In VB, I have:

Public Class Form1

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
...
End Sub

Private Sub ComboBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged
...
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("one")
End Sub
End Class

In C#, I have:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("one");
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
...
}

private void comboBox1_TextChanged(object sender, EventArgs e)
{
...
}
}
}

[2219 byte] By [gbereny] at [2007-12-23]
# 1

interesting, let me see if i can repro this in C#

Nope, works fine here. I have both selectindexchanged and textchanged events kicking in when

1) I change the selection of item in the combobox (both events are fired)

2) I type into the combobox (textchanged event fires)

both events appropriately fire correctly

you may want to remove those events in C# and recreate them, does it work?

ahmedilyas at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
Not possible by design, VB and C# use the same Windows Forms control. Check Form1.Designer.cs (Solution Explorer, Show All Files icon) and verify that the TextChanged event is being registered.
nobugz at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
Yes, thank you. This was the problem. The TextChanged event was not registered in the Designer.cs file but the SelectedIndexChanged event was.

I assume the reason was that when I double-clicked the control in the Design view, it
created the SelectedIndexChanged handler and registered the event as well but I had to
create the TextChanged handler manually and neglected to add it to Designer.cs.

gbereny at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms General...