How to persist data on win form when changing forms - c#

I am working on a multi form project in C# and I am having issues with the forms data persisting when changing between tabs. Each tab will open the respective form as shown below.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
loadForm(new FormHome());
}
#region FORM CALLER
public void loadForm(object Form)
{
if (this.gunaPanelHome.Controls.Count > 0)
this.gunaPanelHome.Controls.RemoveAt(0);
Form fetched = Form as Form;
fetched.TopLevel = false;
fetched.BringToFront();
fetched.Dock = DockStyle.Fill;
this.gunaPanelHome.Controls.Add(fetched);
this.gunaPanelHome.Tag = fetched;
fetched.Show();
}
private void gunaHomeButton_Click(object sender, EventArgs e) => loadForm(new FormHome());
private void gunaDelayButton_Click(object sender, EventArgs e) => loadForm(new FormDelay());
private void gunaAssemButton_Click(object sender, EventArgs e) => loadForm(new FormAssembly());
private void gunaBuildButton_Click(object sender, EventArgs e) => loadForm(new FormBuilder());
private void gunaSettingsButton_Click(object sender, EventArgs e) => loadForm(new FormSettings());
#endregion
}
I have data in other forms that I want to keep throughout the tab changes, and was wondering if there was an easier way besides building a constructor that set data through each action change. Overall, I would like to just .hide() the current form in the window.

Related

add pagebar in my form for to change page

I made a simple GUI, in which I would like to add an option where I can switch pages, it is the same page when I open it, I would like a bar where I click and switch pages
namespace POS
{
public partial class Form1 : Form
{
private object gunaLabel_date;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Guna.UI.Lib.ScrollBar.PanelScrollHelper flowphelper ;
gunaLabel_date = DateTime.Now.ToLongDateString();
}
private void gunaVScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
}
private void userControl17_Load(object sender, EventArgs e)
{
}
}
}

I want to load my main code every 5 seconds?

I have a windows form app that display restaurant orders. I want to load the code every 5 seconds to check if there is a new order to display.
I have a timer created in the form designer:
public void timer1_Tick(object sender, EventArgs e)
{
}
public void DisplayRestaurantOrder()
{
//Display restaurant order here
}
private void Form1_Load(object sender, EventArgs e)
{
DisplayRestaurantOrder();
timer1.Interval = 5000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (true)//check new order
{
DisplayRestaurantOrder();
}
}
1) Set the timer's Interval property to 5000 (milliseconds)
2) Create a method which loads the data e.g.
private void LoadOrders()
{
// ... do stuff here
}
3) In the timer's Tick event handler make a call to the load method, in this case LoadOrders:
public void timer1_Tick(object sender, EventArgs e)
{
LoadOrders();
}
4) In the Form.Load event do timer1.Start();, and maybe also a initial call to the load method, to make a Form.Load event handler just double click the form:
private void Form1_Load(object sender, EventArgs e)
{
//LoadOrders(); //this is the initial load call.
//timer1.Start();
}
as a result you should have something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadOrders();
timer1.Start();
}
public void timer1_Tick(object sender, EventArgs e)
{
LoadOrders();
}
private void LoadOrders()
{
// ... do stuff here
}
}
UPDATE (sins the OP wants to load what is in the constructor):
If what is needed to be loaded, is in the Form1 constructor then just move everything from in there to a new method and make a call to that method in both the timer1_Tick handler and in the constructor itself, e.g.:
public Form1()
{
//InitializeComponent();
Load();
}
//should be kept as to start the timer.
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
public void timer1_Tick(object sender, EventArgs e)
{
Load();
}
private void Load()
{
//InitializeComponent(); //this shouldn't be called more than once as it can create duplicate objects, i.e. buttons, menu strips, etc.
// ... do other stuff here
}

New form fails to appear

