Generalizing the form calling in a button event - c#

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

Related

C#-Winforms-How to use instance objects in different subforms?

I have a "MainForm" and a "GraphicsForm". Clicking "New" on the main form, a "GraphicsForm" will be created.
The problem is that when I create multiple "GraphicsForm", and when I want to save the content of one of the "GraphicsForm", I need to clicking "Save" on the "MainForm" and the program will save the content of the active "GraphicsForm" to a file, I don't know how to pass the content of this "GraphicsForm" to "MainForm" for storage.
MainForm.cs
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private ToolStripMenuItem _winMenuItem = new ToolStripMenuItem();
private GraphicsForm _graphicsForm;
private int _counter = 1;
private void New_Click(objec sender, EventArgs e)
{
_winMenuItem.Name = "Win";
_winMenuItem.Text = "Windows";
int item = MainMenuStrip.Items.IndexOf(_winMenuItem);
if (item == -1)
{
MainMenuStrip.Items.Add(_winMenuItem);
MainMenuStrip.MdiWindowListItem = _winMenuItem;
}
_graphicsForm = new GraphicsForm();
_graphicsForm.Name = string.Concat("Win_", _counter.ToString());
_graphicsForm.Text = _graphicsForm.Name;
_graphicsForm.MdiParent = this;
_graphicsForm.Show();
_graphicsForm.WindowState = FormWindowState.Maximized;
_counter++;
}
private void Save_Click(object sender, EventArgs e)
{
... // Problem here
}
private void Open_Click(object sender, EventArgs e)
{
... // Problem here
}
}
GraphicsForm.cs
public partial class GraphicsForm : Form
{
//StorageDoc is a class to manage all the graphics drawn by the user in the form.
private StorageDoc _storageDoc = new StotageDoc();
public GraphicsForm()
{
InitializeComponent();
}
private Canvas_MouseDown()
{
}
private Canvas_Paint()
{
}
...
Because MainForm is a MDI form, it is easy to use ActiveMdiChild to get the active child form.
class MainForm : Form
{
public void OnSaveButtonClick(object sender, EventArgs e)
{
if(ActiveMdiChild is GraphicsForm g)
Save(g);
}
}
I'm sure this has been answered before but basically, you pass in an instance of the 'data storage' to the new form.
interface ISaveForm
{
void Save();
}
class MainForm
{
private DataStorage _dataStorage;
private ICollection<ISaveForm> _forms = new List<ISaveForm>();
public void OnNew()
{
var subForm = new GraphicsForm(_dataStorage);
subForm.Show();
_forms.Add(subForm);
}
public void OnSave()
{
foreach(var form in _forms)
{
form.Save();
}
}
}
class GraphicsForm : Form,ISaveForm
{
private DataStorage _dataStorage;
public GraphicsForm(DataStorage dataStorage)
{
_dataStorage = dataStorage;
}
public void Save()
{
}
}

Calling Another user control from one user control

I have two user controls usercontrol_1 and usercontrol_2, using a click event i am bring in usercontrol_1 to a panel called panel_screen on my main form.
private void btn_Click(object sender, EventArgs e)
{
if (!panel_screen.Controls.Contains(usercontrol_1.Instance))
{
panel_screen.Controls.Add(usercontrol_1.Instance);
usercontrol_1.Instance.Dock = DockStyle.Fill;
usercontrol_1.Instance.BringToFront();
}
else
usercontrol_1.Instance.BringToFront();
}
Similarly i wanna bring usercontrol_2 to the same panel on the main form using a button (click event) on usercontrol_1.
How do i do this? any help would be appreciated.
I'm not sure where you are having problem, I think the way you are using usercontrol_1.Instance might cause the issue.
I have working example here, you can try it.
private void btn_Click(object sender, EventArgs e)
{
bool userControlIsAlreadyInPanel = false;
//assuming you are cheking if usercontrol_1 is already there on panel
// you don't want to create new usercontrol, but just bring existing control to the front
foreach(UserControl control in panel_screen.Controls)
{
if (control.GetType() == typeof(usercontrol_1))
{
userControlIsAlreadyInPanel = true;
control.BringToFront();
}
}
if(!userControlIsAlreadyInPanel)
{
usercontrol_1 instane = new usercontrol_1();
panel_screen.Controls.Add(instane);
instane.Dock = DockStyle.Fill;
instane.BringToFront();
}
}
output
namespace WindowsFormsApp1
{
public delegate void MyEventDelegate(object sender, string name);
public partial class Form1 : Form
{
usercontrol_1 _ctrl1 = null;
usercontrol_2 _ctrl2 = null;
public Form1()
{
InitializeComponent();
_ctrl1 = new usercontrol_1();
_ctrl1.Dock = DockStyle.Fill;
_ctrl1.userControlButtonClicked += userControlButtonClicked;
_ctrl2 = new usercontrol_2();
_ctrl2.Dock = DockStyle.Fill;
_ctrl2.userControlButtonClicked += userControlButtonClicked;
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
userControlButtonClicked(_ctrl1, "1");
}
private void userControlButtonClicked(object sender, string name)
{
panel1.Controls.Clear();
if (sender.Equals(_ctrl1))
{
panel1.Controls.Add(_ctrl2);
}
else if (sender.Equals(_ctrl2))
{
panel1.Controls.Add(_ctrl1);
}
}
}
}
namespace WindowsFormsApp1
{
public partial class usercontrol_1 : UserControl
{
public event MyEventDelegate userControlButtonClicked;
public usercontrol_1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyEventDelegate med = userControlButtonClicked;
if (med != null)
{
med(this, "1");
}
}
}
}
namespace WindowsFormsApp1
{
public partial class usercontrol_2 : UserControl
{
public event MyEventDelegate userControlButtonClicked;
public usercontrol_2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyEventDelegate med = userControlButtonClicked;
if (med != null)
{
med(this, "2");
}
}
}
}

