This is a migrated thread and some comments may be shown as answers.

Bogus Routed events on comboboxes

1 Answer 43 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 2
OverCoded asked on 05 Aug 2010, 07:55 PM
I have and application with a user control that is a font chooser. It is nothing more than a single row of comboboxes. This user control (FontChooser) raises a selectionChangedEvent when any of the comboboxes are changed.
In my parent program I use a foreach loop to create a new FontChooser with a unique name for each item it is associated with (see code). So, for example I have eleven items, each with a FontChooser control  fc_A - K.
I have an event hander that handles the SelectionChangedEvent for all of the FontChoosers. When an event is fired I use the e.OrginalSource to capture which FontChooser fc_A -K  is being changed ex: FontChooser fc = (e.OriginalSource as FontChooser);. However, no matter which FontChooser is changed the last one added shows up as the OriginalSource. So, If where to write the fc name to the Console with Console.Out.WriteLine( fc.Name); I will get something like fc_K and fc_B, or fc_K and fc_K. The event seems to fire twice and always with the last FontChooser added to the parent app. BTW,I have tried to use e.Source and Sender as FontChooser, all produce the same result. If anyone has any ideas I am all ears.
fontChooser = new FontChooser();                        
                        fontChooser.selectedFont = row.ItemArray[3]; //From dataTable
                        fontChooser.selectedStyle = row.ItemArray[5].ToString().Trim();
                        fontChooser.selectedSize = row.ItemArray[4];
                        fontChooser.selectedWeight = FontWeight.FromOpenTypeWeight( Int32.Parse(row.ItemArray[7].ToString()));
                        fontChooser.Name = "fc_" + row.ItemArray[0].ToString().Trim();
                        fontChooser.SelectionChanged += new SelectionChangedEventHandler(fontChooser_SelectionChanged);
                        grid.RegisterName(fontChooser.Name, fontChooser);
                        Grid.SetRow(fontChooser, i);
                        Grid.SetColumn(fontChooser, 3);
                        Grid.SetColumnSpan(fontChooser, 4);
//Event Handler
 void fontChooser_SelectionChanged(object sender, SelectionChangedEventArgs e)
            FontChooser fc = (e.OriginalSource as FontChooser);
            selectedFont = fc.selectedFont;
            selectedFontStyle = fc.selectedStyle;
            FontWeight weight = (FontWeight)fc.selectedWeight;
            selectedFontWeight = weight;
            Console.Out.WriteLine(fc.Name); //Added to see what is going wrong
            string name = fc.Name.Substring(3);
            activeControl = name;
            string filterExpr = "LABEL_NAME = '" + name + "'";
            DataRow[] rowToUpdate = tempTable.Select(filterExpr);
            int ptr = tempTable.Rows.IndexOf(rowToUpdate[0]);
            DataRow dr = rowToUpdate[0];
            dr[3] = selectedFont;
            if (fc.selectedSize != null
                selectedFontSize = fc.selectedSize;
                dr[4] = selectedFontSize;
            dr[5] = selectedFontStyle;            
            dr[7] = weight.ToOpenTypeWeight();            
            tempTable.Rows[ptr].ItemArray = dr.ItemArray;

1 Answer, 1 is accepted

Sort by
Top achievements
Rank 2
answered on 05 Aug 2010, 09:47 PM
General Discussions
Asked by
Top achievements
Rank 2
Answers by
Top achievements
Rank 2
Share this question