C# 如何访问另一个类';什么方法?
我有一个WinForms应用程序。在主窗体中,我编写了一个方法,该方法将清除作为参数传递的任何形式的所有文本框。我想从另一个窗体调用此方法。下面的代码是我经过多次尝试/错误并浏览该网站后得出的。每次单击新窗体的“全部清除”按钮时实例化主窗体的新版本是否是一种良好做法?如果我要用它自己的clear all按钮创建另一个表单,我必须通过类似的实践实例化一个新的主表单(除非我将方法设置为静态)?有人能建议从其他表单访问一个表单的方法的替代方法吗?非常感谢 编辑:我知道将方法设置为静态将是一个简单而有效的解决方案,但我对使用非静态方法很好奇C# 如何访问另一个类';什么方法?,c#,winforms,methods,C#,Winforms,Methods,我有一个WinForms应用程序。在主窗体中,我编写了一个方法,该方法将清除作为参数传递的任何形式的所有文本框。我想从另一个窗体调用此方法。下面的代码是我经过多次尝试/错误并浏览该网站后得出的。每次单击新窗体的“全部清除”按钮时实例化主窗体的新版本是否是一种良好做法?如果我要用它自己的clear all按钮创建另一个表单,我必须通过类似的实践实例化一个新的主表单(除非我将方法设置为静态)?有人能建议从其他表单访问一个表单的方法的替代方法吗?非常感谢 编辑:我知道将方法设置为静态将是一个简单而有效
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void ClearAll(Form formToClear) //CLEAR TEXTBOXES
{
foreach (var box in formToClear.Controls.OfType<TextBox>())
{
box.Text = "";
}
}
}
public partial class NewItemForm : Form
{
public NewItemForm()
{
InitializeComponent();
}
private void clearAllButton_Click(object sender, EventArgs e)
{
Form1 mainForm=new Form1();
mainForm.ClearAll(this);
}
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
public void ClearAll(Form-toclear)//清除文本框
{
foreach(formToClear.Controls.OfType()中的变量框)
{
box.Text=“”;
}
}
}
公共部分类NewItemForm:Form
{
公共NewItemForm()
{
初始化组件();
}
私有void clearAllButton_单击(对象发送者,事件参数e)
{
Form1 mainForm=新Form1();
mainForm.ClearAll(这个);
}
}
几乎可以肯定,您应该创建一个带有静态函数的静态实用程序类。这将通过防止创建不必要的Form1
实例来保留内存。根据表单类的大小和其中包含的对象/变量,创建一个新实例只是为了使用该类中的一个函数,最终可能会导致大量内存随着时间的推移而丢失。静态类中的静态方法可以防止这种情况发生,因为静态方法在流程的生命周期中只定义/实例化一次,而不是每个实例定义/实例化一次
您可能应该选择以下内容:
internal static class FormUtils
{
internal static void ClearAllTextBoxes(Form form)
{
if (form == null)
return;
if (form.Controls.Count <= 0)
return;
foreach (var box in form.Controls.OfType<TextBox>())
{
box.Clear();
}
}
}
几乎可以肯定,您应该创建一个带有静态函数的静态实用程序类。这将通过防止创建不必要的
Form1
实例来保留内存。根据表单类的大小和其中包含的对象/变量,创建一个新实例只是为了使用该类中的一个函数,最终可能会导致大量内存随着时间的推移而丢失。静态类中的静态方法可以防止这种情况发生,因为静态方法在流程的生命周期中只定义/实例化一次,而不是每个实例定义/实例化一次
您可能应该选择以下内容:
internal static class FormUtils
{
internal static void ClearAllTextBoxes(Form form)
{
if (form == null)
return;
if (form.Controls.Count <= 0)
return;
foreach (var box in form.Controls.OfType<TextBox>())
{
box.Clear();
}
}
}
您不必使ClearAll方法保持静态。如果您保留对主窗体的全局引用就足够了。您可以在Program.cs中执行此操作。但这不是最好的办法
static class Program {
public static Form1 TheForm;
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
TheForm = new Form1();
Application.Run(TheForm);
}
}
又来了!仅仅因为它是可以做到的,并不意味着我会鼓励你去做。这不符合面向对象编程的精神
如果希望访问Form1方法的唯一原因是清除文本框,那么我建议创建一个中间类:
public class InterForm : Form
{
public void ClearAll() //CLEAR TEXTBOXES
{
foreach (var box in this.Controls.OfType<TextBox>())
{
box.Text = "";
}
}
}
公共类交互:表单
{
public void clearlall()//清除文本框
{
foreach(此.Controls.OfType()中的var框)
{
box.Text=“”;
}
}
}
所有其他表单都应该继承自
InterForm
您不必使ClearAll方法保持静态。如果您保留对主窗体的全局引用就足够了。您可以在Program.cs中执行此操作。但这不是最好的办法
static class Program {
public static Form1 TheForm;
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
TheForm = new Form1();
Application.Run(TheForm);
}
}
又来了!仅仅因为它是可以做到的,并不意味着我会鼓励你去做。这不符合面向对象编程的精神
如果希望访问Form1方法的唯一原因是清除文本框,那么我建议创建一个中间类:
public class InterForm : Form
{
public void ClearAll() //CLEAR TEXTBOXES
{
foreach (var box in this.Controls.OfType<TextBox>())
{
box.Text = "";
}
}
}
公共类交互:表单
{
public void clearlall()//清除文本框
{
foreach(此.Controls.OfType()中的var框)
{
box.Text=“”;
}
}
}
所有其他表单都应该继承自
InterForm
您可以在这里使用事件的概念
要调用主窗体的方法的另一个窗体应该有一个事件
在创建此表单的实例时(我猜您只是从主表单创建此表单的实例),您可以将此表单的事件订阅到目标方法
因此,无论何时需要调用主窗体的方法(从另一个窗体),都可以引发该事件
请参见下面的示例代码
假设Form1
是您的主要表单
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void f2_ClearTextBoxOfForm(Form targetForm)
{
foreach (Control control in targetForm.Controls)
{
if (control is TextBox)
((TextBox)control).Text = string.Empty;
}
}
private void btnShowForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ClearTextBoxOfForm += f2_ClearTextBoxOfForm;
f2.Show();
}
}
而Form2
是另一个要清除Form1
public delegate void ClearTextBoxEventHandler (Form targetForm);
public partial class Form2 : Form
{
public event ClearTextBoxEventHandler ClearTextBoxOfForm;
public Form2()
{
InitializeComponent();
}
private void btnClearTextBox_Click(object sender, EventArgs e)
{
if (ClearTextBoxOfForm != null)
{
//here passing 'this' means we want to clear textBoxes of this form (Form2)
//you can pass any Form's object of which you want to clear Textboxes
ClearTextBoxOfForm(this);
}
}
}
您可以在此处使用事件的概念
要调用主窗体的方法的另一个窗体应该有一个事件
在创建此表单的实例时(我猜您只是从主表单创建此表单的实例),您可以将此表单的事件订阅到目标方法
因此,无论何时需要调用主窗体的方法(从另一个窗体),都可以引发该事件
请参见下面的示例代码
假设Form1
是您的主要表单
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void f2_ClearTextBoxOfForm(Form targetForm)
{
foreach (Control control in targetForm.Controls)
{
if (control is TextBox)
((TextBox)control).Text = string.Empty;
}
}
private void btnShowForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ClearTextBoxOfForm += f2_ClearTextBoxOfForm;
f2.Show();
}
}
而Form2
是另一个要清除Form1
public delegate void ClearTextBoxEventHandler (Form targetForm);
public partial class Form2 : Form
{
public event ClearTextBoxEventHandler ClearTextBoxOfForm;
public Form2()
{
InitializeComponent();
}
private void btnClearTextBox_Click(object sender, EventArgs e)
{
if (ClearTextBoxOfForm != null)
{
//here passing 'this' means we want to clear textBoxes of this form (Form2)
//you can pass any Form's object of which you want to clear Textboxes
ClearTextBoxOfForm(this);
}
}
}
只需创建一个静态
实用程序函数,该函数将清除表单
的文本框
是否有此方法必须成为主表单成员的原因?为什么不创建一个单独的静态类呢?为什么不创建一个继承自表单的中间类,而其他所有类都继承自该类?不,创建主表单的新实例一点都不好。只需创建一个静态
实用函数,它将清除表单的文本框是否存在