This may or may not compile... but it will give you the idea
Code:void InitX() { //SqlConnection MySqlConnection myConnection = new MySqlConnection(); myConnection.ConnectionString = "*************"; myConnection.Open(); string mySelectQuery = "SELECT * FROM table"; MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection); MySqlDataReader myReader; myReader = myCommand.ExecuteReader(); System.Drawing.Point butonPos = new System.Drawing.Point(0,0); int index = 0; while(myReader.Read()) { //using the data from the reader create a button Button button = new Button(); button.Text = dr.GetString(0);//or wherever the data is in your reader button.Tag = "put whatever data here, as an object, you need to pass to event"; button.Click+=new EventHandler(button_Click);// assign event this.Controls.Add(button); //add button to form button.Location = butonPos; //if you also want to create other info on the form, now is the time to do it //example Label lab = new Label(); lab.Text = "yo" + index++; this.Controls.Add(lab); //add button to form lab.Location = new Point(200, buttonPos.Y); butonPos.Y += button_Click.Height * 1.5;//set place for next button to create } } void button_Click(object sender, EventArgs e) { Button senderButton = (Button)sender; string buttonTagString = (string)senderButton.Tag; <--- this is how you can GetChildAtPointSkip data from the button //do something MessageBox.Show(buttonTagString); }




Reply With Quote