I have code that creates a row of 5 pictureboxes at runtime. I have added (I think) the code to add a click event handler to each picturebox as it is created.
int xPos = 95;
for (int index = 0; index < 5; index++)
{
keepImage[index] = new PictureBox();
keepImage[index].Width = 120;
keepImage[index].Height = 41;
keepImage[index].Left = xPos;
keepImage[index].Top = 360;
keepImage[index].Click += new EventHandler(keepImage_Click);
keepImage[index].BackColor = Color.Transparent;
keepImage[index].SizeMode = PictureBoxSizeMode.CenterImage;
this.Controls.Add(keepImage[index]);
xPos += 125;
}
The code works - it creates and displays the pictureboxes. I have been looking on here to find out how to find which one of the pictureboxes is clicked on...
public void keepImage_Click(object sender, EventArgs e)
{
PictureBox index = sender as PictureBox;
// identify which button was clicked and perform necessary actions
Debug.Write(index);
}
This code was taken from a solution found on here, but how do I adapt it for my needs? I have tried but so far, no luck.
At runtime the debug shows System.Windows.Forms.PictureBox, SizeMode: CenterImage but not the actual index.
Thanks for any suggestions.
EDIT
After trying one of the solutions mentioned in the comments, I now get the following error...
You can follow second approach from Get the index of array of picturebox clicked. But there is a typo issue with this answer it should be (sender as PictureBox).
So in your case, you can use Control.Tag property to store index of pictures as:
int xPos = 95;
for (int index = 0; index < 5; index++)
{
//Other codes
keepImage[index].Tag = index; //Set tag from index
this.Controls.Add(keepImage[index]);
xPos += 125;
}
Then click event would be like:
public void keepImage_Click(object sender, EventArgs e)
{
int index = int.Parse((sender as PictureBox).Tag.ToString());
Debug.Write(index);
}
Related
im making a project for university, for which i use windows forms. i have to create a program, where you can generate an several amount of textboxes and calculate all values. the calcutation is about the sum and average of every value combined. therefore i have two buttons, one to create the textboxes and another one to do the calculation (doTheMath_Click).
heres my code so far:
button for the calculation:
private void doTheMath_Click(object sender, EventArgs e)
{
int radius = int.Parse(numberofnumbers.Text)
int sum = 0;
for (int i = 0; i < zahlen.Length; i++)
{
sum += numbers[i];
}
double average = (double)sum / (double)radius;
total.Text = sum.ToString();
averagee.Text = average.ToString();;
}
int newtextboxn = 8;
int alingment = 200;
public TextBox addnewtextbox()
{
TextBox textbox = new TextBox();
this.Controls.Add(textbox);
if (newtextboxn % 18 == 0)
{
alingment += 200;
newtextboxn = 8;
textbox.Top = newtextboxn * 27;
textbox.Left = alingment;
}
else
{
textbox.Top = newtextboxn * 27;
textbox.Left = alingment;
}
newtextboxn = newtextboxn + 1;
return textbox;
}
button to print the textbox:
public void printTextbox(int radius)
{
for (int i = 0; i < radius; i++)
{
addnewtextbox();
}
}
private void printTheBox_Click(object sender, EventArgs e)
{
int radius = int.Parse(numberofnumbers.Text);
printTextboxandLabels(radius);
}
I had the idea to save the values of the textboxes in an array, but i dont know if it would work, because the array length should also be dynamically and i also dont know, how to initialize the array in the end. My other idea was to save the values in a list, but theres the same problem about the initialization.
i hope, that my problem is understandable and that you can help me.
i already surfed around stackoverflow, but i didnt found an idea to solve my problem.
thx
Just create a List where you store the created set of textboxes. Then you can use the code to loop over this list and retrieve the content of each textbox.
List<TextBox> textboxslist = new List<TextBox>();
and inside addnewtextbox() method fill textboxlist using The Add method.
textboxlist.Add(textbox);
then inside loop code in doTheMath_Click button you can access the values of each textbox by using Text Property.
for (int i = 0; i < textboxlist.Count; i++) {
int textboxvalue = int.Parse(textboxlist[i].Text);
sum += textboxvalue;
}
I've filled grid with Images in code and I'm tryig to add mouse events to them
public FilesWindow()
{
InitializeComponent();
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Image image = new Image();
image.MouseRightButtonDown += new MouseButtonEventHandler(setPicture);
myGrid.Children.Add(image);
image.Source = new BitmapImage(randomPic());
Grid.SetRow(image, i);
Grid.SetColumn(image, j);
}
}
}
private void setPicture(object sender, MouseButtonEventArgs e)
{
}
but I don't know how to access this specyfic Image (the one I click). Clicking itself (ex. with writing on console) is working but how to change picture in a cell that was clicked?
You can cast sender (which is type object) to Image:
Image image = (Image)sender;
I am working on windows form application in c#.
Firstly i created a table layout on run time and then inserted picture boxes after reading from database. Now i want to write sql query on every tablepanel cell/picturebox click.
Firstly I tried this by inserting picture box by drag and drop and i wrote query in picturebox_onclick function. But now i am creating picture box dynamically on run time.Now how i can handle onclick function of picture boxes that i created on run time.
private void GenerateTable(int columnCount, int rowCount)
{
//Clear out the existing controls, we are generating a new table layout
tableLayoutPanel1.Controls.Clear();
//Clear out the existing row and column styles
tableLayoutPanel1.ColumnStyles.Clear();
tableLayoutPanel1.RowStyles.Clear();
//Now we will generate the table, setting up the row and column counts first
tableLayoutPanel1.ColumnCount = columnCount;
tableLayoutPanel1.RowCount = rowCount;
for (int x = 0; x < columnCount; x++)
{
//First add a column
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
for (int y = 0; y < rowCount; y++)
{
//Next, add a row. Only do this when once, when creating the first column
if (x == 0)
{
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
}
//Create the control, in this case we will add a button
picturebox cmd = new picturebox();
cmd.image = new bitmap("c:\\images\\abc.png");
tableLayoutPanel1.Controls.Add(cmd, x, y);
}
}
}
Please suggest me simple way as i am new in development. Thanks in advance.
This should do it for you.
PictureBox cmd = new PictureBox();
cmd.Click += cmd_Click;
Then have a new function to handle the click.
void cmd_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
To perform unique actions depending on what PictureBox was clicked you may want to give the PictureBox a name you can relate to such as a row number or unique ID of the row.
PictureBox cmd = new PictureBox();
cmd.Click += cmd_Click;
cmd.Name = y.ToString();
void cmd_Click(object sender, EventArgs e)
{
string RowID = ((PictureBox)sender).Name;
}
I try to add check box for loop that when i enter 3 for example in textbox and click the button it automatically add 3 check boxes in the form
i tried this code but only add one check box
private void button1_Click(object sender, EventArgs e)
{
int x = Convert.ToInt32(textBox1.Text);
int m = 1;
for (int i = 0; i < x; i++)
{
CheckBox button2 = new System.Windows.Forms.CheckBox();
button2.Location = new System.Drawing.Point(5, m);
button2.Name = "button2 "+ m.ToString();
button2.Size = new System.Drawing.Size(51, 23);
button2.TabIndex = m;
//button2.UseVisualStyleBackColor = true;
this.Controls.Add(button2);
m++;
}
}
You are setting the location of all three buttons to nearly the same place so they are displayed on top of each other. Try spacing them out a little more.
For example change m++; to m += 40;.
You need to space your buttons out a little. Also, you should give each of your buttons a unique ID.
button2.ID = "Button_" + i;
I have problem with my C# WinForms project. I have a function that should change the place of buttons if they touch each other. For example, if I have btn1 at oldloction = (4,2) and btn2 at oldlocaction (2,6), then if I will move the buttons and they will touch bt1 new location = (2,6) and bt2 new location = (4,2)
now i did that with 2 buttons and it works.
locationx - means the x location on the button and its orgenize firat place of the location feat to the first buttons[0], the second feat to locationx[1] = buttons[1].location.x;
location - works the same ass locationx but uts the y locaion.
private void myText_MouseUp(object sender, MouseEventArgs e)
{
Point oldlocation = new Point(locationx[0], locationy[0]);
Point oldlocation2 = new Point(locationx[1], locationy[1]);
if (buttons[0].Location.Y == buttons[1].Location.Y)
{
buttons[1].Location = oldlocation;
buttons[0].Location = oldlocation2;
}
}
When I tried to make that as a global function it doesn't work and I don't know why.
This is the code of the global function that doesn't work:
private void myText_MouseUp(object sender, MouseEventArgs e)
{
for (int i = 0; i < counter; i++)
{
Point oldlocation = new Point(locationx[i], locationy[i]);
for (int j = 0; j < counter; j++)
{
if (i != j)
{
Point oldlocation2 = new Point(locationx[j], locationy[j]);
if (buttons[i].Location.Y != buttons[j].Location.Y)
{
buttons[j].Location = oldlocation2;
buttons[i].Location = oldlocation;
}
else if (buttons[i].Location.Y == buttons[j].Location.Y)
{
buttons[j].Location = oldlocation;
buttons[i].Location = oldlocation2;
}
}
}
}
}
Try using the button event for when it is pressed to call the function, rather than creating your own.
If the second function is not part of the control or form containing the buttons, it won't have a way to access the buttons array or locationx and locationy. You may need to pass these values as arguments to your function or ensure that they are provided as members of the class containing the second function. Note that generally a utility function would not take in "sender" and "MouseEventArgs" - pass only the specific data that the utility function needs to do its job.