如何建100个button?

zqhdz 2003-10-18 02:07:21
在form中建100个button,用数组行不行?
使用时调用button[i],应该怎么定义和初始化呢?
请高手赐教!
...全文
90 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zqhdz 2003-10-21
  • 打赏
  • 举报
回复
看了上面的例子,懂了很多,不过我还有一个问题:
如果要设置这样的操作应该怎么写?
按了button i
则另button i+3 不可见,怎么样呢?

再有我要建一个PictureBox
在上面的类似加:
PictureBox toAdd=new PictureBox();

// 设置属性
toAdd.Name="PictureBox5";
toAdd.Location= new Point(localX, localY);
toAdd.TabStop = false;
toAdd.Image=((System.Drawing.Image)(resources.GetObject("PictureBox5.Image")));//这一句它说: The type or namespace name 'resources' could not be found (are you missing a using directive or an assembly reference?)
我该怎么设置它的图片呢?

coudoufu 2003-10-19
  • 打赏
  • 举报
回复
呵呵,用鼠标拖啊
jknight 2003-10-19
  • 打赏
  • 举报
回复
一个添加按钮的例子,希望有所帮助

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace addCtrlAtRuntimeApp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btn_addButtons;
private System.Windows.Forms.TextBox txt_msg;
private int count;//用于计数,添加了多少个按钮

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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.btn_addButtons = new System.Windows.Forms.Button();
this.txt_msg = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btn_addButtons
//
this.btn_addButtons.Name = "btn_addButtons";
this.btn_addButtons.TabIndex = 0;
this.btn_addButtons.Text = "添加按钮";
this.btn_addButtons.Click += new System.EventHandler(this.btn_addButtons_Click);
//
// txt_msg
//
this.txt_msg.Location = new System.Drawing.Point(80, 0);
this.txt_msg.Name = "txt_msg";
this.txt_msg.Size = new System.Drawing.Size(144, 21);
this.txt_msg.TabIndex = 1;
this.txt_msg.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.txt_msg,
this.btn_addButtons});
this.Name = "Form1";
this.Text = "动态添加控件";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void btn_addButtons_Click(object sender, System.EventArgs e)
{
// 计数,计算目前是添加的几个按钮
count++;

// 计算待添加按钮的位置
int localY = this.btn_addButtons.Height * count;
int localX = 10 * count;

Button toAddButton = new Button();

// 设置待添加按钮的属性
toAddButton.Name="Button " + count;
toAddButton.Text=" 按钮" + count + " ";
toAddButton.Location= new Point(localX, localY);

// 设置待添加按钮的事件
toAddButton.MouseEnter += new System.EventHandler(this.btn_MouseEnter);
toAddButton.MouseLeave += new System.EventHandler(this.btn_MouseLeave);
toAddButton.Click += new System.EventHandler(this.btn_Click);

// 把控件添加到窗口中
this.Controls.Add(toAddButton);
}

private void btn_MouseEnter(object sender, System.EventArgs e)
{
// unboxing
Button currentButton = (Button)sender;

// 设置鼠标的背景颜色
currentButton.BackColor = Color.Blue;;
}

private void btn_MouseLeave(object sender, System.EventArgs e)
{
//unboxing
Button currentButton = (Button)sender;

// 设置鼠标的背景颜色
currentButton.BackColor = System.Windows.Forms.Control.DefaultBackColor;
}

private void btn_Click(object sender, System.EventArgs e)
{
// unboxing
Button currentButton = (Button)sender;

// 显示点击了哪个按钮
txt_msg.Text = "你点击了" + currentButton.Text;
}
}
}
-_-中英文数字 2003-10-18
  • 打赏
  • 举报
回复
用鼠标放100个按钮也很累的 :)
dldl 2003-10-18
  • 打赏
  • 举报
回复
初始化也是个问题,不会把100个按钮都爹在一起吧!还是用鼠标放好!(开玩笑)
还是用循环,算好了每个按钮的长和宽,在根据窗体的宽窄和按钮之间的间距初始化好了。
storm97 2003-10-18
  • 打赏
  • 举报
回复
System.Windows.Forms.Button[] buttons=new Button[100];
for(int i=0;i<buttons.Length;i++)
{
buttons[i].Text=(i+1).ToString();
Buttons[i].Name=(i+1).ToString();
}
-_-中英文数字 2003-10-18
  • 打赏
  • 举报
回复
一样用button[i]定义和初始化

110,539

社区成员

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

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

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