Create one loading form and load different form - c#

I have created one loading form, but I want access the loading form and load the different form. I know there is a way to achieve this, but the way that I think is create another loading form and access that another loading form to load the different form, even though the loading form contents are same.
How can I achieve this?
Here is the code of Loading Form:
public Loading()
{
InitializeComponent();
this.timer1.Interval = SystemManager.RandomNumberGenerator(1000, 2000);
this.timer1.Tick += new EventHandler(this.CheckTimer);
}
private void Loading_Load(object sender, EventArgs e)
{
this.timer1.Start();
}
private void CheckTimer(object sender, EventArgs e)
{
uint timeLeft = 1;
timeLeft--;
if (timeLeft == 0)
{
this.timer1.Stop();
this.Hide();
AgeConfirmation _ageConfirmation = new AgeConfirmation();
_ageConfirmation.ShowDialog();
this.Close();
}
}
Above code is one loading form and load another form by the time is reached 0.
I have tried like this:
public class SystemManager
{
public static void LoadForm(Form _form = null, Form _loadForm = null)
{
_form.Hide();
_loadForm = new Form();
_loadForm.ShowDialog();
_form.Close();
}
}
and access it like this:
SystemManager.LoadForm(this, AgeConfirmation);
But it is throws the following error:
'System.Windows.Forms.AgeConfirmation' is a 'type' but is used like a 'variable'
My Question is: Create only one form (Loading Form), and access that Loading Form and by the time, the time reached 0, it will access different form.
Your answer much appreciated!
Thank you very much!

You should be using _ageConfirmation, which is the form object, not AgeConfirmation which is the form type.
i.e. SystemManager.LoadForm(this, _ageConfirmation);

Solved by myself! I create a getter and setter int value and by using switch case and the code will be like this:
public class UserInformation
{
public static int Value
{
get;
set;
}
}
public partial class Loading : Form
{
public Loading()
{
InitializeComponent();
this.timer1.Interval = SystemManager.RandomNumberGenerator(1000, 2000);
this.timer1.Tick += new EventHandler(this.CheckTimer);
}
private void Loading_Load(object sender, EventArgs e)
{
this.timer1.Start();
}
private void CheckTimer(object sender, EventArgs e)
{
uint timeLeft = 1;
timeLeft--;
if (timeLeft == 0)
{
this.timer1.Stop();
this.Hide();
switch (UserInformation.Value)
{
case 0:
AgeConfirmation _ageConfirmation = new AgeConfirmation();
_ageConfirmation.ShowDialog();
break;
case 1:
MainForm _mainForm = new MainForm();
_mainForm.ShowDialog();
break;
case 2:
Event _event = new Event();
_event.ShowDialog();
break;
}
this.Close();
}
}
}
private void AgeConfirmation_Load(object sender, EventArgs e)
{
UserInformation.Value = 1;
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Loading _loading = new Loading();
_loading.ShowDialog();
this.Close();
}
private void MainForm_Load(object sender, EventArgs e)
{
UserInformation.Value = 2;
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Loading _loading = new Loading();
_loading.ShowDialog();
this.Close();
}
By the time the program runs, UserInformation.Value will be 0
Thank you very much for those who are replying to my question.

Change
_ageConfirmation.ShowDialog();
to
_ageConfirmation.Show();

Related

Object reference is required for the non static field

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();
PongForm.Show();
this.Close();
}
private void ExitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Can someone explain why I'm getting an error? I've had a look online and think it should work. I'm trying to change to a new form on button click.
In this function you should refer form, not PongForm:
private void PlayButton_Click(object sender, EventArgs e)
{
PongForm form = new PongForm();
form.Show();
this.Close();
}
Change "PongForm.Show();" to "form.Show().
To eloborate: you are attempting to call the class, not the instance you created.
to just add to what others have said. you probably don't want multiple of the same forms open. I cant comment or I would have done that instead. hope this solves your problem.
if (Application.OpenForms["PongForm"] != null)
{
Application.OpenForms["PongForm"].WindowState = FormWindowState.Normal;
Application.OpenForms["PongForm"].BringToFront();
}
else
{
PongForm form = new PongForm();
form.Show();
}

Generalizing the form calling in a button event

