简体   繁体   中英

How to refresh a user control every time a button is clicked

I have a series of menu options which are all individual user controls on a windows form application.

在此处输入图片说明

How do I refresh the user control so that if for example I added a new person to my txt file, when I click the Birthdays button, it preforms all the functions within the Birthday User control again on the file with the new person added.

在此处输入图片说明

What's happening now is when I add a new person to my txt file, The user controls don't refresh therefore the Data.updatedata() method isn't called and the data is not updated.

Is there a particular event or method that I could use in order to refresh the user control when clicked?

I have tried using birthdayUserControl1.refresh() in the main form

namespace Project
{
    public partial class ChildrenUi : Form
    {

        public ChildrenUi()
        {
            InitializeComponent();
            homeUserControl1.BringToFront();


        }

        private void button2_Click(object sender, EventArgs e)
        {
            birthdaysUserControl1.Refresh();
            birthdaysUserControl1.BringToFront();
        }

     }

}

I have only just started learning about Winforms and came across Data Binding using XAML/XML files on similar questions regarding refreshing user controls however I don't know much about XAML/XML and I would imagine i'd have to redesign a good portion of my project to facilitate that. I'm using a text file.

Refreshing whole birthdaysUserControl1 won't refresh inner ListBox datasource, you need to manually refresh it.

  private void button2_Click(object sender, EventArgs e)
        {
            birthdaysUserControl1.RefreshList();
        }

And inside birthdaysUserControl1:

 public void RefreshList()
        {
            listbox1.DataSource=null;       
            listbox1.DataSource=UpcominBdays; 
        }

To watch the contents of your textfile, you can use the System.IO.FileSystemWatcher class. The Changed -Event informs your application whenever the content of the watched file is changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM