Creating an array of pictureboxes in windows form (C#) - c#

I'm trying to create an array/list of picturebox objects that are declared and added to the form on button click (Meaning that I'm not creating multiple objects with my array, but plan to if I can get this to run). Not getting errors, but the pictureboxes themselves do not appear on the form.
private void spawn_Click(object sender, EventArgs e)
{
var pictureTest[0] = new PictureBox();
pictureTest[0].Image = Properties.Resources.testimage;
pictureTest[0].Location = new Point(500, 250);
pictureTest[0].Name = "spawn1";
pictureTest[0].Size = new Size(50, 50);
pictureTest[0].TabIndex = 98;
pictureTest[0].TabStop = false;
this.Controls.Add(pictureTest[0]);
}
Through the course of my research, I've mainly just gotten the advice to use this.Controls.Add, but that doesn't seem to be my issue here. My array is declared earlier with:
PictureBox[] pictureTest = new pictureTest[100];

As #HansPassant says in the comments, this code doesn't compile. The following should broadly do what you want:
int _position = 10;
private void spawn_Click(object sender, EventArgs e)
{
var pictureTest = new PictureBox();
pictureTest.Image = Properties.Resources.testimage;
pictureTest.Location = new Point(_position, 250);
pictureTest.Name = "spawn1";
pictureTest.Size = new Size(50, 50);
pictureTest.TabIndex = 98;
pictureTest.TabStop = false;
this.Controls.Add(pictureTest);
_position += 100;
}
Firstly, there's no need to maintain a separate array of PictureBox, as they are part of your form. Secondly, as #HansPassant said - you were overlaying the images directly on top of each other, so you couldn't tell if you had 1 or 1000.

Related

PictureBox Is Nowhere To Be Seen

I've been experimenting with adding elements to Windows Forms dynamically via code.
I need to create a PictureBox element. So, far, I have the following code:
private void Form1_Load(object sender, EventArgs e)
{
//stylise form
this.BackColor = System.Drawing.Color.Black;
PictureBox bgui = new PictureBox();
bgui.Image = Properties.Resources.attack_box;
bgui.Name = "bgui";
bgui.Location = new Point(0, 600);
this.Controls.Add(bgui);
bgui.Visible = true;
}
However, when this code is run, I get nothing but the black background which I set earlier. I've looked at many questions similar to mine; and they all say I need to add it to the control, which I have done, yet it still abstains from showing.
I would really appreciate it if you could give me an insight into my wrong-doing.
Thanks, Computo.
You need to set Width and Height Properties of the PictureBox.
Try This:
bgui.Width = 500;
bgui.Height = 500;
Complete Code:
private void Form1_Load(object sender, EventArgs e)
{
//stylise form
this.BackColor = System.Drawing.Color.Black;
PictureBox bgui = new PictureBox();
bgui.Image = Properties.Resources.attack_box;
bgui.Name = "bgui";
bgui.Width = 500;
bgui.Height = 500;
bgui.Location = new Point(0, 600);
this.Controls.Add(bgui);
bgui.Visible = true;
}
Turns out that System.Drawing.Point does not translate to the actual pixels on the screen. I will have to investigate how Point translates into pixels.
Here its works perfect. Specify the SizeMode and change the location.
private void Form1_Load(object sender, EventArgs e)
{
//stylise form
this.BackColor = System.Drawing.Color.Black;
PictureBox bgui = new PictureBox();
bgui.Image = Properties.Resources.attack_box;
bgui.Location = new System.Drawing.Point(100, 0);
bgui.Name = "pictureBox1";
bgui.SizeMode = PictureBoxSizeMode.AutoSize;
this.Controls.Add(bgui);
}

C# - Converting a 'System.Drawing.Size' Element to an Integer(s)

