谢谢两位的回答,可能是我描述的不够清楚。using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { myclass.YourSelect(textBox1.Text); } } public class myclass { public static void YourSelect(string No) { if (No == "1") { Label label=????;//此处判断是否存在label1并获取句柄 MessageBox.Show(label.Text); } else { Label label=????;//此处判断是否存在label2并获取句柄 MessageBox.Show(label.Text); } } } }也就是说要调用另一个类,这样不知道能不能实现?
我试了一下,使用四楼的方法是可以的,但还是必须将当前的Form传递过去,有没有办法不用传递Form而达到目的? 使用4楼方法实现代码如下: namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { myclass my = new myclass(); my.YourSelect(this,textBox1.Text); } } public class myclass { public void YourSelect(System.Windows.Forms.Form form, string No) { PropertyInfo info = form.GetType().GetProperty("Controls"); System.Windows.Forms.Control.ControlCollection collection = (System.Windows.Forms.Control.ControlCollection)info.GetValue(form, null); Control[] controls = collection.Find("label" + No, false); if (controls.Length == 1) { MessageBox.Show(controls[0].Text); } } } }
private Label label = null;Onclick_button1(object sender, EventArgs e)
{
label = new Label();
}Onclick_button2(object sender, EventArgs e)
{
if(label != null)
{
MessageBox.Show(label.Text);
}
}
private void button1_Click(object sender, EventArgs e)
{
Label label = new Label();
label.Name = "Label1";
label.Text = "标签";
this.Controls.Add(label);
}private void button2_Click(object sender, EventArgs e)
{
Control[] controls = this.Controls.Find("Label1", false);
if (controls.Length == 1)
{
MessageBox.Show(controls[0].Text);
}
}
private void button1_Click(object sender, EventArgs e)
{
Label label = new Label();
label.Name = "Label1";
label.Text = "标签";
this.Controls.Add(label);
}private void button2_Click(object sender, EventArgs e)
{
PropertyInfo info = this.GetType().GetProperty("Controls");
ControlCollection collection = (ControlCollection)info.GetValue(this, null);
Control[] controls = collection.Find("Label1", false);
if (controls.Length == 1)
{
MessageBox.Show(controls[0].Text);
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
myclass.YourSelect(textBox1.Text);
}
} public class myclass
{
public static void YourSelect(string No)
{
if (No == "1")
{
Label label=????;//此处判断是否存在label1并获取句柄
MessageBox.Show(label.Text);
}
else
{
Label label=????;//此处判断是否存在label2并获取句柄
MessageBox.Show(label.Text);
}
}
}
}也就是说要调用另一个类,这样不知道能不能实现?
使用4楼方法实现代码如下:
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
myclass my = new myclass();
my.YourSelect(this,textBox1.Text);
}
} public class myclass
{
public void YourSelect(System.Windows.Forms.Form form, string No)
{
PropertyInfo info = form.GetType().GetProperty("Controls");
System.Windows.Forms.Control.ControlCollection collection = (System.Windows.Forms.Control.ControlCollection)info.GetValue(form, null);
Control[] controls = collection.Find("label" + No, false);
if (controls.Length == 1)
{
MessageBox.Show(controls[0].Text);
}
}
}
}
因相关代码非常的长,因此不便贴出。
希望没把大家整糊涂,抱歉抱歉,如果问题解决我再加分。
不传form是绝对不可能的。