动态按钮自动排列 [英] Dynamic Button automatic arrangement

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

问题描述

朋友,

我是C#编程的初学者.

我想动态绘制按钮并使用Windows Forms Application自动排列它.

例如:

我有6个按钮,我在客户端计算机上仅显示4个按钮,而在另一台客户端计算机上仅显示5个按钮.

我如何处理这种情况.

请帮帮我.

在此先感谢.

Hi Friends,

I am beginner in C# programming.

I want to draw button dynamically and arrange it automatically using Windows Forms Application.

For example:

I have 6 button and i show only 4 button in my client machine, and 5 button for another client machine.

How i can manage this type of situation.

Please help me.

Thanks in Advance.

推荐答案

确定.您有一些数据,它可能会改变.填充它.做这样的事情:

Sure. You have some data, it can change. Populate it. Do something like this:

Button button = new Button();

//...

button.Location = //.. from data
button.Text = //.. from data, calculate parent size, arrangement
parent.Controls.Add(button); // some parent Control; at this moment button appears on screen
button.Click += (sender, eventArgs) => { /* whatever you need on click... */ }

//...



无论是否显示UI,您都可以动态地进行所有操作.

这只是个主意,因为您没有标记要使用的UI库.反正看起来很像.

—SA



All this you can do dynamically, when UI is already shown or not.

This is just the idea, because you did not tag what UI library you want to use. It would look pretty much like that anyway.

—SA


这可能对您有用...
This may useful to you...
//here we assum that there is 5 element... you have to create an array with number of button
string[] ButtonText = {"FirstValue", "SecondValue", "ThirdValue", "FourthValue", "FifthValue" };
//after creating an array of ButtonName call the below function from where you want to generate your dynamic buttons...
CreateButtonDynamic(5,ButtonText);//call of function

//function to create dynamic buttons
private void CreateButtonDynamic(int numberOfButton, string[] ButtonText)
{
   for (int i = 0; i < numberOfButton; i++)
   {
      Button temp = new Button();
      temp.Name = "button"+(i+1).ToString();
      temp.Text = ButtonText[i];
      temp.AutoSize = true;
      temp.Location = new System.Drawing.Point(100, 100 + (25 * i));//please adjust location as per your need
      temp.Tag = i;
      temp.Click += new EventHandler(OnButtonClick); 
      this.Controls.Add(temp);//this will add the control to form if you have panel or some other container then you have to specify the name of that container like...
      //this.panel1.Controls.Add(temp);
   }
}
private void OnButtonClick(object sender, EventArgs e)
{
   Button temp = (Button)sender;
   MessageBox.Show("You have clicked on  : " + temp.Text);
}


对于Windows应用程序
只需指定此

For windows application
just specify this

this.demotextbox.Location = new System.Drawing.Point(139, 127);





创建所有按钮并为用户检查哪个按钮,然后相应地使按钮可见
并在Web应用程序的情况下在运行时创建一个表并将控件添加到其中
单击此处
并对此有更多了解



or

create all the buttons and check which for the user and then make the button visible accordingly
and in case of web application create a table at run time and add the controls to it
Click Here
and know more about it


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

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