如何在不丟失用戶輸入的值的情況下回傳表單 1?
我試過:
Form1 fm1 = new Form1();
fm1.Show();
this.Hide();
但我抹去了價值觀
uj5u.com熱心網友回復:
我制作了一個示例應用程式來展示如何讓兩個表單以我認為您正在尋找的方式相互互動。
結果如下所示:

- 在我的應用程式中
Form1有一個按鈕來創建和顯示Form2. 每次這樣做時,它都會記錄打開表單的次數。 - 當您關閉任何打開的
Form2表單時,它還會記錄關閉的表單數量。 Form2有一個引發事件的文本框,以便Form1可以顯示該文本。- 我已經通過了
Form2開放計數,以便每個人都Form2可以在其標題中顯示計數。
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.labelOpenForm2Count.Text = "";
this.labelCloseForm2Count.Text = "";
this.labelEnteredText.Text = "";
}
private int _openForm2Count = 0;
private int _closeForm2Count = 0;
private void buttonOpenForm2_Click(object sender, EventArgs e)
{
_openForm2Count = 1;
var form2 = new Form2(_openForm2Count);
form2.FormClosed = Form2_FormClosed;
form2.EnteredTextChanged = Form2_EnteredTextChanged;
this.labelOpenForm2Count.Text = _openForm2Count.ToString();
form2.Show();
}
private void Form2_EnteredTextChanged(object sender, string enteredText)
{
this.labelEnteredText.Text = enteredText;
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
if (sender is Form2 form2)
{
form2.FormClosed -= Form2_FormClosed;
_closeForm2Count = 1;
this.labelCloseForm2Count.Text = _closeForm2Count.ToString();
}
}
}
Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form2(int count) : this()
{
this.Text = count.ToString();
}
private void buttonClose_Click(object sender, EventArgs e)
{
this.Close();
}
public event EventHandler<string> EnteredTextChanged;
private void textBox_TextChanged(object sender, EventArgs e)
{
this.EnteredTextChanged?.Invoke(this, textBox.Text);
}
}
以及設計器中的代碼,以便您可以復制、粘貼和運行我的代碼。
Form1:
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.buttonOpenForm2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.labelOpenForm2Count = new System.Windows.Forms.Label();
this.labelCloseForm2Count = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.labelEnteredText = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// buttonOpenForm2
//
this.buttonOpenForm2.Location = new System.Drawing.Point(13, 13);
this.buttonOpenForm2.Name = "buttonOpenForm2";
this.buttonOpenForm2.Size = new System.Drawing.Size(116, 23);
this.buttonOpenForm2.TabIndex = 0;
this.buttonOpenForm2.Text = "Open Form2";
this.buttonOpenForm2.UseVisualStyleBackColor = true;
this.buttonOpenForm2.Click = new System.EventHandler(this.buttonOpenForm2_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(13, 43);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 23);
this.label1.TabIndex = 1;
this.label1.Text = "Opened Form1:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label2
//
this.label2.Location = new System.Drawing.Point(13, 62);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 23);
this.label2.TabIndex = 2;
this.label2.Text = "Closed Form2:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// labelOpenForm2Count
//
this.labelOpenForm2Count.AutoSize = true;
this.labelOpenForm2Count.Location = new System.Drawing.Point(119, 47);
this.labelOpenForm2Count.Name = "labelOpenForm2Count";
this.labelOpenForm2Count.Size = new System.Drawing.Size(38, 15);
this.labelOpenForm2Count.TabIndex = 3;
this.labelOpenForm2Count.Text = "label3";
this.labelOpenForm2Count.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// labelCloseForm2Count
//
this.labelCloseForm2Count.AutoSize = true;
this.labelCloseForm2Count.Location = new System.Drawing.Point(119, 66);
this.labelCloseForm2Count.Name = "labelCloseForm2Count";
this.labelCloseForm2Count.Size = new System.Drawing.Size(38, 15);
this.labelCloseForm2Count.TabIndex = 4;
this.labelCloseForm2Count.Text = "label4";
this.labelCloseForm2Count.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label3
//
this.label3.Location = new System.Drawing.Point(13, 96);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(100, 23);
this.label3.TabIndex = 5;
this.label3.Text = "Entered Text:";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// labelEnteredText
//
this.labelEnteredText.AutoSize = true;
this.labelEnteredText.Location = new System.Drawing.Point(119, 100);
this.labelEnteredText.Name = "labelEnteredText";
this.labelEnteredText.Size = new System.Drawing.Size(38, 15);
this.labelEnteredText.TabIndex = 6;
this.labelEnteredText.Text = "label4";
this.labelEnteredText.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(297, 135);
this.Controls.Add(this.labelEnteredText);
this.Controls.Add(this.label3);
this.Controls.Add(this.labelCloseForm2Count);
this.Controls.Add(this.labelOpenForm2Count);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.buttonOpenForm2);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button buttonOpenForm2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label labelOpenForm2Count;
private System.Windows.Forms.Label labelCloseForm2Count;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label labelEnteredText;
}
Form2:
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.buttonClose = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// buttonClose
//
this.buttonClose.Location = new System.Drawing.Point(167, 61);
this.buttonClose.Name = "buttonClose";
this.buttonClose.Size = new System.Drawing.Size(75, 23);
this.buttonClose.TabIndex = 0;
this.buttonClose.Text = "Close";
this.buttonClose.UseVisualStyleBackColor = true;
this.buttonClose.Click = new System.EventHandler(this.buttonClose_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(13, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(61, 15);
this.label1.TabIndex = 1;
this.label1.Text = "Enter Text:";
//
// textBox
//
this.textBox.Location = new System.Drawing.Point(13, 32);
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(229, 23);
this.textBox.TabIndex = 2;
this.textBox.TextChanged = new System.EventHandler(this.textBox_TextChanged);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(254, 94);
this.Controls.Add(this.textBox);
this.Controls.Add(this.label1);
this.Controls.Add(this.buttonClose);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button buttonClose;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox;
}
如果您需要對代碼的任何部分進行進一步解釋,請在下面的評論中提問。
uj5u.com熱心網友回復:
這就是我在我的代碼中用多種形式所做的:
public Form2 form2;
public Form3 form3;
public Form4 form4;
private Form activeForm = null;
public Form1() {
InitializeComponent();
form2 = new Form2();
form3 = new Form3();
form4 = new Form4();
}
private void openForm(Form childForm) {
if (activeForm != null) {
activeForm.Visible = false;
activeForm = null;
}
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
childForm.Refresh();
// Container Panel is my parent panel
containerPanel.Controls.Add(childForm);
containerPanel.Tag = childForm;
containerPanel.Refresh();
childForm.BringToFront();
childForm.Show();
childForm.Refresh();
}
uj5u.com熱心網友回復:
在您的應用程式中,創建 ac# 類并將其設為靜態。在那里宣告靜態變數。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
static class Class1
{
public static string static_variable = null;
}
}
然后您可以以兩種形式訪問此變數。
Class1.static_variable = "AAA";
如果你不明白,請告訴我。您也可以使用委托傳遞值,但這種方式比那樣容易。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/344146.html