I'm working with .NET forms in Visual C#.
I've created a label dynamically, which shows upon a button click. This all works fine; what I'm trying to do is position it so that the element is at the centre of the form. Normally, I'd just set it to half the form size, minus half the element size, but of course this won't work as I am setting the text programmatically also.
My code for the label is as follows:
if(part == 1){
theLabel.Text = "Choose a name for your character!";
}
theLabel.ForeColor = Color.DarkGray;
theLabel.Font = new Font("Arial", 14, FontStyle.Bold);
theLabel.Location = new Point();
I've tried many things here, but I just cannot think of a way. I've tried int[] sizes = (int)theLabel.Size and various other but I just cannot get this to work. Is there another way to line this element to the middle?
If I was you I'd do it this way.
Label theLabel;
private void button1_Click(object sender, EventArgs e)
{
theLabel = new Label();
theLabel.AutoSize = true;
theLabel.BackColor = Color.Red;
theLabel.TextAlign = ContentAlignment.MiddleCenter;
theLabel.Text = "Choose a name for your character!";
this.Controls.Add(this.theLabel);
theLabel.Left = (this.ClientRectangle.Width - theLabel.Width) / 2;
//theLabel.ForeColor = Color.Black;
theLabel.Top = 25;
theLabel.Show();
}

WP8 Error When Trying to Re-Add Push-Pins on Zoom

When I try to zoom in (Button_Click_1 event) I'm getting an error as I try to add images/layers back to a map.
I should note: I've simplified the code substantially so that it would be easier to pick out the error (that I can't seem to figure out).
Each zoom level has a different set of images which are attached to it in case you're wondering why I need to continually clear the layers/images.
Each of the Images/Layers/Overlays is defined globally (so that I can use them in several methods
Image img1 = new Image();
Image img2 = new Image();
MapLayer lyr1 = new MapLayer();
MapLayer lyr2 = new MapLayer();
MapOverlay ovrly1 = new MapOverlay();
MapOverlay ovrly2 = new MapOverlay();
Each of these is initialized when the page loads in a separate method:
private void initializeImages()
{
ovrly1.GeoCoordinate = new GeoCoordinate(49.33783000, -0.45215600);
img1.Source = new BitmapImage(new Uri("images/push-pin.png", UriKind.Relative));
ovrly1.Content = img1;
ovrly1.PositionOrigin = new Point(0.0, 0.0);
img1.Opacity = 0.8;
img1.Height = 30;
img1.Width = 30;
img1.Tap += img1_Tap;
ovrly2.GeoCoordinate = new GeoCoordinate(49.35783000, -0.45425600);
img2.Source = new BitmapImage(new Uri("images/push-pin.png", UriKind.Relative));
ovrly2.Content = img2;
ovrly2.PositionOrigin = new Point(0.0, 0.0);
img2.Opacity = 0.8;
img2.Height = 30;
img2.Width = 30;
img2.Tap += img2_Tap;
}
When I try to zoom on the Button_Click the first time it works fine. But any other time I try to zoom I get an error:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
map1.ZoomLevel += 1;
map1.Layers.Clear();
lyr1.Add(ovrly1); // ERROR OCCURS HERE
map1.Layers.Add(lyr1);
lyr2.Add(ovrly2);
map1.Layers.Add(lyr2);
}
This error disappears when I declare all of the images/overlays/layers 'locally' inside the Button_Click event. But I can't do that, otherwise I won't be able to access the Images outside of the method.
Any help would be greatly appreciated.
I think the problem is that you are adding many overlays on exactly same position.
Additionaly, in the Button Click event you clear the Layers element of the map, but you don't do the same with each layer, adding new elements to to the layer which already have one, each time. Since you add the elements in the same position, you get the error.
Following code should work if both elements have different GeoCoordinate values:
private void Botoia_Click(object sender, RoutedEventArgs e)
{
map1.ZoomLevel += 1;
map1.Layers.Clear();
lyr1.Clear();
lyr2.Clear();
lyr1.Add(ovrly1);
map1.Layers.Add(lyr1);
lyr2.Add(ovrly2);
map1.Layers.Add(lyr2);
}

