C# Picture Box Loop - c#

I have written loop for picture box, it looks like this:
void labelAdder()
{
List<Label> labels = new List<Label>();
List<TextBox> texboxex = new List<TextBox>();
List<PictureBox> pictureBoxes = new List<PictureBox>();
for (int i = 0; i < args.Length - 1; i++)
{
equals++;
var temp = new TextBox();
var temp2 = new PictureBox();
int x = 10;
int y = xD * equals;
temp.Location = new Point(x, y);
temp2.Location = new Point(x + 100, y);
temp2.Image = global::Xbox360_Complex_Checker.Properties.Resources.button_offline;
temp.Text = args[i];
temp2.Text = status[i];
this.Controls.Add(temp);
this.Controls.Add(temp2);
temp.Show();
temp2.Show();
texboxex.Add(temp);
pictureBoxes.Add(temp2);
}
}
My problem is that PictureBox is loading only next to first textBox, it should load next to all textBoxes. I don't know why it's not working. I tried this with labels, and labels were loading properly.

Related

How to make the element(resource) in the panel

This code of my button:
int offsetY = 5;
int x = 0;
int y = 0;
int index = 1;
//Start adds new panel and new label
Panel b = new Panel();
Label la = new Label();
//Adds panel properties
b.Controls.Add(la);
b.Location = new Point(x, y + offsetY);
b.Size = new Size(633, 119);
newhaven.Class1 cl = new newhaven.Class1();
b.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
flowLayoutPanel1.Controls.Add(b);
b.ResumeLayout(false);
//Adds label properties
x = 0;
y = -20;
la.Location = new Point(x,y);
// la.Size = new Size(60, 30);
la.Text = "Hello";
flowLayoutPanel1.Controls.Add(la);
I want to hello to be in the panel)
Are can you help me?
You almost got it right, but need to take care of the position co-ordinates for the label, please ensure that they lie inside the panel co-ordinates.
The following works:
int offsetY = 5;
int x = 0;
int y = 0;
int index = 1;
//Start adds new panel and new label
Panel b = new Panel();
Label la = new Label();
//Adds panel properties
b.Controls.Add(la);
b.Location = new Point(x, y + offsetY);
b.Size = new Size(633, 119);
//newhaven.Class1 cl = new newhaven.Class1();
b.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
flowLayoutPanel1.Controls.Add(b);
b.ResumeLayout(false);
// Adds label properties -
// commented the co-ordinates and using the same as that of panel
//x = 0;
//y = -20;
la.Location = new Point(x, y + offsetY);
// la.Size = new Size(60, 30);
la.Text = "Hello";
// no need to add the label separately, its inside the panel
//flowLayoutPanel1.Controls.Add(la);

dynamically adding controls under loop in C#

