from the code below I first get the items from MySql database and insert it into a programmatically created checkbox, then loop the checkbox based on the total amount of items in the database, after creating all the checkbox based on the items in the database, an onclick eventhandler is created for all the checkboxes.
I tried clicking on the checkbox, it is only showing me the last created checkbox with the last item in the database.
What I want is, when the checkbox is displayed on the window I want to be able to click on a specific checkbox and then get the content of that checkbox I clicked on. Thanks for the help.
List<CheckBox> allAutoCreatedObjects = new List<CheckBox>();
int currentX = 0;
const int STEP = 4;
string itemstoDisplay;
public string[] itemstoDisplaya;
string nam;
public void geItems()
{
MySqlConnection connection = new MySqlConnection("server=localhost;database=eyosms;uid=root;pwd=;");
MySqlCommand command;
var Query = "SELECT * FROM items";
command = new MySqlCommand(Query, connection);
try
{
connection.Open();
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
itemstoDisplay += readerc["Item_Name"].ToString() + ",";
}
itemstoDisplaya = itemstoDisplay.Split(',');
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//creating object for student
private void createObject()
{
getItems();
for (int i = 0; i <= itemstoDisplaya.Length-1; i++)
{
CheckBox CheckBoxa = new CheckBox();
CheckBoxa.Foreground = Brushes.Black;
CheckBoxa.Name = itemstoDisplaya[i];
CheckBoxa.Content = itemstoDisplaya[i];
allAutoCreatedObjects.Add(CheckBoxa);
CheckBoxa.Click += new RoutedEventHandler(CheckBoxstudentClick);
nam = CheckBoxa.Name;
}
}
//displaying the objects on a canvas
private void autoShowBtn()
{
foreach (var item in allAutoCreatedObjects)
{
Canvas.SetLeft(item, STEP * currentX);
currentX += 25;
MainCanvas.Width = currentX * STEP;
MainCanvas.Children.Add(item);
}
}
//onclick event for the object
private void CheckBoxstudentClick(object sender, EventArgs e)
{
MessageBox.Show(""+ nam);
}
Aucun commentaire:
Enregistrer un commentaire