Click here to Skip to main content
15,920,438 members
Home / Discussions / C#
   

C#

 
QuestionContextMenu Help Pin
Harvey Saayman19-Aug-08 3:21
Harvey Saayman19-Aug-08 3:21 
AnswerRe: ContextMenu Help Pin
Abhijit Jana19-Aug-08 7:03
professionalAbhijit Jana19-Aug-08 7:03 
GeneralRe: ContextMenu Help Pin
Harvey Saayman19-Aug-08 9:02
Harvey Saayman19-Aug-08 9:02 
AnswerRe: ContextMenu Help Pin
DaveyM6919-Aug-08 12:41
professionalDaveyM6919-Aug-08 12:41 
GeneralRe: ContextMenu Help Pin
Harvey Saayman19-Aug-08 20:36
Harvey Saayman19-Aug-08 20:36 
GeneralRe: ContextMenu Help Pin
DaveyM6920-Aug-08 2:46
professionalDaveyM6920-Aug-08 2:46 
GeneralRe: ContextMenu Help Pin
Harvey Saayman20-Aug-08 2:57
Harvey Saayman20-Aug-08 2:57 
QuestionProblem with databinding to property of interface type. Pin
Member 140473319-Aug-08 3:08
Member 140473319-Aug-08 3:08 
I have a challenge to you all!

I have found a strange (and really annoying!!) behavior when trying to databind a combobox to a property witch is pointing to an interface.

I have the following interface and class:

IPerson.cs:

public interface IPerson
{
   string Name { get; set; }
}


Person.cs:
public class Person : IPerson
{
   public Person(string name)
   {
      Name = name;
   }

   public string Name { get; set; }

   public override string ToString()
   {
      return Name;
   }
}


Then I have a form witch contains one ComboBox (comboBox1) and one TextBox (textBox1). The Form1.cs looks like this:

public partial class Form1 : Form
{
   public Form1()
   {
      InitializeComponent();

      this.CurrentPerson = new Person("Person 1");
      comboBox1.Items.Add(this.CurrentPerson);
      comboBox1.Items.Add(new Person("Person 2"));
      comboBox1.Items.Add(new Person("Person 3"));
      comboBox1.Items.Add(new Person("Person 4"));

      comboBox1.DataBindings.Add("SelectedItem", this, "CurrentPerson");
      textBox1.DataBindings.Add("Text", CurrentPerson, "Name");
   }

   public IPerson CurrentPerson { get; set; }
}


Now to the strange part. If you run the application, change the combobox to "Person 2", and focus the textbox so that the databinding should be triggered, you will see that the combobox is reset to "Person 1"! Why isn't the CurrentPerson changed to "Person 2" instead?

If you let CurrentPerson be a Person, not an IPerson, everything works just fine.

And one more thing... If you add a SelectedIndexChanged event to the combobox, and let it do the databinding, you will find that it is impossible to change the selected item!
Code:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
   Console.WriteLine("Before: Combo:" + comboBox1.SelectedItem.ToString() + 
      ", Current Person: " + CurrentPerson.ToString());
   comboBox1.DataBindings[0].WriteValue();
   Console.WriteLine("After: Combo:" + comboBox1.SelectedItem.ToString() + 
      ", Current Person: " + CurrentPerson.ToString());
}


If you try to change the selected item from "Person 1" to "Person 3", you get this output:
Before: Combo: Person 3, Current Person: Person 1<br />
Before: Combo: Person 1, Current Person: Person 1<br />
After: Combo: Person 1, Current Person: Person 1<br />
After: Combo: Person 1, Current Person: Person 1<br />

Expected would be:
Before: Combo: Person 3, Current Person: Person 1<br />
After: Combo: Person 3, Current Person: Person 3


Anyone have any clue what I can do to make this work? (Besides doing the binding manually, of cource...)

Thanks in advance! Smile | :)
AnswerRe: Problem with databinding to property of interface type. Pin
MidwestLimey19-Aug-08 4:37
professionalMidwestLimey19-Aug-08 4:37 
GeneralRe: Problem with databinding to property of interface type. Pin
Member 140473319-Aug-08 19:43
Member 140473319-Aug-08 19:43 
AnswerRe: Problem with databinding to property of interface type. Pin
Member 140473320-Aug-08 21:14
Member 140473320-Aug-08 21:14 
QuestionMicrosoft.SqlServer.Management.Smo problem Pin
hdv21219-Aug-08 2:58
hdv21219-Aug-08 2:58 
AnswerRe: Microsoft.SqlServer.Management.Smo problem Pin
Csharp_Raja19-Aug-08 3:42
Csharp_Raja19-Aug-08 3:42 
GeneralRe: Microsoft.SqlServer.Management.Smo problem Pin
hdv21219-Aug-08 4:08
hdv21219-Aug-08 4:08 
GeneralRe: Microsoft.SqlServer.Management.Smo problem Pin
Csharp_Raja19-Aug-08 4:50
Csharp_Raja19-Aug-08 4:50 
GeneralRe: Microsoft.SqlServer.Management.Smo problem Pin
hdv21219-Aug-08 4:55
hdv21219-Aug-08 4:55 
GeneralRe: Microsoft.SqlServer.Management.Smo problem Pin
Csharp_Raja20-Aug-08 4:34
Csharp_Raja20-Aug-08 4:34 
Questionout of memory:at System.image.fromfile(string filename,boolean useembeddedcolormanagement) Pin
vishnukamath19-Aug-08 2:44
vishnukamath19-Aug-08 2:44 
AnswerRe: out of memory:at System.image.fromfile(string filename,boolean useembeddedcolormanagement) Pin
Manas Bhardwaj19-Aug-08 5:16
professionalManas Bhardwaj19-Aug-08 5:16 
QuestionAccess to the file in the same directory Pin
arkiboys19-Aug-08 2:17
arkiboys19-Aug-08 2:17 
AnswerPlease don't repost questions just to bump them to the top. Pin
Pete O'Hanlon19-Aug-08 2:21
mvePete O'Hanlon19-Aug-08 2:21 
GeneralRe: Please don't repost questions just to bump them to the top. Pin
arkiboys19-Aug-08 2:26
arkiboys19-Aug-08 2:26 
RantRe: Please don't repost questions just to bump them to the top. Pin
Paul Conrad19-Aug-08 6:18
professionalPaul Conrad19-Aug-08 6:18 
QuestionHow to create a progressBar? Pin
Admin88719-Aug-08 2:00
Admin88719-Aug-08 2:00 
AnswerRe: How to create a progressBar? Pin
Pete O'Hanlon19-Aug-08 2:06
mvePete O'Hanlon19-Aug-08 2:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.