Can someone explain why my new form doesn't appear? The old form closes but it isn't replaced by my new one:
namespace Pong
{
public partial class Menu : Form
{
public Menu()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void PlayButton_Click(object sender, EventArgs e)
{
PongForm form = new PongForm();
form.Show();
this.Close();
}
private void ExitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Is "Menu" the startup form for your application? If yes, then when it closes the entire application closes.
One solution would be to hide the current form, display "form" with ShowDialog(), then call Close():
private void PlayButton_Click(object sender, EventArgs e)
{
this.Visible = false; // hide the current form
Application.DoEvents();
PongForm form = new PongForm();
form.ShowDialog(); // code stops here until PongForm is dismissed
this.Close(); // now close the form (and presumable the whole app)
}

How to edit the contents of a list box, from a text box in another form

I am creating a library application with a listbox containing a list of books on the main form. I have created an edit form for the books. I want to be able to change the contents of the selected item in the listbox by changing the text in the textboxes of the edit form. Any suggestions how I could do this?
Main form:
private void lstBooks_SelectedIndexChanged(object sender, EventArgs e)
{
string currentBook = lstBooks.SelectedItem.ToString();
}
private void btnEdit_Click(object sender, EventArgs e)
{
lstBooks_SelectedIndexChanged(null, null);
frmEditBook tempEditBook = new frmEditBook(lstBooks);
tempEditBook.Show();
frmkeepBookstore.Hide();
}
Edit form:
private void frmEditBook_Load(object sender, EventArgs e)
{
txtName.Text = listBoxBooks.SelectedItem.ToString();
}
private void btnSave_Click(object sender, EventArgs e)
{
listBoxBooks.Items.Add(txtName.Text.Replace);
frmBookstore.frmkeepBookstore.Show();
this.Close();
}
In your edit form, have a public property for your text
public string NewText
{
get
{
return txtName.Text;
}
}
And then when changing the item in the listbox, simply use the following procedure.
private void btnEdit_Click(object sender, EventArgs e)
{
//Your code from above
lstBooks_SelectedIndexChanged(null, null);
frmEditBook tempEditBook = new frmEditBook(lstBooks);
tempEditBook.Show();
frmkeepBookstore.Hide();
//My line
lstBooks.Items[lstBooks.SelectedIndex] = tempEditBook.NewText;
}
Well, it could be very simple if you call the new form with ShowDialog() instead of Show(). In your frmEditBook you should define the property
public string Txt {get; set;}
and set it at btnSave_Click
private void btnSave_Click(object sender, EventArgs e)
{
listBoxBooks.Items.Add(txtName.Text);
Txt = txtName.Text;
frmBookstore.frmkeepBookstore.Show();
this.Close();
}
and use the variable after the edit form is closed:
private void btnEdit_Click(object sender, EventArgs e)
{
lstBooks_SelectedIndexChanged(null, null);
frmEditBook tempEditBook = new frmEditBook(lstBooks);
tempEditBook.ShowDialog();
//tempEditBook.Txt here is your text
lstBooks.SelectedItem = tempEditBook.Txt;
frmkeepBookstore.Hide();
}

New coder, trying to make Label invisible on program start

First, pardon my new-ness, I just started coding class recently. Now, upon startup, I want parts of my form (c#) to not be shown, however when I put
NameDisplay.Visible = false;
(NameDisplay being the label I wish to hide) into my Form1.cs it gives me the error of that it is a 'field' being used as a 'type'. How do I correct this, and apply to other object types (buttons, textboxes, etc?)
EDIT 1-
Code- as it stands
namespace ATM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Label NameDisplay;
NameDisplay.Visible = false;
private void Form1_Load(object sender, EventArgs e)
{
}
private void StartButton_Click(object sender, EventArgs e)
{
}
private void NameDisplay_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Remove Label NameDisplay;, and place NameDisplay.Visible = false; into your FormLoad event.
The loading of a form is an event just like clicking a button, and will execute the code like so.
Also, when I hide labels, I use .Hide(), but I believe that only works on WinForms.
Hope this helps!
You need to drag and drop the Label on the form and object will be created and initialized automatically in InitializeComponent.
In the form constructor (after InitializeComponent function) or Form_Load event, you may set the visibility to false
For example:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
NameDisplay.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void StartButton_Click(object sender, EventArgs e)
{
}
private void NameDisplay_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}

Categories