I usually call this piece of code to show a form whenever a button is clicked.
private frmSelection _frmSelection;`
private void _frmSelection_FormClosing(object sender, FormClosingEventArgs e)
{
_frmSelection = null;
}
private void changeFeedOrderToolStripMenuItem_Click(object sender, EventArgs e)
{
if (_frmSelection == null)
{
_frmSelection = new frmSelection();
_frmSelection.FormClosing += _frmSelection_FormClosing;
_frmSelection.WindowState = FormWindowState.Minimized;
_frmSelection.Show();
_frmSelection.WindowState = FormWindowState.Normal;
}
else
{
_frmSelection.WindowState = FormWindowState.Minimized;
_frmSelection.WindowState = FormWindowState.Normal;
}
}
If the form is already open it will show the already opened instance instead of creating new instance. It is working fine.
But my problem is i need to copy paste and change the form name whenever i am adding a new form.
How it can be generalized and added to
Helper
class?
Something like this:
public sealed class ReusableFormContainer<T> : IDisposable
where T : Form, new()
{
private bool isDisposed;
private void HandleFormClosing(object sender, FormClosingEventArgs e)
{
Form = null;
}
public T Form { get; private set; }
public void Show()
{
if (isDisposed)
{
throw new ObjectDisposedException(null);
}
if (Form == null)
{
Form = new T { WindowState = FormWindowState.Minimized };
Form.FormClosing += HandleFormClosing;
Form.Show();
}
else
{
Form.WindowState = FormWindowState.Minimized;
}
Form.WindowState = FormWindowState.Normal;
}
public void Dispose()
{
// IDisposable.Dispose is implemented to handle cases, when you want to close
// wrapped form using code
if (!isDisposed)
{
Form?.Dispose();
isDisposed = true;
}
}
}
Usage:
// must be initialized somewhere in constructors
private readonly ReusableFormContainer<FormA> container_A;
private readonly ReusableFormContainer<FormB> container_B;
private void button1_Click(object sender, EventArgs e)
{
container_A.Show();
}
private void button2_Click(object sender, EventArgs e)
{
container_B.Show();
}
The following code might do what you want. Just thought about to use the Application functions because those can cover all forms that are on thread.
public partial class Form1 : Form
{
public int i;
private Form1 _frmSelection;
public Form1()
{
InitializeComponent();
i = Application.OpenForms.Count;
}
private void _frmSelection_FormClosing(object sender, FormClosingEventArgs e)
{
_frmSelection = null;
}
private void button1_Click(object sender, EventArgs e)
{
if (_frmSelection == null)
{
_frmSelection = new Form1();
_frmSelection.FormClosing += _frmSelection_FormClosing;
_frmSelection.WindowState = FormWindowState.Minimized;
_frmSelection.WindowState = FormWindowState.Normal;
_frmSelection.Show();
if (Application.OpenForms.Count > 1)
{
_frmSelection.Text = Application.OpenForms[i].Text + " and going";
}
}
else
{
_frmSelection.WindowState = FormWindowState.Minimized;
_frmSelection.WindowState = FormWindowState.Normal;
}
}
}

How can I init items of a tab?

I have a form with 2 tabs on it. I can chose the tab viewed after initialization and I need some initial code every time after the tab2 is initialized:
public partial class SetupComponent : Form
{
public SetupComponent(bool tab2)
{
InitializeComponent();
if (tab2)
{
this.tabControl1.SelectedTab = tabPage2;
}
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
textBox2.SelectionStart = textBox2.Text.Length;
textBox2.Focus();
}
}
if I call this class with tab2=false and then click onto tab2, tabControl1_SelectedIndexChanged is called.
But if I select the tab2=true during SetupComponent, I find no possibility to do that code. All the TabControl1_Events I found are too early and I don`t find a matching TabPage2_Event.
How can I manage it?
I managed this problem using the Paint_Event:
bool activated = false;
private void tabPage2_Paint(object sender, PaintEventArgs e)
{
if (!activated)
{
tabControl1_SelectedIndexChanged(null, null);
activated = true;
}
}
I use the variable because the Paint_Event is called many times.

How to open a form only once?