Creating dynamic comboboxes on a dynamic panel

I am trying to create 4 comboboxes on a dynamically created panel but nothing is showing. What am I missing here, this is my code:
public partial class Form1 : Form
{
ComboBox[] cmb;
public Form1()
{
InitializeComponent();
}
Panel pnl;
private void Form1_Load(object sender, EventArgs e)
{
panel();
createCombo();
}
private void panel()
{
pnl= new Panel();
pnl.Location = new Point(10, 10);
pnl.BorderStyle = BorderStyle.FixedSingle;
pnl.Size = new Size(200, 150);
this.Controls.Add(pnl);
createCombo();
}
private void createCombo()
{
for (int i = 0; i <= 3; ++i)
{
cmb[i] = new ComboBox();
cmb[i].Text = "CodeCall!";
cmb[i].Size = new Size(90, 00);
cmb[i].Location = new Point(i+5, 0);
pnl.Controls.Add(cmb[i]);
}
}
}
Interface shows only the panel:
I initially thought the problem was the height:
cmb[i].Size = new Size(90, 00);
However, it turns out you actually cannot set the height for a ComboBox.
The real problem is a NullReferenceException that is getting swallowed (you must be on a 64-bit system). You never initialize cmb and thus it is null when cmb[i] = new ComboBox() is called.
See http://blog.adamjcooper.com/2011/05/why-is-my-exception-being-swallowed-in.html for an explanation as to why this happens but the conditions that must be met are (quoting):
You are running on a 64-bit version of Windows (whether your application is built for 32-bit or 64-bit doesn’t matter; only the bit depth of the OS)
You are building a WinForms app
You are debugging the application with Visual Studio (using default options for Exception catching)
Your main form has a Load event handler
During the execution of your Load handler, an exception occurs
Tested and the code works fine, provided you're not getting a null reference exception. You need to instantiate your array:
private void createCombo()
{
cmb = new ComboBox[5];
for (int i = 0; i <= 3; ++i)
{
cmb[i] = new ComboBox();
cmb[i].Text = "CodeCall!";
cmb[i].Size = new Size(90, 00);
cmb[i].Location = new Point(i+5, 0);
pnl.Controls.Add(cmb[i]);
}
}
For me, the combobox appeared without issue. However they all appear almost on top of eachother, you'll need to set cmb[i].Location differently. Also, I personally would recommend this instead of what you are doing:
private void createCombo()
{
cmb = new ComboBox[5];
int width = 90;
int height = 25;
int spacing = 5;
for (int i = 0; i <= 3; ++i)
{
ComboBox newBox = new ComboBox();
newBox.Text = "CodeCall!";
newBox.Size = new Size(width, height);
newBox.Location = new Point((i*width)+spacing, 0);
cmb[i] = newBox;
pnl.Controls.Add(newBox);
}
}
The problem is the height:
cmb[i].Size = new Size(90, 00);
Answer doesn't include exact answer.
The problem is that you have a 0 for height. Try for example:
cmb[i].Size = new Size(90, 90);

Adding dynamic controls to form with auto scroll

When I add my controls to the form, it goes fine until I try to specify a location larger than int16.MaxValue. The controls just pile up on top of each other. Here is code which is simplified but demonstrates the behavior:
private void Form1_Load(object sender, EventArgs e)
{
this.AutoScroll = true;
int nexttop = 0;
for (int i = 0; i < 500; i++)
{
TextBox t = new TextBox();
t.Text = i.ToString();
t.Multiline = true;
if (nexttop > Int16.MaxValue)
{
bool debug = true;
}
t.Location = new Point(0, nexttop);
t.Size = new Size(100, 77);
nexttop += t.Height;
this.Controls.Add(t);
}
}
I want to avoid moving the scroll bar programaticaly, since this causes timing issues.
Do you have any ideas on how to fix this? TIA.
This limit (32767) is due to GDI+. I believe different behaviours may be observed according to the Windows version.

Categories

Resources