I have a problem adding picture boxes using Controls class on another picture box that already exists.
when i load the form i don't see any changes because the "coded" pictures are under the main picture (Arena)
Here is my code:
void drawSpikes()
{
PictureBox[] spikes = new PictureBox[Arena.Height / 25 * 2];
int position = 0;
byte wall = 1;
byte spike_count = 0;
for (int i = 0; i < Arena.Height / 25 * 2; i++)
{
spikes[i] = new PictureBox();
}
foreach (var Spike in spikes)
{
if (spike_count == 18) wall = 2;
Spike.Size = new Size(25, 25);
if (wall == 1)
{
Spike.Location = new Point(21, position);
Spike.BackColor = Color.Yellow;
}
if (wall == 2)
{
Spike.Location = new Point(position, 250);
Spike.BackColor = Color.Yellow;
}
if (position == 450) position = 0;
position += 25;
spike_count += 1;
Controls.Add(Spike);
}
}
How can i fix it ?
sorry for the function disconnection (i am new in stack overflow).
You can use the BringToFront() function after adding the picture box to the controls collection:
Controls.Add(Spike);
Spike.BringToFront();
Related
By using ColorBand tool I am scoping the axis to chart, so separate chart area is assigned to that axis and signal, but when cursor is enabled then cursor is not visible for whole chart(refer attached image) and axis titles are overlapped with each other(refer attached image).
Dictionary<int, AxisScope> list = new Dictionary<int, AxisScope>();
foreach (AxisScope axis in this.Chart.Axes.Custom)
{
axis.Visible = axis.Scope;
totalWeight += axis.Weight;
while (list.Keys.Contains(axis.Ordinal))
axis.ordinal++;
list.Add(axis.Ordinal, axis);
}
int ord = 0;
double start = 0;
int pos = 0;
int[] array = list.Keys.ToArray();
Array.Sort(array);
foreach (int i in array)
{
AxisScope scope = list[i];
scope.Ordinal = ord++;
if (scope.Scope && scope.Weight > 0)
{
if (scope.AxisColorBackground == null)
scope.AxisColorBackground = new ColorBand(this.Chart);
this.Chart.Tools.Add(scope.AxisColorBackground);
scope.AxisColorBackground.Axis = scope;
Color pen = Color.DarkRed;
Color back = Color.FromArgb(253, 253, 233);
if ((pos++ % 2) == 0)
{
pen = Color.DarkBlue;
back = Color.FromArgb(233, 253, 253);
}
scope.StartPosition = start;
start += (scope.Weight / totalWeight) * 100;
scope.EndPosition = start;
scope.AxisPen.Color = pen;
scope.AxisColorBackground.Pen.Color = back;
scope.AxisColorBackground.Brush.Color = back;
scope.AxisColorBackground.Brush.Transparency = 33;
scope.AxisColorBackground.Transparency = 33;
scope.AxisColorBackground.Start = double.MinValue;// scope.Minimum;
scope.AxisColorBackground.End = double.MaxValue;// scope.Maximum;
scope.AxisColorBackground.ResizeEnd = false;
scope.AxisColorBackground.ResizeStart = false;
scope.AxisColorBackground.Tag = "Axis -" + scope.Title.ToString();
scope.AxisColorBackground.Active = true;
}
else if (scope.Scope && scope.Weight == 0)
{
scope.Visible = false;
}
}
Could you please produce a Minimal, Reproducible Example I could run here to immediately reproduce your problem? If you feel your code is too long to post here, maybe you could consider zipping up your Visual Studio project and posting it to Steema's upload page.
Before I generate buttons in my program, it's supposed to clear them using:
for (int i = 0; i < buttons.Length; i++)
this.Controls.Remove(buttons[i]);
However all the buttons from the previous generation remain. What might be causing this?
(Below is the entire function, numButton changes in other functions.)
int numButtons = 5;
Button[] buttons = new Button[10];
private void generate_buttons()
{
for (int i = 0; i < buttons.Length; i++)
{
this.Controls.Remove(buttons[i]);
}
for (int i = 0; i < numButtons; i++)
{
buttons[i] = new Button();
buttons[i].Name = "btn" + i.ToString();
buttons[i].Text = Convert.ToString(i + 1);
buttons[i].Size = new Size(40, 24);
int yOffset = 40;
int xOffset = 20 + i * 42;
buttons[i].Location = new Point(xOffset, yOffset);
buttons[i].BackColor = System.Drawing.SystemColors.Control;
buttons[i].Enabled = false;
buttons[i].Click += new EventHandler(this.clickMe);
buttons[i].Visible = true;
this.Height = yOffset + 104;
this.Width = xOffset + 75;
}
Controls.AddRange(buttons);
}
Although you're removing the buttons from the control collection, you're not removing it from your buttons array. Add
Array.Clear(buttons, 0, buttons.Length);
I've also modified the removal loop to explicitly dispose of any resources held by the button as shown here on MSDN.
for (int i = 0; i < buttons.Length; i++)
{
//you can change break to 'continue' if you remove buttons
//from the array randomly so that if it encounters a null
//it will carry on reading the rest of the array.
if (buttons[i] == null)
break;
//dispose any button resources and event handlers so no references remain
buttons[i].Click -= new EventHandler(this.clickMe);
this.Controls.Remove(buttons[i]);
buttons[i].Dispose();
}
Array.Clear(buttons, 0, buttons.Length);
//..
Look into using a generic list collection instead. This allows you to use the .Add() and .Remove()/.RemoveAt() methods to more easily add and remove elements.
Tutorial/examples: http://www.dotnetperls.com/list
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]);
}
}
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++;
}
}
}
my panel in my windows form application doesn't include all the buttons i asked it to. It shows only 1 button, Here is the code
private void AddAlphaButtons()
{
char alphaStart = Char.Parse("A");
char alphaEnd = Char.Parse("Z");
for (char i = alphaStart; i <= alphaEnd; i++)
{
string anchorLetter = i.ToString();
Button Buttonx = new Button();
Buttonx.Name = "button " + anchorLetter;
Buttonx.Text = anchorLetter;
Buttonx.BackColor = Color.DarkSlateBlue;
Buttonx.ForeColor = Color.GreenYellow;
Buttonx.Width = 30;
Buttonx.Height = 30;
this.panelButtons.Controls.Add(Buttonx);
//Buttonx.Click += new System.EventHandler(this.MyButton_Click);
}
}
Aren't they all going to be on the same position?
Try setting Buttonx.Location = new Point(100, 200);
(but with different points for different buttons)
You could use a FlowLayoutPanel, which would take care of the layout for you, or you need to track the locations yourself, or which could look something like this:
private void AddAlphaButtons()
{
char alphaStart = Char.Parse("A");
char alphaEnd = Char.Parse("Z");
int x = 0; // used for location info
int y = 0; // used for location info
for (char i = alphaStart; i <= alphaEnd; i++)
{
string anchorLetter = i.ToString();
Button Buttonx = new Button();
Buttonx.Name = "button " + anchorLetter;
Buttonx.Text = anchorLetter;
Buttonx.BackColor = Color.DarkSlateBlue;
Buttonx.ForeColor = Color.GreenYellow;
Buttonx.Width = 30;
Buttonx.Height = 30;
// set button location
Buttonx.Location = new Point(x, y);
x+=30;
if(x > panel1.Width - 30)
{
x = 30;
y+=30;
}
this.panelButtons.Controls.Add(Buttonx);
//Buttonx.Click += new System.EventHandler(this.MyButton_Click);
}
}