WinForms, sending data from Form to already existing Form - c#

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.

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.

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.

Can I have different form_loads inside a form that can be assigned to a specific event/action? C#

I'm making a windows form app and I have one form that represents a base form and it gets data from a different class (that's almost like a database):
private void base_form_Load(object sender, EventArgs e)
{
database_class dc = new database_class();
button1.Text =dc.Name;
}
NOTE: The reason why this code is in the form_Load is because it doesn't show up, unless I put it there, which I find strange, but it might not be?
I have a main form, that acts like a menu - it has four buttons on it and all the buttons lead to the base form. The database class is actually supposed to change the names of the controls in the base form based on the what button is chosen in the main form(menu). The base form has a lot more buttons than the main form.
Since this is confusing, here's an example of what I want to do: If the menu had options (buttons) Mozart, Beethoven, Liszt, Chopin - when people click on Mozart they're supposed to get buttons with the names of his compositions, if they click Beethoven then they get his compositions and so on.(These buttons in the base form do lead to something else, if that's important/helpful). The reason I'm not making separate forms for these menu buttons, is because I have a lot of buttons and I don't think making plenty of forms is ideal (it's a simple app, I don't want to slow it down with a lot of forms).
My question is what is the best way to do this? Do I have to somehow assign the data I want to the button (mouse) click events in the menu (main form)? Is there a possibility of having different form loads in the base form, that can be assigned to the mouse clicks in the menu?
Thank you for your time.
Example constructor as requested - form has a richTextBox that gets populated by the constructor and also a string value that's used later:
public partial class Form2 : Form
{
public string _filename;
public Form2(string text, string filename)
{
InitializeComponent();
richTextBox1.Text = text;
_filename = filename;
}
}
Create an instance of this form from the main form:
private void button1_Click(object sender, EventArgs e)
{
string textForRTB = "Some text";
string valueForFilename = "\some\file\name";
Form2 frm2 = new Form2(textForRTB, valueForFilename);
frm2.Show();
}

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