I have an app that show a form call System Parameters and i want the form to only pop one time so that the user cant open the same window million times. I tried
private void SystemParametersClick(object sender, EventArgs e)
{
Xpan sp = new Xpan();
sp.CurrentItem = this.GetCaller(sender);
if (sp.Visible==false)
{
sp.Show();
}
}
It doesnt work because it is not the same instance. :(
How do i make it only pop once?
Why do you instantiate the form within the method? Simply instantiate it within the parent class and only call the Show() method within the click event.
public class MainForm : Form
{
private Xpan _Xpan;
public MainForm()
{
InitializeComponent();
_Xpan = new Xpan();
}
private void SystemParametersClick(object sender, EventArgs e)
{
_Xpan.Show();
}
}
Maybe this simple approach would suffice?
private bool has_been_shown = false;
private void SystemParametersClick(object sender, EventArgs e)
{
if(!has_been_shown)
{
has_been_shown = true;
Xpan sp = new Xpan();
}
}
First disable closing for Xpan form. You can do it by defining OnFormClosing event handler.
private void Xpan_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Hide();
}
Then define your Xpan form as a class member of the parent form, e.g.:
private readonly Xpan _sp = new Xpan();
And finally defile your Click handler this way:
private void SystemParametersClick(object sender, EventArgs e)
{
if (!_sp.Visible)
{
_sp.Show();
}
else
{
_sp.Activate();
}
}
That's it.

A new form in a main form

in C# forms I need code to add a second form to my existing. this is what I've tried:
First form:
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
frmMain fM = new frmMain();
fM.KeyPress += new KeyPressEventHandler(MMForm);
}
private void MMForm(object sender, KeyPressEventArgs e)
{
Keys KP; KP = (Keys)sender;
if (KP == Keys.Escape) { frm2 fM2 = new frm2(); fM2.Show(); }
}
}
And this is Second form:
public class frm2 : Form
{
public frm2()
{
frm2 fM2 = new frm2();
fM2.Height = 200; fM2.Width = 200;
Controls.AddRange(new System.Windows.Forms.Form[] { fM2 });
}
}
What am I missing?
EDIT: forget all this for a moment. Even if I do it as suggested down there I get an error when I press the key.
An unhandled exception of type 'System.InvalidCastException' occurred in Project 09.exe
Additional information: Specified cast is not valid.
You could do this:
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
this.private void MMForm(object sender, KeyPressEventArgs e)
}
private void MMForm(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToChar(((int)Keys.Escape)))
{
frm2 fM2 = new frm2(); fm2.Height=200; fm2.Width=200; fM2.Show();
}
}
public class frm2 : Form
{
public frm2()
{
InitializeComponent();
}
}
frm2 does not use InitializeComponent() command. so add it to your code.
secondly you try to add frm2 object to itself, so it will not work.
you should use the code belove for your exiting form (and set please its weight from properties, if you will not resize the form.
public class frm2 : Form
{
public frm2()
{
InitializeComponent(); ,
this.Width = 200; this.Height = 200;
}
}
And after a special key if you want to display frm2 :
frm2 secondFrom = new frm2();
frm2.Show(); // frm2.ShowDialog(); works too but they are working differently.
private void frmMain_Load(object sender, EventArgs e)
{
frmMain fM = new frmMain();
fM.KeyPress += new KeyPressEventHandler(MMForm);
}
Replace with this:
private void frmMain_Load(object sender, EventArgs e)
{
this.KeyPress += new KeyPressEventHandler(MMForm);
}
Or you can just register to your own KeyPress via designer, directly to MMForm...
And also, it is unclear what you are trying to do here:
public frm2()
{
frm2 fM2 = new frm2();
fM2.Height = 200; fM2.Width = 200;
Controls.AddRange(new System.Windows.Forms.Form[] { fM2 });
}
It should probably look more like this:
public frm2()
{
InitializeComponents();
this.Height = 200;
this.Width = 200;
}
Even if you don't want to InitializeComponents, you should edit your own (this) properties, not a new frm2 properties.
You've had the same problem in frmMain_Load, when you created a new frmMain, and subscribed to it's KeyPress, when really you should've subscribed to your own KeyPress.
Also, you can shorten your MMForm just to beautify, like so:
private void MMForm(object sender, KeyPressEventArgs e)
{
if ((Keys)sender == Keys.Escape)
{
new frm2().Show();
}
}
If you are trying to open frm2 when escape key is pressed on the main form do the following:
public frmMain()
{
InitializeComponent();
this.KeyPress += new KeyPressEventHandler(MMForm);
}
//You don't need to put anything in form load
private void frmMain_Load(object sender, EventArgs e)
{
}
//This is fine
private void MMForm(object sender, KeyPressEventArgs e)
{
Keys KP; KP = (Keys)sender;
if (KP == Keys.Escape) { frm2 fM2 = new frm2(); fM2.Show(); }
}
In frm2 do:
public class frm2 : Form
{
public frm2()
{
InitializeComponent();
this.Height = 200; this.Width = 200;
Controls.AddRange(new System.Windows.Forms.Form[] { fM2 });
}
}

Categories

Resources