如何安排动态按钮 [英] How to arrange dynamic button

查看:62
本文介绍了如何安排动态按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys,我希望你会很好,我面临着一个逻辑问题,它需要像这样的按钮 in m [ ^ ]

在我的形式,但我不能这样做请帮助我我该怎么办

谢谢



我尝试过的:



按钮按钮=发送者为按钮;

string buttonText = button.Text;

MessageBox.Show(buttonText);

int CatId = OperationAccess.GetId(从tbl_Catagary中选择catID,其中name ='+ button.Text +');

列表按钮=新列表();

列表<字符串> All_Catagary = OperationAccess.GetName(SELECT name FROM tbl_product where catID =+ CatId +);

int repetition = 0;

for(int i = 0; i< All_Catagary.Count; i ++)

{

Button Productbtn = new Button();

Productbtn.Text = All_Catagary [i] .ToString();

string Command =SELECT [image] FROM tbl_product where name ='+ Productbtn.Text +';

string path = OperationAccess。 GetImage(Command);

string appPath = Path.GetDirectoryName(Application.ExecutablePath)+ @\ ProductImage \;

string TotalPath = appPath + path;

Productbtn.Image = System.Drawing.Image.FromFile(TotalPath);

Productbtn.Visible = true;

Productbtn.Location = new Point (3 +重复* 107,3);

Productbtn.Height = 54;

Productbtn.Width = 110;



Productbtn.BackColor = Color.FromArgb(40,40,40) ;

Productbtn.ForeColor = Color.White;

Productbtn.Top = 10;

Productbtn.Font = new Font(Lucida Console ,16);

buttons.Add(Productbtn);

重复++;

this.panel9.Controls.Add(Productbtn);

Productbtn.Click + = new EventHandler(Productbtn_Click);

Hello Guys,I hopeyou will be fine guys i have facing a logical problems that it
i want buttons like this in m[^]
in my form but i can't do it please help me how can i do it
Thanks

What I have tried:

Button button = sender as Button;
string buttonText = button.Text;
MessageBox.Show(buttonText);
int CatId = OperationAccess.GetId("SELECT catID from tbl_Catagary where name='" + button.Text + "'");
List buttons = new List();
List<string> All_Catagary = OperationAccess.GetName("SELECT name FROM tbl_product where catID=" + CatId + "");
int repetition = 0;
for (int i = 0; i < All_Catagary.Count; i++)
{
Button Productbtn = new Button();
Productbtn.Text = All_Catagary[i].ToString();
string Command = "SELECT [image] FROM tbl_product where name ='" + Productbtn.Text + "'";
string path = OperationAccess.GetImage(Command);
string appPath = Path.GetDirectoryName(Application.ExecutablePath) + @"\ProductImage\";
string TotalPath = appPath + path;
Productbtn.Image = System.Drawing.Image.FromFile(TotalPath);
Productbtn.Visible = true;
Productbtn.Location = new Point(3 + repetition * 107, 3);
Productbtn.Height = 54;
Productbtn.Width = 110;

Productbtn.BackColor = Color.FromArgb(40, 40, 40);
Productbtn.ForeColor = Color.White;
Productbtn.Top =10;
Productbtn.Font = new Font("Lucida Console", 16);
buttons.Add(Productbtn);
repetition++;
this.panel9.Controls.Add(Productbtn);
Productbtn.Click += new EventHandler(Productbtn_Click);

推荐答案

您的代码存在多个问题。 但也许最重要的是,您只是将创建的按钮添加到列表中,而不是表单。 而不是:

There are several issues with your code.  But perhaps most importantly, you're simply adding the created buttons to a list, and not the form.  Instead of doing:
buttons.Add(Productbtn);

尝试:

try:

this.Controls.Add(Productbtn);

/ ravi


我看到了几个问题。



1.像Ravi指出的那样你只需要将按钮添加到面板上。

2.你需要设置按钮的顶部和左侧属性,将按钮定位到你想要的屏幕上



当你将它们全部设置为10时,我会做类似下面的事情(诚然,我只是连续设置5但它应该给你的想法)



I see a couple of issues.

1. like Ravi pointed out you need to add the button to the panel only.
2. you need to set the top and left properties of the button to position the button to the screen where you want it

at the moment you are setting all of them to 10, I would do something similar to the below (admittedly I am only setting 5 in a row but it should give you an idea)

int leftPosition = 10;
for(int i =0; i< 5;i++)
{
  Button myButton = new Button();
  myButton.Name = "Button" + i.ToString();
  myButton.Text = i.ToString();
  myButton.width = 110;
  myButton.Height = 58;
  myButton.top = 10;
  myButton.Left = leftPosition;
  panel1.Controls.Add(myButton);

  leftPosition += 120;
}


我用这个链接解决了这个问题.net - 动态按钮创建&使用c#将它们置于预定义的顺序 - Stack Overflow [ ^ ]

谢谢大家的帮助。
Well i have solved this issue my self with this link .net - Dynamic button creation & placing them in a predefined order using c# - Stack Overflow[^]
Thank you guys for my help.


这篇关于如何安排动态按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