如何使用单击事件和位置创建动态按钮

编程入门 行业动态 更新时间:2024-10-18 10:27:54
本文介绍了如何使用单击事件和位置创建动态按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我面临着位置分配和点击动态创建按钮事件的问题。 我有一个按钮数组,它将动态创建按钮,但我很困惑,根据水平和垂直的表格大小分配位置以及这些动态创建按钮的点击事件。 为了动态加载按钮,我这样写了。

Hi All, I am facing problems with location assigning and click events of dynamically created buttons. I have a button array which will create the no of buttons dynamically, but i am very confused to assigning the location according to the form size horizontally and vertically and the click event of these dynamically created buttons. For loading the buttons dynamically i wrote like this.

private void frmResturantTables_Load(object sender, EventArgs e) { createTables(50); // count from my database, for eg 50 }

其中createTables是我动态创建按钮的功能。

Where createTables is my function for creating buttons dynamically.

private void createTables(int length) { int j = 0; Button[] buttons = new Button[length]; for (int i = 0; i < buttons.Length; i++) { buttons[i] = new Button(); buttons[i].Size = new System.Drawing.Size(95, 75); buttons[i].Name = "Button" + i.ToString(); buttons[i].Text = "Table" + (i + 1).ToString(); buttons[i].Font = new System.Drawing.Font("Comic Sans MS", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); buttons[i].Image = global::Biz_Maxx.Properties.Resources.tab; buttons[i].TextAlign = System.Drawing.ContentAlignment.BottomCenter; buttons[i].UseVisualStyleBackColor = true; Point p = new Point(10 + i * 100, 10); buttons[i].Location = p; buttons[i].Click += new EventHandler(this.Button_Click); Controls.Add(buttons[i]); } }

这里我使用按钮点击事件来打开表格

Here i am using the button click event to open the forms

protected void Button_Click(object sender, EventArgs e) { ucSales sale= new ucSales(); }

现在我的实际问题是根据ucSales表格的按钮打开。即 如果我点击按钮[1],这意味着动态创建的第一个按钮,那么ucSales表格也像ucSale [1],对于按钮[2],ucSales [2]应该打开,就像我动态创建的每个按钮一样,应该单独打开ucSales表单。 还要检查表单是否已经打开,然后只显示我输入的数据那个特殊的形式。 我的第二个问题是在表格中动态显示这些按钮的分配,根据表格长度和宽度显示按钮。到第一排,第二排的形状大小..... 我正在开发一个餐厅软件,它有这么多餐厅的桌子在里面..根据我的数据库餐厅表计数,按钮应该在表格中动态显示。这样每个餐厅的桌子都有不同的名称和不同的计费屏幕。如果我点击表格中的任何一个餐厅餐桌按钮,它应该显示结算屏幕。 任何帮助表示赞赏 感谢advance

Now my actual problem is according to the buttons the ucSales forms to be opened. ie If i click button[1], that means the first button from the dynamically created, then the ucSales form is also be like ucSale[1] , for button[2], ucSales[2] should be opened, like that for each and every buttons i dynamically created should open the ucSales forms individually . Also want to check if the form is opened already, then only display it with the data i entered for that particular form. My second issue is in the allocation of these buttons dynamically displayed in the form, according to the form length and width.how to display the buttons according to the form size like first row,second row..... I am developing a restaurant software, which have so many restaurant tables in it.. according to my database restaurant tables count, the buttons should shown dynamically in the form. So that for each restaurant table having different names and different billing screens. If i click any one of the restaurant table button from the form, it should display the billing screen. Any help appreciated Thanks in advance

推荐答案

要绕过定位问题,您可以使用FlowLayoutPanel控件自动排列按钮。 关于点击事件处理你可以在按钮Tag属性(让我说一个ID或类似的东西)中放入something,它将在button_click事件处理程序中标识ucSale。 例如: To bypass the positioning problem you could use the FlowLayoutPanel control which will automatically arrange your buttons. Regarding the click event handling you could put "something" in the button Tag property (let me say an ID or something similar) that will identify the ucSale in the button_click event handler. For example: protected void Button_Click(object sender, EventArgs e) { Button btn = sender as Button; var ucSaleID = btn.Tag; //etc.... }

与你的第一个(第一个)问题有关,我可以给你一个包含50个元素的EventHandler数组。看看下面的代码: private EventHandler [] events = new EventHandler [50] {(object snd,EventArgs e) => {ucSale sale1 = new ucSale(); }, (对象snd,EventArgs e)=> {ucSale sale2 = new ucSale(); }, 。 。 。 ; //等等到定义的最大限制数量 } buttons [i]。点击+ = events [i]; ------------------------------------------- ------------------------------------------------- 现在,关于你的第二个问题,我们可以设计安排在水平位置排列的5个按钮,让它在两侧按垂直顺序排列10个按钮。 int btnWidth = 0,btnHeight = 0; if(Form1.Width%50 == 0) btnWidth = Form1.Width / 50; else btnWidth = int32.Parse(Form1.Width / 50); if(Form1.Height%10 == 0) btnHeight = Form1.Height / 10; 否则 btnHeight = int32.Parse(Form1.Height / 10); buttons [i] .Width = btnWidth;按钮[i] .Height = btnHeight; 我希望这会有所帮助,...谢谢 Pertaining to your first (1st) problem, i can give you an EventHandler's array with 50 elements in it. Look at the codes illustrated below: private EventHandler[] events = new EventHandler[50] { (object snd, EventArgs e) => { ucSale sale1 = new ucSale(); } , (object snd, EventArgs e) => { ucSale sale2 = new ucSale(); } , . . . ; // and so forth to the max limit number defined } buttons[i].Click += events[i]; -------------------------------------------------------------------------------------------- Now, pertaining to your second problem, we could design to arrange 5 buttons lined up in horizontal position and let it be left 10 buttons aligned in the vertical order on both side. int btnWidth = 0, btnHeight = 0; if( Form1.Width % 50 == 0 ) btnWidth = Form1.Width / 50; else btnWidth = int32.Parse( Form1.Width / 50 ); if( Form1.Height % 10 == 0 ) btnHeight = Form1.Height / 10; else btnHeight = int32.Parse( Form1.Height / 10 ); buttons[i].Width = btnWidth; buttons[i].Height = btnHeight; I hope that will help, .. Thank's

更多推荐

如何使用单击事件和位置创建动态按钮

本文发布于:2023-11-02 14:21:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552645.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   单击   按钮   位置   事件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!