will mannix
will mannix

Reputation: 153

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.

enter image description here

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.

enter image description here

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.

Upvotes: 1

Views: 9434

Answers (2)

kaqq
kaqq

Reputation: 365

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; 
        }

Upvotes: 1

Björn Albowitz
Björn Albowitz

Reputation: 53

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.

Upvotes: 0

Related Questions