大家帮忙看看这个应该怎么写,只有一句话!!!急等!!!

zhuzhusex1012 2012-03-06 10:02:38
有一个
public class ComMethod
{
public static void ClearAllTextBox(ref Form form)
{
foreach (Control control in form.Controls)
{
if (control.GetType().ToString().Contains("TextBox"))
{
((TextBox)control).Text = "";
}
}
}
}

我想在窗体中调用这个类中的方法,清空TextBox,
ComMethod.ClearAllTextBox(ref this); //this只读,,,

请问大家应该怎么写啊?
...全文
320 24 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
blueyady 2012-03-06
  • 打赏
  • 举报
回复
我也觉得是去掉ref
cnwin 2012-03-06
  • 打赏
  • 举报
回复
楼主说说怎么不对了?是不是楼主准备些窗体的扩展方法啊?那你的扩展方法所在的类应该是静态类,方法为静态方法,并在前面加this

public static class ComMethod
{
public static void ClearAllTextBox(this Form form)
{
foreach (Control control in form.Controls)
{
if (control.GetType().ToString().Contains("TextBox"))
{
((TextBox)control).Text = "";
}
}
}
}

zhuzhusex1012 2012-03-06
  • 打赏
  • 举报
回复
oK ,,,谢谢大家的回答,,,我自己搞错了!!!!!结贴
fanfanrot 2012-03-06
  • 打赏
  • 举报
回复
你先确定下你的textbox空间是直接放在窗体里面的还是放在窗体子控件里面的。。。。
你这个方法只能情况窗体的子控件textbox
方法换个写法
public class ComMethod
{
public static void ClearAllTextBox(Control control)
{
//遍历子控件
foreach (Control child in control.Controls)
{
if (child is TextBox)
{
((TextBox)child).Text = "";
}
//如果子控件扔包含子控件则子调用删除
ClearAllTextBox(child);
}
}
}

调用的入参不变
ComMethod.ClearAllTextBox(this);

测试过的 可以用
  • 打赏
  • 举报
回复
把所有的ref去掉
东海凌波 2012-03-06
  • 打赏
  • 举报
回复
上面窗体代码发错了,更正:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TEST
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
ComMethod.ClearAllTextBox(this );
}
}
}
东海凌波 2012-03-06
  • 打赏
  • 举报
回复
类代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TEST
{
public class ComMethod
{
public static void ClearAllTextBox( Form form)
{
foreach (Control control in form.Controls)
{
if (control.GetType().ToString().Contains("TextBox"))
{
((TextBox)control).Text = "";
}
}
}

}
}
窗体有3个TEXT,一个BUTTON,窗体代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TEST
{
public class ComMethod
{
public static void ClearAllTextBox( Form form)
{
foreach (Control control in form.Controls)
{
if (control.GetType().ToString().Contains("TextBox"))
{
((TextBox)control).Text = "";
}
}
}

}
}
Castiel丶Luo 2012-03-06
  • 打赏
  • 举报
回复
不可能还不对啊
Castiel丶Luo 2012-03-06
  • 打赏
  • 举报
回复
public class ComMethod
{
public static void ClearAllTextBox(ref Form form)
{
foreach (Control control in form.Controls)
{
if (control.GetType().ToString().Contains("TextBox"))
{
((TextBox)control).Text = "";
}
}
}
}

ComMethod.ClearAllTextBox(this); //this只读,,,

ck11926375 2012-03-06
  • 打赏
  • 举报
回复
public static void ClearAllTextBox(Form form)

ComMethod.ClearAllTextBox(this);

去掉ref。
不知道你怎么还没对。
bobo66542401 2012-03-06
  • 打赏
  • 举报
回复
楼上说的不错
全局变量 2012-03-06
  • 打赏
  • 举报
回复
[Quote=引用楼主 zhuzhusex1012 的回复:]
有一个
public class ComMethod
{
public static void ClearAllTextBox(ref Form form)
{
foreach (Control control in form.Controls)
{
if (control……
[/Quote]
你应该 在 show 的时候就吧 NEW 的窗体给这个方法
jshi123 2012-03-06
  • 打赏
  • 举报
回复
去掉ref
zhuzhusex1012 2012-03-06
  • 打赏
  • 举报
回复
上面的所有人都说的不对!!!!!!!!有没有人会啊??????????????
全局变量 2012-03-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lihanbing 的回复:]
public static void ClearAllTextBox(Form form)

不需要ref
[/Quote]
++
Castiel丶Luo 2012-03-06
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ziyouli 的回复:]
Form 本来就是引用类型的,没必要在加上ref了。
[/Quote]

++
去掉 ref 就可以执行
zhuzhusex1012 2012-03-06
  • 打赏
  • 举报
回复
楼上2个人说的都不对。。。。。。。。。。。。。急等。。。。。。。。。。。。。。
ziyouli 2012-03-06
  • 打赏
  • 举报
回复
Form 本来就是引用类型的,没必要在加上ref了。
ziyouli 2012-03-06
  • 打赏
  • 举报
回复
去掉ref就可以了。
zhuzhusex1012 2012-03-06
  • 打赏
  • 举报
回复
有没有人会啊???????????
加载更多回复(3)

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