I am developing a windows application where I want to create some controls dynamically inside a loop.
The code I am trying is
private Label newLabel = new Label();
private int txtBoxStartPosition = 100;
private int txtBoxStartPositionV = 25;
for (int i = 0; i < 7; i++)
{
newLabel.Location = new System.Drawing.Point(txtBoxStartPosition, txtBoxStartPositionV);
newLabel.Size = new System.Drawing.Size(70, 40);
newLabel.Text = i.ToString();
panel1.Controls.Add(newLabel);
txtBoxStartPositionV += 30;
}
This code is generating only one Label with text 7 but I want to create 8 Lables with their respective texts, how can I do this?
In your loop you are essentially updating properties of the very same Label. If you want create a new one on each step, move creation of the object inside the loop:
private Label newLabel;
for (int i = 0; i < 7; i++)
{
newLabel = new Label();
...
By the way if you want 8 labels - your for should iterate 8 times, not 7, as it does now:
for (int i = 0; i < 8; i++)
Try this:
private int txtBoxStartPosition = 100;
private int txtBoxStartPositionV = 25;
for (int i = 0; i < 7; i++)
{
newLabel = new Label();
newLabel.Location = new System.Drawing.Point(txtBoxStartPosition, txtBoxStartPositionV);
newLabel.Size = new System.Drawing.Size(70, 40);
newLabel.Text = i.ToString();
panel1.Controls.Add(newLabel);
txtBoxStartPositionV += 30;
}
You need to put the line private Label newLabel = new Label(); in the for loop.
private int txtBoxStartPosition = 100;
private int txtBoxStartPositionV = 25;
for (int i = 0; i < 7; i++)
{
Label newLabel = new Label();
newLabel.Location = new System.Drawing.Point(txtBoxStartPosition, txtBoxStartPositionV);
newLabel.Size = new System.Drawing.Size(70, 40);
newLabel.Text = i.ToString();
panel1.Controls.Add(newLabel);
txtBoxStartPositionV += 30;
}

How to enable auto scroll?

I have a feeling that Im am missing something obvious but:
I have a single row of pictures in a form, in theory the pictures could go on forever. I need a scroll bar so that the user can look at all of the pictures in the row. I know I need to enable auto scroll but I have no idea how to enable it. Can someone tell me how to enable it or something that I am missing?
If it helps this is the code i am using to generate the pictures:
private void imagePalletToolStripMenuItem_Click(object sender, EventArgs e)
{
MyPalletGui.Show();
Dictionary<string,Bitmap> MyPallet = MyImageCollection.ToDictionary();
int xcor = -50;
int ycor = 0;
foreach (Bitmap curtImage in MyPallet.Values){
PictureBox myPicBox = new PictureBox();
xcor += 50;
myPicBox.Location = new Point(xcor, ycor);
myPicBox.Width = 50;
myPicBox.Height = 50;
myPicBox.Visible = true;
myPicBox.Image = new Bitmap(curtImage);
this.MyPalletGui.Controls.Add(myPicBox);
This code will do exactly what you want, it uses the Form as the ViewPort with AutoScroll:
public Form1()
{
InitializeComponent();
PopulatePictures();
}
private void PopulatePictures()
{
this.AutoScroll = true;
string[] list = Directory.GetFiles(#"C:\\Users\\Public\\Pictures\\Sample Pictures", "*.jpg");
PictureBox[] picturebox= new PictureBox[list.Length];
int y = 100;
for (int index = 0; index < picturebox.Length; index++)
{
picturebox[index] = new PictureBox();
this.Controls.Add(picturebox[index]);
picturebox[index].Location=new Point(index * 120, y);
if(x%12 == 0)
y = y + 150;
picturebox[index].Size = new Size(100,120);
picturebox[index].Image = Image.FromFile(list[index]);
}
}

Displaying an array of images in picturebox?

I'm very new to visual C# I want to display an array of images in a picture box
Here's my code:
string[] list = Directory.GetFiles(#"C:\\pictures", "*.jpg");
Image[] images = new Image[5];
for (int index = 0; index < 5; index++)
{
//HERE IS WHERE IM STUCKED WITH
picturebox[index] = Image.FromFile(list[index]);
}
Edit-1 : This answer has a scope limited to Win-Forms C#.
You need certain assemblies added in your application before using this code.
using System.IO;
using System.Windows.Forms;
Edit ended;
Original Answer
You have to draw all image to one image for displaying them in single picturebox
That is bit complex you can use mutiple pictureboxes
In following code they are created dynamically according to need:
// For confirm visibility of all images set
this.AutoScroll = true;
string[] list = Directory.GetFiles(#"C:\pictures", "*.jpg");
PictureBox[] picturebox= new PictureBox[list.Length];
int y = 0;
for (int index = 0; index < picturebox.Length; index++)
{
this.Controls.Add(picturebox[index]);
// Following three lines set the images(picture boxes) locations
if(index % 3 == 0)
y = y + 150; // 3 images per rows, first image will be at (20,150)
picturebox[index].Location=new Point(index * 120 + 20, y);
picturebox[index ].Size = new Size(100,120);
picturebox[index].Image = Image.FromFile(list[index]);
}
The answer provided throws an object reference exception. Otherwise thanks for the example!
for (int index = 0; index < picturebox.Length; index++)
{
this.Controls.Add(picturebox[index]);
// Following three lines set the images(picture boxes) locations
should be
for (int index = 0; index < picturebox.Length; index++)
{
picturebox[index] = new PictureBox();
this.Controls.Add(picturebox[index]);
// Following three lines set the images(picture boxes) locations
Use picturebox[index].Image = Image.FromFile(list[index]);
//this code help you to work with picturebox in arraye
public partial class Form_Begin : Form
{
PictureBox[] pictureBoxs = new PictureBox[50];
public Form_Begin()
{
InitializeComponent();
pictureBoxs[0] = pictureBox1;
pictureBoxs[1] = pictureBox2;
pictureBoxs[2] = pictureBox3;
pictureBoxs[3] = pictureBox4;}
List<PictureBox> pictureBoxes = new List<PictureBox>();
private void buttonX1_Click(object sender, EventArgs e)
{
for (int i = 0; i <2; i++)
{
pictureBoxs[i].Image =your_name_project.Properties.Resources.Image_1; // Load Image_1 from Resources on property of picturebox
}
for (int i = 2; i < 4; i++)
{
pictureBoxs[i].Image =your_name_project.Properties.Resources.Image_2; // Load Image_12 from Resources on property of picturebox
}
private void picbutton_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
PictureBox[] picture = new PictureBox[5];
int x = 0;
int y = 15;
for (int index = length; index < picture.Length; index++)
{
picture[index] = new PictureBox();
picture[index].Size = new Size(100, 50);
open.Title = "OPen Image";
open.Filter = "JPG Files (*.jpg)|*.jpg|JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|GIF Files (*.gif)|*.gif";
DialogResult result = open.ShowDialog();
if (result == DialogResult.OK)
{
picture[index].BackgroundImage = new Bitmap(open.FileName);
picture[index].SizeMode = PictureBoxSizeMode.AutoSize;
listBox1.Controls.Add(picture[index]);
if ((x % 3 == 0) && (index != 0))
{
y = y + 150; // 3 images per rows, first image will be at (20,150)
x = 0;
}
picture[index].Location = new Point(x * 210 + 20, y);
picture[index].Size = new Size(200, 150);
x++;
}
}
}

How Can I Add multipe buttons on c# dynamically using point class?

Hello I try to add multiple Buttons on a panel on form and next to each other but instead it put them above each other.
I am using the following function.
the Code:
private void CreatBtn()
{
Point[] p = new Point[6];
string log = "";
Form2 frm2 = new Form2();
Button[] btn = new Button[6];
for (int i = 0; i < btn.GetLength(0); i++)
{
btn[i] = new Button();
btn[i].Height = 65;
btn[i].Width = 80;
p[i] = new Point();
p[i].X = i * 83;
p[i].Y =0;
log +=p.ToString() +"\n";
btn[i].PointToClient(p[i]);
btn[i].Show();
}
panel1.Controls.AddRange(btn);
}
Add a Left value to your buttons
btn[i] = new Button();
btn[i].Height = 65;
btn[i].Width = 80;
btn[i].Left = i * 83; //Now they'll be next to each other.
You may also consider using the FlowLayoutPanel.
Use flowLayoutPanel in the panel of it'll but the next to each other until the button reach the end of it, then it will make an new raw
Point[] p = new Point[6];
string log = "";
Button[] btn = new Button[6];
for (int i = 0; i < btn.GetLength(0); i++)
{
btn[i] = new Button();
btn[i].Height = 65;
btn[i].Width = 80;
p[i] = new Point();
p[i].X = i * 83;
p[i].Y = 0;
log += p.ToString() + "\n";
btn[i].PointToClient(p[i]);
btn[i].Show();
}
FlowLayoutPanel pan = new FlowLayoutPanel();
pan.Width=500;//width of all buttons
pan.Height = 100;
pan.Controls.AddRange(btn);
panel1.Controls.Add(pan);

Categories

Resources