How to refresh a user control every time a button is clicked - c#

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.

Related

How do I use data from a combo box in another form c#

I'm writing a program in c# (visual studio, windows forms) which involves the user selecting a name from a combo box, and then clicking a button which brings them to a quiz on another form. I want a text file to be created at the end of the quiz showing the name selected and the quiz results, followed by a "~" symbol.
I honestly don't know where to start. I'm using stream reader.
This is the code I used for the combo box and the button that sends you to the next form
private void quizSelectPupilCB_SelectedIndexChanged(object sender, EventArgs e)
{
selectedClass = quizSelectPupilCB.SelectedItem.ToString();
}
private void quizStartBTN_Click(object sender, EventArgs e)
{
Quiz2 formQuiz2 = new Quiz2();
formQuiz2.Show();
this.Hide();
}
How do I use this data on another form?
I don't even know where to start. I tried looking up things like "how to access data from one form and put it in another c#" and similar things but everything I found was either not what I was looking for, or worded in a really confusing way.
As a variant you can save selectedItem to String/StringCollection, then to app settings(Settings.Default). And on another form get this value back.

WinForms, sending data from Form to already existing Form

I have main Form with DataGridView. I'm opening new Form, filling text fields, and pressing button to close the window. Once the button is clicked I also want to send data from text fields to DataGridView.
I'm currently experimenting on one textbox.
Form1:
public String SetLastDataGridViewFirstName
{
set { dgvDisplay.Rows[dgvDisplay.RowCount].Cells[0].Value = value; }
}
Form2:
private void btnAdd_Click(object sender, EventArgs e)
{
MainMenu.SetLastDataGridViewFirstName = txbAddFirstName.Text;
this.Close();
}
But I'm unable to access setter. Its obvious that this implementation won't work (just showing example what I'm thinking about), but I'm unable to find working solution. All tutorials show how to send data to new Form, not already existing.

how to access a user define controls iner controls in parent form

I'm new to windows forms programming so my question may sound little strange.
I have created a user define control (countdown timer) now I'm creating n no of it dynamically in a form by Click of a button (Add new timer) its working well and good.
Here is the Creation Code
private void Addnew_Click(object sender, EventArgs e)
{
UserControl1.userControl11 = new UserControl1();
flowLayoutPanel1.Controls.Add(userControl11);
}
My user control has a Reset button that reset all the content inside the user define control.
it is also working, but What I want Allow user to reset all the Created timers using the “Reset All” button on the form.
Okay one way to do this.
Create a List<UserControl1> private member on your form called say _myUserControls
In your Addnew Handler add it to the list.
If you have a remove button, don't forget to remove from _myUserControls as well.
Add a Reset method to your UserControl1, that does what it needs to do.
Then in your Reset all button click handler
foreach(UserControl1 ctrl in _myUserControls)
{
ctrl.Reset();
}
Jobs a good 'un
The answer I referred you to in comments, would be a way of finding all instances of your UserControl1 class, so you wouldn't need an internal list.

How to update ComboBox on form1 after saving the record on another form at runtime?

I have one form, "form1", which contains a template ComboBox and a create template-button. When I click the create template-button, a new form opens for saving a new template record. The record is saved successsfuly on the second form but when it closes, the ComboBox was not updated at runtime with the template name which was saved in database. So how to refresh or reload the ComboBox at runtime?
you can load the ComboBox again on closing event of child form as below
private void LoadChildForm_Click(object sender, EventArgs e)
{
ChildForm form = new ChildForm();
form.FormClosed += new FormClosedEventHandler(ChildFormClosed);
form.Show();
}
void ChildFormClosed(object sender, FormClosedEventArgs e)
{
// Load data and bind to ComboBox
}
You could pass parent form(form 1) to form 2, and make public method in form 1 which update your combo box, and call that method on close form 2.
You should really look into databinding.
Databinding MSDN
This way you can keep a list of templates and let your UI controls be updated through events send by the list itself when things change! This technique is much easier to maintain as your UI will grow and grow!

Open another page (not web) on C#

I'm creating a native C# application and I need to do a simple thing:
Once the user clicks some certain button, another .cs file is opened (with its own design, code and stuff). If it is possible, I would like to know how to close the current form at the same time.
EDIT: what I exactly need:
namespace Mokesciai
{
public partial class Mokesciai : Form
{
public Mokesciai()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//write code here to open another page called "NewPage.cs" with its own subfiles "NewPage.Designer.cs" and
//"NewPage.resx", as shown in the solution explorer
}
}
}
The application is C# Windows application
EDIT2: what I want in the graphical way: http://sdrv.ms/JXKVEL
By clicking "Click me" I want to open the new form
private void button1_Click(object sender, EventArgs e)
{
YourSecondForm objForm=new YourSecondForm();
objForm.Show();
this.Close();
}
Assuming YourSecondForm is the name of your another form which you want to display on the button click event.
That is a form.
You can create a new instance of the form class, then call Show().
As far as i understand from your question, may be you want to do this. You can use Process.Start() to start any other application from your native app.
using System.Diagnostics;
string path=#"path to the app"
Process.Start(path);
OR
Create a new form place a multi line text box, then read the file using StreamReader & fill its result on the text box. For more information on how to use Stream Reader Check out this or this

Categories

Resources