c# change panel color from another form

I'm stuck with the following thing.
I want to change the panel color from another Form(ColorForm).
Is it possible?
Code From the MainForm:
public void upperpanel_Paint(object sender, PaintEventArgs e)
{
}
I don't know how to access that upperpanel_Paint in my ColorForm.
I'm opening ColorForm From SettingsForm
Mainform > SettingsForm > ColorForm
public partial class SettingsForm : Form
{
public static event ColourSettingChangedDelegate ColourSettingsChangedEvent;
public delegate void ColourSettingChangedDelegate(Color color);
List<string> adreses;
List<string> bookmarki;
void SelectColour()
{
using (ColorForm colourForm = new ColorForm())
{
if (colourForm.ShowDialog() == DialogResult.OK)
{
//Update colour setting and fire event
OnColourSettingsChanged(colourForm.SelectedColor);
}
}
}
public SettingsForm(List<string> adr, List<string> s)
{
InitializeComponent();
adreses = adr;
bookmarki = s;
}
private void Historyb_Click(object sender, EventArgs e)
{
foreach (Form form in Application.OpenForms)
{
if (form.GetType() == typeof(HistoryForm))
{
form.Activate();
return;
}
}
HistoryForm hf1 = new HistoryForm(adreses);
hf1.Show();
}
private void Bookmarksb_Click(object sender, EventArgs e)
{
BookmarksForm booklist = new BookmarksForm();
booklist.SetAllBookmarks(bookmarki);
booklist.ShowDialog();
}
private void Colorb_Click(object sender, EventArgs e)
{
SelectColour();
}
private void OnColourSettingsChanged(Color color)
{
if (ColourSettingsChangedEvent != null)
ColourSettingsChangedEvent(color);
}
}
Code from ColorForm:
public partial class ColorForm : Form
{
public ColorForm()
{
InitializeComponent();
}
private void Panelcolor_Click(object sender, EventArgs e)
{
ColorDialog colorDlg = new ColorDialog();
colorDlg.AllowFullOpen = true;
colorDlg.AnyColor = true;
if (colorDlg.ShowDialog() == DialogResult.OK)
{
upperpanel.BackColor = colorDlg.Color;
}
}
}
Thank you!
Perhaps it would be better to fire a global event when the settings change, or the particular form colour setting changes, and listen for that event on the form where you need to take action.
for example see this pseudo code:
ColourForm:
Form used just to pick a colour, and store the result in a property.
class ColourForm
{
public Color SelectedColor {get;set;}
private void Panelcolor_Click(object sender, EventArgs e)
{
ColorDialog colorDlg = new ColorDialog();
colorDlg.AllowFullOpen = true;
colorDlg.AnyColor = true;
if (colorDlg.ShowDialog() == DialogResult.OK)
{
this.SelectedColor = colorDlg.Color;
}
}
void Cancel()
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
void Save()
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
SettingsForm:
The main form for updating settings, this creates a colour picker form and saved the settings and fires an event if the dialog result of the colour form is an 'ok' result.
class SettingsForm
{
public static event ColourSettingChangedDelegate ColourSettingsChangedEvent;
public delegate void ColourSettingChangedDelegate(Color color);
void SelectColour()
{
using (ColourForm colourForm = new ColourForm())
{
if (colourForm.ShowDialog() == DialogResult.OK)
{
//Update colour setting and fire event
OnColourSettingsChanged(colourForm.SelectedColour);
}
}
}
private void OnColourSettingsChanged(Color color)
{
if (ColourSettingsChangedEvent!=null)
ColourSettingsChangedEvent(color);
}
}
On you Main Form:
The Main form listens for a settings / colour changed event and changes the panel colour to the colour specified in the event when it fires.
class MainForm()
{
//Constructor
MainForm()
{
SettingsForm.ColourSettingsChangedEvent += ColourSettingsChanged;
}
void ColourSettingsChanged(Color color)
{
upperpanel.BackColor = color;
}
}
it would be better to have some kind of settings manager class than have the event on the settings form itself but you should get the idea

Create one loading form and load different form

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();

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