单击C#清除私有空白 [英] Clear private void with click C#
本文介绍了单击C#清除私有空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码工作并创建自动按钮...
一切正常
我需要点击按钮上的清晰表格
一些帮助?
我尝试了什么:
private void artikli_buttoni()// pice
{
SqlConnection con = new SqlConnection (数据源=。\\SQLEXPRESS;初始目录= bss;集成安全性=真);
// SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand(SELECT ime +' - '+ cijena_sa_porezom,data FROM roba_usluge where grupa_artikala ='Piće',con);
var da = new SqlDataAdapter(cmd);
var ItemTable = new DataTable();
da.Fill(ItemTable);
con.Open();
Int32 count = ItemTable.Rows.Count;
con.Close();
int top = 70;
int left = 10;
for(int i = 1; i< = count; i ++)
{
Button button = new Button();
button.Size = new Size(128,128);
button.BackColor = Color.Transparent;
//butbut.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.BorderSize = 0;
button.Font = new System.Drawing.Font(Trebuchet MS,10);
button.TextAlign = ContentAlignment.TopCenter;
button.BackgroundImageLayout = ImageLayout.Zoom;
button.Left = left;
button.Top = top;
button.Text = ItemTable.Rows [i - 1] [0] .ToString();
if(ItemTable.Rows [i - 1] [1]!= null)
{
try
{
byte [] _byte =( byte [])ItemTable.Rows [i - 1] [1];
MemoryStream ms = new MemoryStream(_byte);
button.BackgroundImage = System.Drawing.Image.FromStream(ms); // bilo image
}
catch {}
}
button.Click + = new EventHandler(this.btn_Click);
this.Controls.Add(button);
if(i%5 == 0)
{
left = 10;
top + = button.Height + 2;
}
其他
{
left + = button.Width + 2;
}
}
}
解决方案
一个懒惰的(?)/实用解决方案将是在列表中注册动态创建的按钮。此列表稍后可用于删除它们。
由@ John-Simmons-outlaw-programmer提示约翰西蒙斯/非法程序员 - 专业档案 [ ^ ]:别忘了也为每个按钮解开事件处理程序
最后这个示意图伪代码可以帮助您实现您的请求
表格:
< pre lang =c#>列表按钮dynBtnList = new ...;
AddBtn:
所需的每个按钮:
Button btn = new 按钮...
btn.Click + = 此 .btn_Click;
dynBtnList.Add(btn)
this .Controls.Add(btn);
RemoveBtn:
for each(Button btn in dynBtnList;)
{
btn.Click- = this .btn_Click;
Controls.Remove(btn);
}
dynBtnList.Clear();
My code work and create automatcly buttons...
Everything working fine
I need clear form on button click
some help?
What I have tried:
private void artikli_buttoni() //pice { SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=bss;Integrated Security=True"); //SqlConnection con = new SqlConnection(cs); SqlCommand cmd = new SqlCommand("SELECT ime+'-'+cijena_sa_porezom, data FROM roba_usluge where grupa_artikala='Piće'", con); var da = new SqlDataAdapter(cmd); var ItemTable = new DataTable(); da.Fill(ItemTable); con.Open(); Int32 count = ItemTable.Rows.Count; con.Close(); int top = 70; int left = 10; for (int i = 1; i <= count; i++) { Button button = new Button(); button.Size = new Size(128, 128); button.BackColor = Color.Transparent; //button.FlatStyle = FlatStyle.Flat; button.FlatAppearance.BorderSize = 0; button.Font = new System.Drawing.Font("Trebuchet MS", 10); button.TextAlign = ContentAlignment.TopCenter; button.BackgroundImageLayout = ImageLayout.Zoom; button.Left = left; button.Top = top; button.Text = ItemTable.Rows[i - 1][0].ToString(); if (ItemTable.Rows[i - 1][1] != null) { try { byte[] _byte = (byte[])ItemTable.Rows[i - 1][1]; MemoryStream ms = new MemoryStream(_byte); button.BackgroundImage = System.Drawing.Image.FromStream(ms);//bilo image } catch { } } button.Click += new EventHandler(this.btn_Click); this.Controls.Add(button); if (i % 5 == 0) { left = 10; top += button.Height + 2; } else { left += button.Width + 2; } } }
解决方案
A maybe lazy(?)/pragmatic solution will be to Register the dynamic created Buttons in a list. This list can later be used to remove them.
Hint by @John-Simmons-outlaw-programmer John Simmons / outlaw programmer - Professional Profile[^]: "Don't forget to also unhook your event handler for each button"
Finally this schematic pseudo code should help you to implement your request
Form:
List Button dynBtnList= new...;
AddBtn:
for each button needed: Button btn= new Button... btn.Click+= this.btn_Click; dynBtnList.Add(btn) this.Controls.Add(btn);
RemoveBtn:
for each(Button btn in dynBtnList;) { btn.Click-= this.btn_Click; Controls.Remove(btn); } dynBtnList.Clear();
这篇关于单击C#清除私有空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文