Linked Questions

Popular Questions

Select object and edit on form

Asked by At

I have a class an some objects from class. Now I want to select one of this object and display on my form. At the same time I want to edit the selected one. I used INotifyPropertyChanged and able to display selected object. Bu I have some troubles.

1- When I use myDislayingObject = myObject1 It does not work. So I have to use

myDislayingObject.property1 = myObject1.property1 
myDislayingObject.property2 = myObject1.property2

I want to copy my object with all properties with one equality including events etc.

2- I am displaying properties on textboxes. When I edit the textboxes I does not changes the source object.

namespace DisplayObjectsInForm
{
    public partial class Form1 : Form
    {

        public Araba Araba1 = new Araba();
        public Araba Araba2 = new Araba();
        public Araba Araba3 = new Araba();

        public Araba DisplayingAraba = new Araba();

        public Form1()
        {
            InitializeComponent();

            Araba1.sName = "Araba1";
            Araba1.sColor = "Kirmizi";
            Araba1.nModel = 1999;

            Araba2.sName = "Araba2";
            Araba2.sColor = "Mavi";
            Araba2.nModel = 2005;

            Araba3.sName = "Araba3";
            Araba3.sColor = "Gri";
            Araba3.nModel = 2018;

            textBox1.DataBindings.Add("Text", DisplayingAraba, "sName");
            textBox2.DataBindings.Add("Text", DisplayingAraba, "sColor");
            textBox3.DataBindings.Add("Text", DisplayingAraba, "nModel");
        }

        public class Araba : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;

            private void NotifyPropertyChanged(string property)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(property));
                }
            }

            public string sNameInternal;
            public string sName
            {
                get
                {
                    return sNameInternal;
                }
                set
                {
                    if (sNameInternal != value)
                    {
                        sNameInternal = value;
                        NotifyPropertyChanged("sName");
                    }
                }
            }

            public string sColorInternal;
            public string sColor
            {
                get
                {
                    return sColorInternal;
                }
                set
                {
                    if (sColorInternal != value)
                    {
                        sColorInternal = value;
                        NotifyPropertyChanged("sColor");
                    }
                }
            }


            public int nModelInternal;
            public int nModel
            {
                get
                {
                    return nModelInternal;
                }
                set
                {
                    if (nModelInternal != value)
                    {
                        nModelInternal = value;
                        NotifyPropertyChanged("nModel");
                    }
                }
            }



        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch(comboBox1.SelectedIndex)
            {
                case 0:
                    {
                        DisplayingAraba.sName = Araba1.sName;
                        DisplayingAraba.sColor = Araba1.sColor;
                        DisplayingAraba.nModel = Araba1.nModel;
                        break;
                    }
                case 1:
                    {
                        DisplayingAraba.sName = Araba2.sName;
                        DisplayingAraba.sColor = Araba2.sColor;
                        DisplayingAraba.nModel = Araba2.nModel;
                        break;
                    }
                case 2:
                    {
                        //Not working
                        DisplayingAraba = Araba3;
                        break;
                    }
            }
        }
    }
}

Related Questions