C# - How to close created form window from other form - c#

I'm making a function in Form1 that make a new form.
private void makeForm(String option) {
Form formWin = new Form();
formWin.TopMost = true;
formWin.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
formWin.Size = new Size(500,600);
formWin.StartPosition = FormStartPosition.Manual;
formWin.Location = new Point(1366 - formWin.Size.Width , 768 - formWin.Size.Width);
formWin.BackColor = Color.White;
formWin.TransparencyKey = Color.White;
var formWinModel = new PictureBox
{
Name = "formWin",
Size = new Size(formWin.Size.Width, formWin.Size.Height),
SizeMode = PictureBoxSizeMode.Zoom,
Image = Trans.Properties.Resources.Form_Special,
Location = new Point(0, 0),
};
formWin.Controls.Add(formWinModel);
if (option == "show")
{
formWin.Show();
}
if (option == "exit") {
formWin.Visible = false;
formWin.Close();
formWin.Dispose();
}
}
When I used makeForm("show") it will create formWin form. But how to close formWin form. I used makeForm("exit") but it doesn't work.
Thanks

You have created a Modal window, which means the previous form, (the one that you opened formWin from) wont be accessible while formWin is active. If you want both forms to be accessible at the same time, you should use formWin.Show() instead.
Your method wont work anyways because you're looking for a local variable (form) to close which is not possible, you should declare Form formWin outside of your method and save the value to this variable. When you later want to close it. But that's gonna cause some other problems such as what if you wanted to create multiple forms at the same time? Which questions your whole logic, why even close the Form from your previous Form?

private Form formWin = null;
private void makeForm(String option)
{
if(formWin == null)
{
formWin = new Form();
formWin.TopMost = true;
formWin.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
formWin.Size = new Size(500, 600);
formWin.StartPosition = FormStartPosition.Manual;
formWin.Location = new Point(1366 - formWin.Size.Width, 768 - formWin.Size.Width);
formWin.BackColor = Color.White;
formWin.TransparencyKey = Color.White;
var formWinModel = new PictureBox
{
Name = "formWin",
Size = new Size(formWin.Size.Width, formWin.Size.Height),
SizeMode = PictureBoxSizeMode.Zoom,
Image = Trans.Properties.Resources.Form_Special,
Location = new Point(0, 0),
};
formWin.Controls.Add(formWinModel);
}
if (option == "show")
{
formWin.Show();
}
if (option == "exit")
{
formWin.Visible = false;
formWin.Close();
formWin.Dispose();
formWin = null;
}
}
You should create formWin varibale in out makeForm function. I hope it help you.

Related

Newly added controls won't paint to Form

