C#动态创建Picture控件的问题

ssjywhr 2009-10-08 11:12:43
我想用以下代码从窗体的右到左依次创建一些picture控件,a的初始值为600,代表该控件离窗体最左边的距离。但我想让这些控件显示在窗体的正中央,并且所创建的控件的个数是不定的。请问这要怎么弄呢?
int a=600;
for (int i = 0; i<puke.Count; i++)
{
pict1[i] = new PictureBox();
pict1[i].SetBounds(a,350,78, 101);
pict1[i].BackColor = Color.AliceBlue;
pict1[i].BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.Controls.Add(this.pict1[i]);
a -= 23;
pict1[i].Click += new System.EventHandler(pict1_Click);
}
...全文
105 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
suners 2009-10-10
  • 打赏
  • 举报
回复
jyhu
smallmaster 2009-10-09
  • 打赏
  • 举报
回复
数学问题
liaoyukun111 2009-10-09
  • 打赏
  • 举报
回复
Winfrom和WEB最大的不同就是它有位置的问题(Location = new System.Drawing.Point)
给你一个例子
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace test
{
public partial class CB: Form
{

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new CB());
}
public CB()
{
int x = 5;
int y = 26;//位置
int tbnums = 10;//控件个数,一行数
int num = 5;
int j;//控件分行
int tempi;//和坐标有关的
int Ygap = 30;//行距
int Xgap = 80;//列距 LB TB为一列
for (int i = 0; i < tbnums; i++)
{
j = i / num;//第几行
tempi = i % num;
Label lb = new Label();
lb.Name = "lb" + ( i + 1 ).ToString();
lb.Text = ( i + 1 ).ToString() + ".";
lb.Size = new System.Drawing.Size(25, 12);
lb.Location = new System.Drawing.Point(x + tempi * Xgap, y + j * Ygap);

TextBox tb = new TextBox();
tb.Tag = "tb" + i;
tb.Name = "tb" + i.ToString();
tb.Size = new System.Drawing.Size(50, 20);
tb.KeyPress += new KeyPressEventHandler(tb_KeyPress);
tb.Location = new System.Drawing.Point(x + 25 + tempi * Xgap, y - 9 + j * Ygap);
this.Controls.Add(lb);
this.Controls.Add(tb);
}
Button bt = new Button();
bt.Text = "第四个的值";
bt.Click += new EventHandler(button1_Click);
Button bt1 = new Button();
bt1.Text = "所有的值";

bt1.Click += new EventHandler(button2_Click);
bt.Location = new System.Drawing.Point(4, 300);
bt1.Location = new System.Drawing.Point(84, 300);
this.Controls.Add(bt);
this.Controls.Add(bt1);

}
//分数的文本框只能输入数字
private void tb_KeyPress(object sender, KeyPressEventArgs e)
{
//阻止从键盘输入键
e.Handled = true;
if (e.KeyChar >= '0' && e.KeyChar <= '9')
{
e.Handled = false;
}
else
{
MessageBox.Show("分数只能是数字!!");
}

}

private void button1_Click(object sender, EventArgs e)
{
//通过ID找
TextBox tb = (TextBox)this.Controls.Find("tb3", false)[0];
MessageBox.Show(tb.Text);
}

private void button2_Click(object sender, EventArgs e)
{
//遍历
string get = "";
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
TextBox tb = c as TextBox;
get += tb.Text + " ";
}
}
MessageBox.Show(get.Trim ());

}

}
}

有时候为了方便遍历,可以把它们放在Panel中,这个应该可能解决放在中间的问题,不想加Panel就去找中间的坐标就行了
ssjywhr 2009-10-09
  • 打赏
  • 举报
回复
感觉看了楼上的方法,总是搞不成啊

110,545

社区成员

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

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

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