我的應用程式需要隱藏初始表單并打開四個新表單中的任何一個。我可以隱藏首字母并打開所選表單:
private void btn_Option1_Click(object sender, EventArgs e){
Visible = false;
Application x = new Application();
x.show();
}
我的問題是如何關閉第二個表單并重新打開原始表單?或者我認為在每個表單打開時關閉每個表單是合理的,但這似乎很浪費。
uj5u.com熱心網友回復:
聽起來你需要這個FormClosed事件。
當您Form從首字母創建 new 的實體時,Form您可以訂閱 newForm的FormClosed事件并Form從處理程式顯示您的首字母。
這樣,每當您的 new 中Form的一個關閉時,事件處理程式將觸發并且您的初始值Form將再次可見。
// This is a method that you would add to your initial Form.
private void SubForm_Closed(object sender, FormClosedEventArgs e)
{
Visible = true;
}
private void btn_Option1_Click(object sender, EventArgs e)
{
Visible = false;
Application x = new Application();
x.FormClosed = SubForm_Closed;
x.show();
}
uj5u.com熱心網友回復:
如果您使用 Hide(),表單“基本上”會消失,您甚至不會在任務欄中看到它。然后,您可以通過從后續表單之一中顯示它來打開。我把下面的東西放在一起,只是快速測驗了它,所以 YMMV,你需要清理它并讓它為你作業,但它應該說明這一點。創建 Windows 表單應用程式。將其稱為“WindowsFormsApp2”,因為這是我使用的。粘貼以下代碼:
using System;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 form2;
Form3 form3;
Form4 form4;
Form5 form5;
private void button1_Click_1(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
form2 = new Form2(this);
form2.Show();
}
if (checkBox2.Checked)
{
form3 = new Form3(this);
form3.Show();
}
if (checkBox3.Checked)
{
form4 = new Form4(this);
form4.Show();
}
if (checkBox4.Checked)
{
form5 = new Form5(this);
form5.Show();
}
this.Hide();
}
}
}
將以下代碼粘貼到 Form1 設計器中:
namespace WindowsFormsApp2
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.checkBox3 = new System.Windows.Forms.CheckBox();
this.checkBox4 = new System.Windows.Forms.CheckBox();
this.button1 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.checkBox4);
this.groupBox1.Controls.Add(this.checkBox3);
this.groupBox1.Controls.Add(this.checkBox2);
this.groupBox1.Controls.Add(this.checkBox1);
this.groupBox1.Location = new System.Drawing.Point(12, 12);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(274, 73);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Forms";
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(17, 32);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(55, 17);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "Form2";
this.checkBox1.UseVisualStyleBackColor = true;
//
// checkBox2
//
this.checkBox2.AutoSize = true;
this.checkBox2.Location = new System.Drawing.Point(78, 32);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new System.Drawing.Size(55, 17);
this.checkBox2.TabIndex = 1;
this.checkBox2.Text = "Form3";
this.checkBox2.UseVisualStyleBackColor = true;
//
// checkBox3
//
this.checkBox3.AutoSize = true;
this.checkBox3.Location = new System.Drawing.Point(139, 32);
this.checkBox3.Name = "checkBox3";
this.checkBox3.Size = new System.Drawing.Size(55, 17);
this.checkBox3.TabIndex = 2;
this.checkBox3.Text = "Form4";
this.checkBox3.UseVisualStyleBackColor = true;
//
// checkBox4
//
this.checkBox4.AutoSize = true;
this.checkBox4.Location = new System.Drawing.Point(200, 32);
this.checkBox4.Name = "checkBox4";
this.checkBox4.Size = new System.Drawing.Size(55, 17);
this.checkBox4.TabIndex = 3;
this.checkBox4.Text = "Form5";
this.checkBox4.UseVisualStyleBackColor = true;
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 91);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(199, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Open Selected Forms";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click = new System.EventHandler(this.button1_Click_1);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.CheckBox checkBox4;
private System.Windows.Forms.CheckBox checkBox3;
private System.Windows.Forms.CheckBox checkBox2;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Button button1;
}
}
添加 4 個 Windows 表單:Form2、Form3、Form4 和 Form5。
將下面的代碼粘貼到每一個,只需將每個上的表單名稱更改為正確的名稱(Form3、Form4、Form5):
using System;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form2 : Form
{
public Form2(Form1 Form1In)
{
InitializeComponent();
form1 = Form1In;
}
Form1 form1;
private void button1_Click(object sender, EventArgs e)
{
form1.Show();
}
}
}
最后將下面的代碼粘貼到每個表單的設計器中,并將 Form2 以外的表單的名稱更改為它們各自的名稱:
namespace WindowsFormsApp2
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(22, 21);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Show Form1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click = new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
}
}
然后運行它。
uj5u.com熱心網友回復:
這是一個閃亮的新輪子。它將所有邏輯集中在一個 Manager 類中。
一個子表單基類
首先,我們將為所有子表單創建一個基類。這樣,您就不需要將表單切換邏輯放入每個表單中。
在您的專案中創建一個新表單,命名為ShowHideFormBase ,更改為“查看代碼”視圖并更改樣板檔案,使其看起來像這樣:
public partial class ShowHideFormBase : Form
{
public ShowHideFormManager Manager { get; private set; }
public ShowHideFormBase()
{
Manager = null;
InitializeComponent();
}
public void SetManager (ShowHideFormManager manager)
{
Manager = manager;
}
}
我們稍后會創建 Manager 類
我真的很想有一個建構式,ShowHideFormBase它將一個ShowHideFormManager實體作為引數。Windows 表單設計器真的不喜歡具有非默認建構式的表單基類。這就是人生。
在此表單的設計器中,顯示表單的屬性,然后更改為“事件”視圖(閃電圖示)。雙擊FormClosing事件。這會將ShowHideWindowBase_FormClosing處理程式的此代碼添加到您的基類中。更改它,使其看起來像這樣。
private void ShowHideWindowBase_FormClosing(object sender, FormClosingEventArgs e)
{
Debug.Assert(Manager != null); //Make sure that SetManager was called
e.Cancel = true;
Manager.HideAll();
}
上面e.Cancel=true;寫著“我知道有人試圖關閉這個表單,不要讓它關閉”。相反,我們將隱藏所有子表單,包括這個子表單。
那堂課就完成了。現在讓我們做經理類。
管理者
在您的專案名稱中創建一個新類ShowHideFormManager。使用此代碼:
public class ShowHideFormManager
{
private Dictionary<string, ShowHideFormBase> _forms = new Dictionary<string, ShowHideFormBase>();
private Form _mainForm;
public ShowHideFormManager(Form mainForm)
{
_mainForm = mainForm;
}
// This will add a sub form to be managed
public void Add(string windowsName, ShowHideFormBase form)
{
_forms.Add(windowsName, form);
}
// This will hide all managed sub forms and show the main form
public void HideAll()
{
_mainForm.Show();
foreach (var formPair in _forms)
{
formPair.Value.Hide();
}
}
// this will hide the main form and show the named sub form
public void Show(string formToShowName)
{
_mainForm.Hide();
foreach (var formPair in _forms)
{
if (formPair.Key != formToShowName)
{
formPair.Value.Hide();
}
}
if (_forms.TryGetValue(formToShowName, out var formToShow))
{
formToShow.Show();
}
}
}
請注意,閃亮的新輪子大約有 75 或 80 行代碼。
申請的主表格
我假設您Form1在應用程式中命名了一個表單(在您創建應用程式時創建的表單)。
添加此私有欄位:
private ShowHideFormManager _manager = null;
和這個公共方法:
public void SetManager(ShowHideFormManager manager)
{
_manager = manager;
}
在主表單上放置三個按鈕,將它們的Name屬性設定為“SubForm1Btn”、“SubForm2Btn”和“SubForm3Btn”,并將這些Text屬性設定為有用的東西。在點擊處理程式中,為每個按鈕做這樣的事情:
private void SubForm1Btn_Click(object sender, EventArgs e)
{
_manager.Show("Sub Form 1");
}
改變數字,顯然
子表格
現在在您的專案中創建三個新表單。命名類SubForm1,SubForm2和SubForm3。
Note, I create three sub forms, not 4 - creating the fourth would be easy
Open the code view for each form. The only change we are going to make to these three forms is to change the class declaration and constructor to:
public partial class SubForm1 : ShowHideFormBase
{
public SubForm1(ShowHideFormManager manager)
{
InitializeComponent();
SetManager(manager);
}
}
For all three forms: 1, 2, and 3, obviously
This changes the base class from Form to ShowHideFormBase. It also makes it so each of these forms must be initialized with a ShowHideFormManager instance, and we call SetManager (which is a base class method) to initialize the form with the manager.
Finally, changes to Program.cs
Finally, we'll make some minor changes to the Main method in Program.cs. These changes:
- Create the Manager
- Instantiate the main form as well as the three sub forms.
- And call the normal
Application.Runmethod
Here's the new Main method:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var mainForm = new Form1();
var manager = new ShowHideFormManager(mainForm);
mainForm.SetManager(manager);
var subForm1 = new SubForm1(manager);
var subForm2 = new SubForm2(manager);
var subForm3 = new SubForm3(manager);
manager.Add("Sub Form 1", subForm1);
manager.Add("Sub Form 2", subForm2);
manager.Add("Sub Form 3", subForm3);
Application.Run(mainForm);
}
Important: The strings you use in the manager.Add call (e.g. "Sub Form 1") must exactly match the string you pass in each of the calls to _manager.Show("Sub Form 1"); in the button handlers. When I finished this, I decided that I should have used an enum, but everything was working.
How it Works
Press F5 and the main form (with three buttons on it) should pop up. Press any of the buttons and the corresponding form will pop up (and the main form will hide). Close the sub form, and the main form will re-open letting you press that button again or another button.
While you are looking at this, notice that there are almost no changes to the Main Form or the Sub Forms. The magic is all in the form base class and the manager class. You can do whatever you want with any of the forms (as long as you get those minimal changes in the right places.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/352161.html