So I have a Form that is a scale A4 page that allows users to drag and drop controls on the form for printing.
IE where ever the control is on the form its location is used to print the controls data, eg: File name or image, to that point of an A4 page.
However I have created a number of templates for the form that sets the controls in certain locations and adds in any missing controls. When the templates are selected any extra controls don't show on the form even though I call the Invalidate() method.
Here is my code for the method that adds the controls to the Form:
private void standardIDToolStripMenuItem_Click(object sender, EventArgs e)
{
selectedID = true;
selectedInvoice = false;
selectedLetter = false;
lblName.Visible = true;
lblDOB.Visible = true;
lblUID.Visible = true;
lblName.Location = new Point(200, 100);
lblDOB.Location = new Point(200, 125);
lblUID.Location = new Point(200, 150);
lblName2.Text = lblName.Text;
lblName2.Location = new Point(60, 750);
lblName2.Enabled = true;
lblName2.Visible = true;
lblDOB2.Text = lblDOB.Text;
lblDOB2.Location = new Point(60, 775);
lblDOB2.Enabled = true;
lblDOB2.Visible = true;
lblUID2.Text = lblUID.Text;
lblUID2.Location = new Point(60,800);
lblUID2.Enabled = true;
lblUID2.Visible = true;
hidden1.Location = new Point(300, 100);
DOBHidden.Location = new Point(300, 125);
UIDHidden.Location = new Point(300, 150);
#region ID Background placeholder
PictureBox backPic = new PictureBox();
backPic.Location = new Point(24, 48);
backPic.ForeColor = System.Drawing.Color.PaleGreen;
backPic.Size = new Size(504, 176);
backPic.Visible = true;
backPic.Show();
backPic.SendToBack();
this.Invalidate();
#endregion
}
Why will the new controls not appear on the form when I have called the Invalidate() method to force it to repaint?
It seems that you don't add them to Controls:
please try this on every control after you have specified location and the rest of the control initialization:
this.Controls.Add(lblName)
Mong Zhu seems to be right and I also suggest you to catch a glimpse of some kind of report designer if you can use some third-part libaries ( I'm not sure if winforms privides something like DevExpress reports for example)
I guess that it will be helpful with stuff you are doing in your project.

How to add button to a previous, manual-created form?

What I'm trying to do is: create a new form with a click (DONE and working, see code below), and then add some buttons to that new form. In this case, it's just one button because I need to make this work before adding more buttons. Should be pretty simple, but after following some Stackoverflow answers/YouTube tutorials/Internet tutorials I'm still not able to do it.
Actually, [the application] It's meant to be like a personal schedule, where I could track every activity or work to do, dispersed over several days (from monday to friday) and in each day you should find the different times of the day (morning/midday/evening).
My code is shown below (this code belongs to the button1_Click method of the first form, as you may notice).
private void button1_Click(object sender, EventArgs e)
{
// día lunes
Form SubLunes = new Form(); // new form
SubLunes.Text = "Día lunes";
SubLunes.Size = new Size(800, 400);
SubLunes.StartPosition = FormStartPosition.CenterScreen;
SubLunes.FormBorderStyle = FormBorderStyle.FixedSingle;
SubLunes.ShowIcon = false;
SubLunes.CreateControl();
SubLunes.ShowDialog();
// botones
Button Mañana = new Button(); // new button
Mañana.Location = new System.Drawing.Point(100, 150);
Mañana.Size = new Size(100, 100);
Mañana.Text = "Mañana";
Mañana.Click += new EventHandler(Mañana_Click);
SubLunes.Controls.Add(Mañana); // should add button to SubLunes
}
private void Mañana_Click(object sender, EventArgs e)
{
MessageBox.Show("hello, i'm new button"); // displayed when clicking new button
}
This is how currently looks:
Main form ///////
After clicking Lunes button (a new Button called 'Mañana' should be in there)
THANKS YOU in advance. See you later.
You're showing the form before creating the button :
Form SubLunes = new Form();
SubLunes.Text = "Día lunes";
SubLunes.Size = new Size(800, 400);
SubLunes.StartPosition = FormStartPosition.CenterScreen;
SubLunes.FormBorderStyle = FormBorderStyle.FixedSingle;
SubLunes.ShowIcon = false;
SubLunes.CreateControl();
SubLunes.ShowDialog();
Button Mañana = new Button();
Mañana.Location = new System.Drawing.Point(100, 150);
Mañana.Size = new Size(100, 100);
Mañana.Text = "Mañana";
Mañana.Click += new EventHandler(Mañana_Click);
SubLunes.Controls.Add(Mañana);
You should create the button before showing the form, like this :
Form SubLunes = new Form();
SubLunes.Text = "Día lunes";
SubLunes.Size = new Size(800, 400);
SubLunes.StartPosition = FormStartPosition.CenterScreen;
SubLunes.FormBorderStyle = FormBorderStyle.FixedSingle;
SubLunes.ShowIcon = false;
SubLunes.CreateControl();
Button Mañana = new Button(); // new button
Mañana.Location = new System.Drawing.Point(100, 150);
Mañana.Size = new Size(100, 100);
Mañana.Text = "Mañana";
Mañana.Click += new EventHandler(Mañana_Click);
SubLunes.Controls.Add(Mañana);
SubLunes.ShowDialog();

How to control form load from another form

I want to control my form load event from another form.
my problem I create some winform control in form1 runtime, but the creation will controlled by form2.
I will read some data from user in form2 and when user enter specific text I will create winform control in form1.
I make some code to do that using from1 to create winform control in runtime.
private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ListBox lstBox = new ListBox();
private CheckBox chkBox = new CheckBox();
private Label lblCount = new Label();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.MaximizeBox = false;
this.MinimizeBox = false;
this.BackColor = Color.White;
this.ForeColor = Color.Black;
this.Size = new System.Drawing.Size(550, 550);
this.Text = "Test Create form in runtime ";
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.StartPosition = FormStartPosition.CenterScreen;
this.btnAdd.BackColor = Color.Gray;
this.btnAdd.Text = "Add";
this.btnAdd.Location = new System.Drawing.Point(90, 25);
this.btnAdd.Size = new System.Drawing.Size(50, 25);
this.txtBox.Text = "Text";
this.txtBox.Location = new System.Drawing.Point(10, 25);
this.txtBox.Size = new System.Drawing.Size(70, 20);
this.lstBox.Items.Add("One");
this.lstBox.Sorted = true;
this.lstBox.Location = new System.Drawing.Point(10, 55);
this.lstBox.Size = new System.Drawing.Size(130, 95);
this.chkBox.Text = "Disable";
this.chkBox.Location = new System.Drawing.Point(15, 190);
this.chkBox.Size = new System.Drawing.Size(110, 30);
this.lblCount.Text = lstBox.Items.Count.ToString() + " items";
this.lblCount.Location = new System.Drawing.Point(55, 160);
this.lblCount.Size = new System.Drawing.Size(65, 15);
this.Controls.Add(btnAdd);
this.Controls.Add(txtBox);
this.Controls.Add(lstBox);
this.Controls.Add(chkBox);
this.Controls.Add(lblCount);
}
How make the same thing from form2 ?
I don't know which kind of 'Control' you need. However in multiple forms environment, communication between forms is trivial. There are many ways to do communicate, like one can be as
Create public properties of type Form in your Parent form,
public Form propForm1 {get;set;}
When on menu item click you open form1, store it's object to that public property.
var form1 = New yourchildformname();
form1.MdiParent = this;
propForm1 = form1; // Add this line.
form1.Show();
Now every time when you will click an other button to open the form2, you will have propForm1 object, which you can use to set some data on that form.
EDIT:
On form2, you can access controls of form1 as
private void button1_Click(object sender, EventArgs e)
{
this.parent.propForm1.txtUserName = "Yokohama";
}
Remember the above code is on form2. Also set 'access modifier' property of txtUserName from private to public.

