How to automatically create pictureboxes? - c#

I would like to automatically create a PictureBox. How to change this in the code:
private void button1_Click(object sender, EventArgs e)
{
PictureBox[] box = new PictureBox[textBox1.Text.Length];
for(int j=0;j<textBox1.Text.Length;j++)
box[0] = pictureBox1;
box[1] = pictureBox2;
box[2] = pictureBox3;
for (int i = 0; i < textBox1.Text.Length; ++i)
box[i].Image = Image.FromFile(string.Format(#"c:\obrazki\{0}.jpg",textBox1.Text[i]));
}

You should probably use a FlowLayoutPanel control to hold your PictureBox controls. Then the code would look something like this:
void button1_Click(object sender, EventArgs e) {
while (flowLayoutPanel1.Controls.Count > 0) {
flowLayoutPanel1.Controls[0].Dispose();
}
for (int i = 0; i < textBox1.Text.Length; ++i) {
PictureBox pb = new PictureBox();
pb.Image = Image.FromFile(string.Format(#"c:\obrazki\{0}.jpg",textBox1.Text[i]));
flowLayoutPanel1.Controls.Add(pb);
}
}

Related

How to change properties just one button

Hey,
I have multiple buttons.I want to reduce the code so that I can set the properties of a button, but only the one I click on will change.So that I don't click on button_1 and change all the others
public static void SetProp()
{
for (int i = 0; i < buttons.Count; i++)
{
buttons[i].Image = Properties.Resources.test;
buttons[i].Width = buttons[i].Image.Width;
buttons[i].Height = buttons[i].Image.Height;
buttons[i].ImageAlign = ContentAlignment.MiddleCenter;
buttons[i].Text = null;
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(0, 0, buttons[i].Width, buttons[i].Height);
buttons[i].Region = new Region(gp);
gp.Dispose();
}
}
private void button1_Click(object sender, EventArgs e)
{
SetProp();
}
public static void SetProp(Button button)
{
for (int i = 0; i < buttons.Count; i++)
{
button.Image = Properties.Resources.test;
button.Width = buttons[i].Image.Width;
button.Height = buttons[i].Image.Height;
button.ImageAlign = ContentAlignment.MiddleCenter;
button.Text = null;
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(0, 0, button.Width, button.Height);
button.Region = new Region(gp);
gp.Dispose();
}
}
private void button1_Click(object sender, EventArgs e)
{
SetProp(sender as Button);
}

c# Picturebox Array Selection

I have created an array of picture boxes and an event for when one is clicked.
public void TicTac_Load(object sender, EventArgs e)
{
PictureBox[] PBox = new PictureBox[9];
PBox[0] = this.pictureBox1;
PBox[1] = this.pictureBox2;
PBox[2] = this.pictureBox3;
PBox[3] = this.pictureBox4;
PBox[4] = this.pictureBox5;
PBox[5] = this.pictureBox6;
PBox[6] = this.pictureBox7;
PBox[7] = this.pictureBox8;
PBox[8] = this.pictureBox9;
for (int i = 0; i < 9; i++)
{
PBox[i].Click += new System.EventHandler(PBoxes_Click);
}
}
public void PBoxes_Click(object sender, EventArgs e)
{
PictureBox myPictureBox = sender as PictureBox;
//if(Pbox[1].click){
//^^ Looking for something like this
}
My question is how can I tell which one of my pictureboxes has been clicked as i am unable to access any of them. I would just like to be able to tell which has been clicked inside the method instead of creating many.
pictureBox1_Click(object sender, EventArgs e)
Like Events
There a multiple ways to solve the issue.
You could cast sender to the correct type (here PictureBox):
public void TicTac_Load(object sender, EventArgs e)
{
PictureBox[] PBox = new PictureBox[9];
PBox[0] = this.pictureBox1;
PBox[1] = this.pictureBox2;
PBox[2] = this.pictureBox3;
PBox[3] = this.pictureBox4;
PBox[4] = this.pictureBox5;
PBox[5] = this.pictureBox6;
PBox[6] = this.pictureBox7;
PBox[7] = this.pictureBox8;
PBox[8] = this.pictureBox9;
for (int i = 0; i < 9; i++)
{
PBox[i].Click += new System.EventHandler(PBoxes_Click);
}
}
public void PBoxes_Click(object sender, EventArgs e)
{
PictureBox myPictureBox = sender as PictureBox;
}
Alternatively (less-recommended), you could move PBox to a class-level array:
PictureBox[] PBox = new PictureBox[9];
public void TicTac_Load(object sender, EventArgs e)
{
PBox[0] = this.pictureBox1;
PBox[1] = this.pictureBox2;
PBox[2] = this.pictureBox3;
PBox[3] = this.pictureBox4;
PBox[4] = this.pictureBox5;
PBox[5] = this.pictureBox6;
PBox[6] = this.pictureBox7;
PBox[7] = this.pictureBox8;
PBox[8] = this.pictureBox9;
for (int i = 0; i < 9; i++)
{
PBox[i].Click += new System.EventHandler(PBoxes_Click);
}
}
public void PBoxes_Click(object sender, EventArgs e)
{
PictureBox myPictureBox = PBox[PBox.indexOf(sender)];
}

Error using massive of panels: Object reference not set to an instance of an object

I want to try using several panels. But there is the error. When I click button_1, it should show one of the panel and hide another. I initialize my panels in Form_Load. Here is my code:
public partial class Form1 : Form
{
public Panel[] p = new Panel[2];
public int iter = 1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (iter%2 == 1)
{
p[0].Visible = true;
p[1].Visible = false;
}
else if (iter%2 == 0)
{
p[0].Visible = false;
p[1].Visible = true;
}
iter++;
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 2; i++)
{
p[i] = new Panel();
p[i].Visible = false;
p[i].Size = new Size(200, 100);
p[i].Location = new Point(41, 103);
}
p[0].BackColor = System.Drawing.Color.Red;
p[1].BackColor = System.Drawing.Color.Blue;
}
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 2; i++)
{
p[i] = new Panel();
p[i].Visible = false;
p[i].Size = new Size(200, 100);
p[i].Location = new Point(41, 103);
this.Controls.Add(p[i]); // You need this line to add panel to form
}
p[0].BackColor = System.Drawing.Color.Red;
p[1].BackColor = System.Drawing.Color.Blue;
}

Get the index of array of picturebox clicked

I am creating some picturebox dynamically and click event for picturebox as follows
Image myImage = Image.FromFile("image/Untitled6.png");
PictureBox[] txtTeamNames = new PictureBox[5];
for (int i = 0; i < txtTeamNames.Length; i++)
{
var txt = new PictureBox();
txtTeamNames[i] = txt;
txtTeamNames[i].Image = myImage;
txtTeamNames[i].Height = 53;
txtTeamNames[i].Width = 48;
this.panel1.Controls.Add(txtTeamNames[i]);
txtTeamNames[i].Visible = true;
txtTeamNames[i].Click += new EventHandler(this.clcikeventhandle);
}
When someone clicks on any picture box, how do I find its array index and name?
void clickEventHandler(object sender, EventArgs e)
{
//???
}
You can access the PictureBox via the sender argument. So try this:
PictureBox[] txtTeamNames;
void YourMethod()
{
Image myImage = Image.FromFile("image/Untitled6.png");
txtTeamNames = new PictureBox[5];
//The same as your code
}
void clcikeventhandle(object sender, EventArgs e)
{
int index = txtTeamNames.IndexOf(sender As PictureBox);
}
EDIT: Approach #2
But if you are not happy with declaring that array in the class scope you can try this approach:
//Same as your code
for (int i = 0; i < txtTeamNames.Length; i++)
{
//Save as your code
txtTeamNames[i].Tag = i; // ADD THIS LINE
}
Then:
void clcikeventhandle(object sender, EventArgs e)
{
int index = int.Parse((sender as PictureBox).Tag.ToString());
}
Another suggestion - create a custom class, which inherits from PictureBox. It will have an extra Index property. And you can set it between these two lines:
txtTeamNames[i].Visible = true;
//assign the index here
txtTeamNames[i].Click += new EventHandler(this.clcikeventhandle);
like so:
txtTeamNames[i].Index = i;
Then in the handler:
void clickEventHandle(object sender, EventArgs e)
{
PictureBox pbox = sender As PictureBox;
int index = pbox.Index();
string name = pbox.Name();
}
You keep the same scope of variables, which may be useful if you are concerned about it. If you are okay with upgrading scope of txtTeamNames to class level, see another answer by Hossein Narimani Rad.
namespace your_name_project
{
public partial class Form_Begin : Form
{
PictureBox[] pictureBoxs = new PictureBox[6];
public Form_Begin()
{
InitializeComponent();
pictureBoxs[0] = pictureBox1; pictureBoxs[1] = pictureBox2; pictureBoxs[2] = pictureBox3;
pictureBoxs[3] = pictureBox4; pictureBoxs[4] = pictureBox5; pictureBoxs[5] = pictureBox6;
}
//continue
List<PictureBox> pictureBoxes = new List<PictureBox>();
private void buttonX1_Click(object sender, EventArgs e)
{
for (int i = 0; i <3; i++)
{
pictureBoxs[i].Image =your_name_project.Properties.Resources.image_1;// load image1 and Image_2from resource in property of picturebox
}
for (int i = 3; i < 6; i++)
{
pictureBoxs[i].Image = your_name_project.Properties.Resources.Image_2;
}
}
}
}
I've tried the 1st approach cited above, but it did not work for me. I needed to change the syntax in order to work. I am unsure why, but my way for approach 1 to work would be:
PictureBox[] txtTeamNames;
void YourMethod()
{
Image myImage = Image.FromFile("image/Untitled6.png");
txtTeamNames = new PictureBox[5];
//The same as your code
}
void clcikeventhandle(object sender, EventArgs e)
{
int index = Array.IndexOf(txtTeamNames, sender); //DIFFERENT HERE
}
If anyone has an idea...

Change SelectedTab of TabControl on MouseOver

I have a Windows Forms project with a TabControl.
Does anyone know how to change the SelectedTab when you hover over it with the pointer?
You can use TabControl's MouseMove event to detect whether your mouse is present on any tab and then can select it:
private void tabControl1_MouseMove(object sender, MouseEventArgs e)
{
Rectangle mouseRect = new Rectangle(e.X, e.Y, 1, 1);
for (int i = 0; i < tabControl1.TabCount; i++)
{
if (tabControl1.GetTabRect(i).IntersectsWith(mouseRect))
{
tabControl1.SelectedIndex = i;
break;
}
}
}
Try this:
private void tabControl1_MouseMove(object sender, MouseEventArgs e)
{
for (int i = 0; i < tabControl1.TabCount; i++)
{
if (tabControl1.GetTabRect(i).Contains(e.X, e.Y))
{
tabControl1.SelectedIndex = i;
break;
}
}
}

Categories