Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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);
Posted
Updated 25-Jul-17 22:45pm
Comments
Member 9983063 19-Jun-17 13:55pm    
well i just want to solution of this question after this i will never use concatenation please bro help me how can i solve this issue

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
 
Share this answer
 
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)

C#
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;
}
 
Share this answer
 
v2
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900