form.Show in function not working when called

public static object loadForm(Form formToLoad, TabControl homeTabControl)
{
//Check if formToLoad parameter is NULL
if (formToLoad == null) throw new ArgumentNullException("formToLoad");
//get the parent/ownining form
Form form1 = new Form1();
//set formToLoad properties
formToLoad = new Form
{
Owner = form1,
FormBorderStyle = FormBorderStyle.None,
TopLevel = false,
Dock = DockStyle.Fill
};
//add formToLoad to tabControl tabPage
homeTabControl.TabPages["tabPageHome"].Controls.Add(formToLoad);
formToLoad.Show();
return formToLoad;
}
How come formToLoad does not show in the tabControl Page when i call my code from a button click?
private void button3_Click(object sender, EventArgs e)
{
LeaveMainForm lM = new LeaveMainForm();
AppCode.FormLoader.loadForm(lM, homeTabControl);
}
You are over-writing the actual form you are trying to load with a new Form instance, in this line :
formToLoad = new Form
Try this :
//set formToLoad properties
formToLoad.Owner = form1;
formToLoad.FormBorderStyle = FormBorderStyle.None;
formToLoad.TopLevel = false;
formToLoad.Dock = DockStyle.Fill;

pop up user control not rendering correctly when created

I have a class which handles a sort of user control popup, which works by inheriting from System.Windows.Forms.ToolStripDropDown. This has worked for the popup type i had currently, of which i shall detail below;
First, is the class which i use to hold the user control in a popup type style;
public class PopupWindow : System.Windows.Forms.ToolStripDropDown
{
private System.Windows.Forms.Control _content;
private System.Windows.Forms.ToolStripControlHost _host;
public PopupWindow(System.Windows.Forms.Control content)
{
//Basic setup...
this.AutoSize = false;
this.DoubleBuffered = true;
this.ResizeRedraw = true;
this.BackColor = content.BackColor;
this._content = content;
this._host = new System.Windows.Forms.ToolStripControlHost(content);
//Positioning and Sizing
this.MinimumSize = content.MinimumSize;
this.MaximumSize = content.Size;
this.Size = content.Size;
content.Location = Point.Empty;
//Add the host to the list
this.Items.Add(this._host);
}
}
as we can see here, i'm simply passing a control to it, and letting it do the work. When using this on an "onclick" popup, like so, it works fine;
public void Popup(object sender, MouseEventArgs e, other params)
{
DevicePopup popupDevice = new DevicePopup();
//do stuff to the control here before displaying
PopupWindow popup = new PopupWindow(popupDevice);
popup.Show(Cursor.Position);
}
and calling it like so;
this.Controls[btnAdd.Name].MouseClick += (sender, e) =>
{
int index = temp;
generatePopup.Popup(sender, e, mDevices[index], this);
};
doing this successfully creates the popup user control at my mouse click, as intended.
However, i'm now trying to use a second type of popup which spawns when something happens. Below is my new popup class, and calling it;
public void AlarmNotificationPopup(IDeviceInterface device)
{
try
{
AlarmNotification ANotification = new AlarmNotification();
//do stuff to the control again before displaying
PopupWindow popup = new PopupWindow(ANotification);
popup.Show(100, 100);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
AlarmNotificationPopup(device);
However, this popup doesn't render / create properly, and looks like so;
I'm not entirely sure how to fix this. anyone have any ideas?
A couple of things to try:
I don't think you need your "Basic Setup", so comment that out. Also, try setting your margins and paddings:
public PopupWindow(Control content) {
// Basic setup...
// this.AutoSize = false;
// this.DoubleBuffered = true;
// this.ResizeRedraw = true;
// this.BackColor = content.BackColor;
this._content = content;
this._host = new ToolStripControlHost(content);
this._host.Margin = new Padding(0);
this._host.Padding = new Padding(0);
this.Padding = new Padding(0);
this.Margin = new Padding(0);
//Positioning and Sizing
this.MinimumSize = content.MinimumSize;
this.MaximumSize = content.Size;
this.Size = content.Size;
content.Location = Point.Empty;
//Add the host to the list
this.Items.Add(this._host);
}
Make sure your AlarmNotification has its MinimumSize property set.
If that doesn't help solve the problem, then you probably need to document what the AlarmNotification class is doing.

Categories

Resources