如何在类里面声明一个picturebox,或者声明一个button可以在form里用

foutcast 2008-04-02 08:23:09
我现在是这样写的
在class1里面
public class Class1
{
public string st;
public Button Citybutton;
public string showst()
{
return st;
}
public Class1()
{
Citybutton = new Button();
}

public void Citybutton_Click(object sender, EventArgs e)
{
st = "wertyu";
}

在FORM里面拖一个button2,然后
cla.Citybutton = this.button2;
那么我现在按了button2,是不是能执行class1里面的Citybutton_Click?
如果不能,这个类里面应该要怎么写呢,谢谢啦

...全文
68 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
foutcast 2008-04-02
  • 打赏
  • 举报
回复
谢谢
搞定了
dancingbit 2008-04-02
  • 打赏
  • 举报
回复

public Button Citybutton
{
get
{
return citybutton;
}
set
{
citybutton=value;
citybutton.Click+=new EventHandler(this.CityButton_Click);
}
}
foutcast 2008-04-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dancingbit 的回复:]
把字段定义为public不是一种好的做法。可以定义成私有成员,再定义对应属性,这样,甚至还可以在set方法中自动为这个Button加上Click事件处理方法。
[/Quote]

你是不是说在class1里写成
private Button citybutton;
public Button Citybutton
{
get
{
return citybutton;
}
set
{
}
}
是这样吗,你说的在set里加上click方法要怎么写呢,谢谢
foutcast 2008-04-02
  • 打赏
  • 举报
回复
加了这一句是不是就不用在form里面写
cla.Citybutton = this.button2;
这句了
dancingbit 2008-04-02
  • 打赏
  • 举报
回复
把字段定义为public不是一种好的做法。可以定义成私有成员,再定义对应属性,这样,甚至还可以在set方法中自动为这个Button加上Click事件处理方法。
dancingbit 2008-04-02
  • 打赏
  • 举报
