I need help I have a set PictureBox (40), and I need to select these pictureboxes with arrows. I mean when I'm on the first picture and press right arrow key (border changing - selected), the border of the first should switch to none and next one switch to border FixedSingle.
One idea is:
if (keyData == Keys.Right) {
if (PictureBox1.BorderStyle == BorderStyle.FixedSingle) {
PictureBox1.BorderStyle = BorderStyle.None;
PictureBox2.BorderStyle = BorderStyle.FixedSingle;
} else if (PictureBox2.BorderStyle == BorderStyle.FixedSingle) {
pictu.....
}
}
but this method takes too much time so I'm looking for a simpler method.
Can somebody help me find a simpler way to do this?
EDIT new code:
namespace testPics
{ public partial class Form1 : Form {
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown_1(object sender, KeyEventArgs e)
{
changePictureBox(e.KeyData);
}
List<PictureBox> myPictureBoxes;
int index;
public void iniPictureBoxes()
{
myPictureBoxes = new List<PictureBox>();
myPictureBoxes.Add(pictureBox1);
myPictureBoxes.Add(pictureBox2);
myPictureBoxes.Add(pictureBox3);
index = 0;
}
public void changePictureBox(Keys keyData)
{
myPictureBoxes[index].BorderStyle = BorderStyle.None;
if (keyData == Keys.Right)
{
if (index < myPictureBoxes.Count - 1)
index++;
}
else if (keyData == Keys.Left)
{
if (index > 0)
index--;
}
myPictureBoxes[index].BorderStyle = BorderStyle.FixedSingle;
}}}
You could create a list of pictureboxes. Then for example you can add an indexer (just to keep it simple). The indexer is an int (or in your case can be a byte) that stores the index of the currently selected picturebox. If the user presses "right arrow" key just change the border of the current indexed picturebox increment the indexer and update "the now indexd picturebox".
List<PictureBox> myPictureBoxes;
int index;
public void iniPictureBoxes()
{
myPictureBoxes = new List<PictureBox>();
myPictureBoxes.Add(pictureBox1);
myPictureBoxes.Add(pictureBox2);
index = 0;
}
public void changePictureBox(Keys keyData)
{
myPictureBoxes[index].BorderStyle = BorderStyle.None;
if(keyData == Keys.Right)
{
if(index < myPictureBoxes.Count - 1)
index++;
}
else if(keyData == Keys.Left)
{
if(index>0)
index--;
}
myPictureBoxes[index].BorderStyle = BorderStyle.FixedSingle;
}
this just sets the borderstyle. If you want to really select the picturebox you also need to implement it (picturebox.select();
)
It may be better to create the pictureboxes generically. So you don't have to do add all of them manually to the List.
Here is the generic code for adding pictureboxes (in this case 5):
public void iniPictureBoxes()
{
myPictureBoxes = new List<PictureBox>();
for (int i = 0; i < 5; i++)
{
PictureBox tempPB = new PictureBox();
tempPB.Location = new Point(i * 15, 10);
tempPB.Size = new Size(10, 10);
tempPB.BackColor = Color.Black;
Controls.Add(tempPB);
myPictureBoxes.Add(tempPB);
}
index = 0;
}
and here the way to add an event: just double click the event u want to have.
Then a method is being auto generated for you. And you should change it to
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
changePictureBox(e.KeyData);
}