Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3300496
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:40:05+00:00 2026-05-17T20:40:05+00:00

I have a class that implements the INotifyPropertyChanged interface. If I create a new

  • 0

I have a class that implements the INotifyPropertyChanged interface.
If I create a new instance of the object, the PropertyChanged event gets set after I retrieve a value from it.

Example:

MyItem itm = new MyItem();  //MyItem.PropertyChanged == null
string test = itm.Value;    //MyItem.PropertyChanged != null

If I assign itm the value of another MyItem, the PropertyChanged event remains null.

Example:

itm = (MyItem)cboMyItemsCombobox.SelectedItem; // Properties for itm change to the values 
                                               // of the selected item, but PropertyChanged 
                                               // == null

I believe the problem lies partially in my custom constructor for the class, but I’m not entirely sure.

The goal is to have a variable to hold data for a record, called mnuitm, that is bound to
3 textbox objects. When the text in a textbox changes, the change is made to the property in mnuitm. When the property in mnuitm is changed, the change is made in the textbox.
This works if I create a new MenuItem and assign the values individually, but does not work if I assign an already populated MenuItem to mnuitm.

Here is my actual code for (hopefully) more clearity on the issue.

public partial class frmMenuItems : Form
{
    private class MenuItem : INotifyPropertyChanged
    {
        private Int32 mid;
        private string txt;
        private string url;
        private string scp;

        public MenuItem() { }

        public MenuItem(Int32 id, string txt, string url, string scp)
        {
            ID = id;
            Text = txt;
            URL = url;
            Script = scp;
        }

        public Int32 ID
        {
            get
            {
                return mid;
            }
            set
            {
                if (mid != value)
                {
                    mid = value;
                    NotifyPropertyChanged("ID");
                }
            }
        }

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

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

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

        public void Clear()
        {
            ID = 0;
            Text = "";
            URL = "";
            Script = "";
        }

        public override string ToString()
        {
            return Text;
        }

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

        public event PropertyChangedEventHandler PropertyChanged;
    }


    private MenuItem mnuitm;
    private MySqlConnection sqlcon;

    public frmMenuItems()
    {
        InitializeComponent();
    }

    private void btnClose_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        mnuitm.Clear();
    }

    private void frmMenuItems_Load(object sender, EventArgs e)
    {
        string constr = "server={0};uid={1};pwd={2};database={3};";
        DBItem dbi = CountyDataManager.CountyData.DBConnection;
        constr = string.Format(constr, [MyHost], [MyUsername], [MyPassword], [MyDatabase]);
        sqlcon = new MySqlConnection(constr);
        sqlcon.Open();

        mnuitm = new MenuItem();
        SetBindings();
        RefreshList();
    }

    private void SetBindings()
    {
        txtMenuText.DataBindings.Clear();
        txtURL.DataBindings.Clear();
        txtScript.DataBindings.Clear();

        txtMenuText.DataBindings.Add("Text", mnuitm, "Text");
        txtURL.DataBindings.Add("Text", mnuitm, "URL");
        txtScript.DataBindings.Add("Text", mnuitm, "Script");
    }

    private void RefreshList()
    {
        using (MySqlCommand cmd = new MySqlCommand("SELECT `menuid`,`menutext`,`url`,`script` FROM tblindexmenu ORDER BY `menutext`", sqlcon))
        {
            lstMenuItems.Items.Clear();
            using (MySqlDataReader rdr = cmd.ExecuteReader())
            {
                while (rdr.Read())
                {
                    lstMenuItems.Items.Add(new MenuItem(Int32.Parse(rdr[0].ToString()), rdr[1].ToString(),rdr[2].ToString(),rdr[3].ToString()));
                }
            }
        }
    }

    private void frmMenuItems_FormClosing(object sender, FormClosingEventArgs e)
    {
        sqlcon.Close();
    }

    private void lstMenuItems_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lstMenuItems.SelectedIndex > -1)
        {
            mnuitm = (MenuItem)lstMenuItems.SelectedItem;
        }
    }
}   

After receiving feedback, I made the following changes:

Added CopyFrom() to MenuItem

public void CopyFrom(MenuItem itm)
{
    this.ID = itm.ID;
     this.URL = itm.URL;
     this.Text = itm.Text;
     this.Script = itm.Script;
}

I then modified the SelectedIndexChanged code to use the new function

mnuitm.CopyFrom((MenuItem)lstMenuItems.SelectedItem);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:40:05+00:00Added an answer on May 17, 2026 at 8:40 pm

    This is by design. When you write

    itm = (MyItem)cboMyItemsCombobox.SelectedItem;
    

    you haven’t changed any of the properties of the MenuItem itm used to point to, rather you changed the MenuItem itm points to.

    One option for what you need is add a function to MenuItem that looks like

    SetFromOtherMenuItem(MenuItem other)
    {
        this.Url = other.Url
        //etc
    }
    

    Again, PropertyChanged means that a property on some instance has changed. In your case, only one of the variables pointing to that instance changed (to point to a different instance).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.