回复
肯定不能。
在Form2中某个合适的地方button2.Click+=new EventHandler(cla.CityButton_Click);
没法下载,到这折腾一把试试。 本文由abc2253130贡献 doc文档可能在WAP端浏览体验不佳。建议您优先选择TXT,或下载源文件到本机查看。 C#(WINFORM)学习 一、 C#基础 基础 类型和变量 类型和变量 类型 C# 支持两种类型:“值类型”和“引用类型”。值类型包括简单类型(如 char、int 和 float 等)、枚举类型和结构类型。引用类型包括类 (Class)类 型、接口类型、委托类型和数组类型。 变量的类型声明 变量的类型声明 每个变量必须预先声明其类型。如 int a; int b = 100; float j = 4.5; string s1; 用 object 可以表示所有的类型。 预定义类型 下表列出了预定义类型,并说明如何使用。 类型 object 说明 所有其他类型的最终 基类型 字符串类型; 字符串是 Unicode 字符序列 8 位有符号整型 16 位有符号整型 32 位有符号整型 64 位有符号整型 示例 object o = null; 范围 string sbyte short int long string s = "hello"; sbyte val = 12; short val = 12; int val = 12; long val1 = 12; -128 到 127 -32,768 到 32,767 -2,147,483,648 2,147,483,647 -9,223,372,036,854,775,808 到 第1页 C#(WINFORM)学习 long val2 = 34L; 到 9,223,372,036,854,775,807 byte ushort 8 位无符号整型 16 位无符号整型 byte val1 = 12; ushort val1 = 12; uint val1 = 12; uint 32 位无符号整型 uint val2 = 34U; ulong val1 = 12; ulong val2 = 34U; ulong 64 位无符号整型 ulong val3 = 56L; ulong val4 = 78UL; float 单精度浮点型 float val = 1.23F;7 位 double val1 = 1.23; double 双精度浮点型 double val2 = ±5.0 × 10?324 ±1.7 × 10 308 0 到 255 0 到 65,535 0 到 4,294,967,295 0 到 18,446,744,073,709,551,615 ±1.5 × 10?45 ±3.4 × 10 38 到 到 4.56D;15-16 布尔型;bool 值或为 真或为假 字符类型;char 值是 一个 Unicode 字符 精确的小数类型, 具有 28 个有效数字 bool val1 = true; bool val2 = false; char val = 'h'; decimal val = bool char decimal DateTime ±1.0 × 10?28 ±7.9 × 10 28 到 1.23M;28-29 变量转换 简单转换: float f = 100.1234f; 可以用括号转换: short s = (short)f 也可以利用 Convert 方法来转换: string s1; s1=Convert.ToString(a); MessageBox.Show(s1); 常用 Convert 方法有: 第2页 C#(WINFORM)学习 C# Convert.ToBoolean Convert.ToByte Convert.ToChar Convert.ToDateTime Convert.ToDecimal Convert.ToDouble Convert.ToInt16 Convert.ToInt32 Convert.ToInt64 Convert.ToSByte Convert.ToSingle Convert.ToString Convert.ToUInt16 Convert.ToUInt32 Convert.ToUInt64 备注 Math 类 常用科学计算方法: C# Math.Abs Math.Sqrt Math.Ro
用VB.NET做的小游戏--打地鼠 Public Class Form1 Inherits System.Windows.Forms.Form Dim pic As New PictureBox Dim goal As Integer Dim i As Integer Dim j As Integer Dim xx1() As Integer = {135, 277, 417} Dim xx2() As Integer = {157, 367} Dim xx3() As Integer = {32, 251, 482} Dim timer As Double = 1 Dim count As Integer '计时器跳动次数 Dim ifclick As Boolean Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Text = "小游戏" Me.Size = New Size(650, 465) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Timer1.Interval = 600 Timer1.Enabled = True Button2.Visible = True End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If Button2.Text = "暂停" Then Button2.Text = "继续" Timer1.Enabled = False PictureBox1.Enabled = False ElseIf Button2.Text = "继续" Then Button2.Text = "暂停" Timer1.Enabled = True PictureBox1.Enabled = True End If End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Me.Close() End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick ifclick = False PictureBox1.Enabled = True If timer = 0 Then Timer1.Enabled = False MsgBox("游戏结束,您的得分:" + goal.ToString) Button3.Visible = False Exit Sub End If addpic() timer = 10 - count * 0.5
C#编写的GUI游戏 源代码 using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace gn { public partial class Form1 : Form { string ready; int counti = 1; public Form1() { InitializeComponent(); this.label1.Hide(); this.button2.Hide(); this.button1.Hide(); this.button3.Hide(); this.button4.Hide(); this.textBox1.Hide(); this.pictureBox1.Hide(); this.pictureBox2.Hide(); } private void button1_Click(object sender, EventArgs e) { string number; int x, y; int count = 0; number = textBox1.Text; if (number.Length != 4) { MessageBox.Show("请输入4位数字"); } else { if (counti <=8) { if (count <= 3) { x = 0; y = 0; for (int i = 0; i <= 3; i++) { for (int j = 0; j <= 3; j++) { if (number[i] == ready[j]) { if (i == j) ++x; else ++y; } } } label2.Text = "你已经猜过" + counti + "次"; ++count; label3.Text = x.ToString() + "A" + y.ToString() + "B"; if (number == ready) { this.label2.Text = "答对了"; this.pictureBox1.Show(); } } if (counti == 8) { this.button1.Enabled = false; this.pictureBox2.Show(); } } else { this.button1.Enabled = false; this.pictureBox2.Show(); } ++counti; } } private void button2_Click(object sender, EventArgs e) { MessageBox.Show(ready); } private void button3_Click(object sender, EventArgs e) { //this.button2.Hide(); this.textBox1.Text = ""; this.label3.Text = ""; this.label2.Text = ""; this.button1.Enabled = true; counti = 1; this.pictureBox1.Hide(); this.pictureBox2.Hide(); Random r = new Random(); string[] a; a = new string[8]; a[0] = "1263"; a[1] = "6598"; a[2] = "9654"; a[3] = "5986"; a[4] = "2915"; a[5] = "2046"; a[6] = "9035"; a[7] = "1057"; int z; z = r.Next(8); ready = a[z]; } private void label1_Click(object sender, EventArgs e) { } private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { } private void button4_Click(object sender, EventArgs e) { Close(); } private void button5_Click(object sender, EventArgs e) { Random r = new Random(); string[] a; a = new string[8]; a[0] = "1263"; a[1] = "6598"; a[2] = "9654"; a[3] = "5986"; a[4] = "2915"; a[5] = "2046"; a[6] = "9035"; a[7] = "1057"; int z; z = r.Next(8); ready = a[z]; this.textBox1.Show(); this.button1.Show(); this.button3.Show(); this.button4.Show(); this.label1.Show(); this.button2.Show(); this.button5.Hide(); } private void label4_Click(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { } private void pictureBox1_Click(object sender, EventArgs e) { } private void pictureBox1_Click_1(object sender, EventArgs e) { } } }

110,533

社区成员

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

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

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