I want to display a PictureBox ontop of a Picturebox. I have a "mother" Picturebox and a button next to it. Everytime you click the Button a new PictureBox should be displayed on the "mother". I have created the PictureBox like this:
PictureBox newPictureBox = new PictureBox();
newPictureBox.Location = new Point(x:30,y:30);
newPictureBox.BackColor = Color.Red;
newPictureBox.Visible = true;
newPictureBox.Height = 200;
newPictureBox.Width = 200;
Now I have no idea how to display it to the user. I tried to use .Show() and
Call the GetChildIndex and SetChildIndex methods of the parent's Controls collection.
I also tried that. Either I don't know how to call it or it just doesn't work. Been searching for a solution for way too long. Does anybody have an idea of how to display that PictureBox on top of that pictureBox?
So turns out, I forgot to add the picture Box to the "mother" Picturebox. I added 1 line:
motherPictureBox.Controls.Add(newPictureBox);
So now my Code looks like this:
PictureBox newPictureBox = new PictureBox();
newPictureBox.Location = new Point(x:30,y:30);
newPictureBox.BackColor = Color.Red;
newPictureBox.Visible = true;
newPictureBox.Height = 200;
newPictureBox.Width = 200;
motherPictureBox.Controls.Add(newPictureBox);
Didn't need to bring it forward, I just forgot to add it...
you need to add new picturebox to form's controls
PictureBox NewPictureBox = new PictureBox();
NewPictureBox.BackColor = Color.Red;
NewPictureBox.Location = MotherPictureBox.Location;
NewPictureBox.Size = MotherPictureBox.Size;
this.Controls.Add(NewPictureBox);
NewPictureBox.BringToFront();
Related
I am writing a WinForms application. In this application I generate dynamic Label, PictureBox and TextBox controls.
With dragging and dropping an Image into the PictureBox , the added TextBox opens. With entering some text and pressing 'Enter' the following method is fired.
private void tb_Tile_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
TextBox tb_Tile = sender as TextBox;
Tile tb_Tag = tb_Tile.Tag as Tile;
//add function that overgives the given name to the matrix i.e. GiveNameToMatrix()
tb_Tile.Visible = false;
Label lbl_Tile = Controls.Find("Label" + tb_Tag.X + tb_Tag.Y, true).FirstOrDefault() as Label;
lbl_Tile.Visible = true;
//find picture box by tag or sth and then make this pB the parent
PictureBox pb_Tile = (PictureBox)gb_gridBox.Controls["Tile" + tb_Tag.X + tb_Tag.Y];
pb_Tile.BackgroundImage = pb_Tile.Image;
lbl_Tile.Parent = pb_Tile;
// pb_Tile.Visible = false;
if (pb_Tile.HasChildren)
{
lbl_Tile.Text = tb_Tile.Text; //parent has to be set to PictureBox
lbl_Tile.Visible = true;
lbl_Tile.ForeColor = Color.Black;
lbl_Tile.BackColor = Color.Transparent;
lbl_Tile.Location = pb_Tile.Location;
lbl_Tile.Refresh();
pb_Tile.Refresh();
gb_gridBox.Controls.Add(lbl_Tile);
lbl_Tile.BringToFront();
}
}
}
I want the Label.Text to be displayed on the PictureBox. This is why I set the PictureBox as the parent of the Label and the Label.BackColor as Transparent. But the Label just disappears behind the PictureBox...
Does anyone have a clue how to solve this or can give me a hint to another possibility of showing Text in front of the PictureBox?
Thanks in advance.
The problem I see is here:
lbl_Tile.Location = pb_Tile.Location;
The documentation for Location property:
Gets or sets the coordinates of the upper-left corner of the control relative to the upper-left corner of its container.
In your case the pb_Tile is the container of the lbl_Tile, so to achive the desired location you should use something like
lbl_Tile.Location = new Point(0, 0);
Also you should remove this line
gb_gridBox.Controls.Add(lbl_Tile);
because it changes the Parent of the label. parent.Controls.Add(child) and child.Parent = parent do one and the same.
When I add a picturebox to the form at runtime, the picturebox acts exactly like I want. But when I add it to the panel, the pictureBox expands to fit the image.
Below is the code:
private PictureBox globalPicBox = (PictureBox)null;
private void Form1_Load(object sender, EventArgs e)
{
this.globalPicBox = new PictureBox();
this.globalPicBox.Name = "a";
this.globalPicBox.Tag = (object)"a";
this.globalPicBox.SizeMode = PictureBoxSizeMode.StretchImage;
this.globalPicBox.Top = 50;
this.globalPicBox.Left = 50;
this.globalPicBox.Height = 100;
this.globalPicBox.Width = 100;
this.globalPicBox.Image = Image.FromFile(#"C:\Users\PC\Downloads\Photo.jpg");
this.globalPicBox.BorderStyle = BorderStyle.FixedSingle;
this.globalPicBox.AutoSize = true;
this.panel1.Controls.Add((Control)this.globalPicBox);
}
What is the problem? And why?
Based on your edit ... set globalPicBox.AutoSize = false and it'll work.
If it doesn't please check the rest of your code for changes to globalPicBox's AutoSize, SizeMode and Dock parameters and hopefully you'll find your answer!
Simple Just Set globalPicBox.SizeMode=Strech
The image will shrink or stretch to meet the picturebox size and your picture box will not expand or shrink it will remain the same size
I create a user control dynamically in my code
UserControl myobject = new UserControl();
myObject contains a button etc.
when I add this control to my picturebox
picturebox.Controls.Add(myobject);
my picturebox's backgorund image is dissappeared.
Why?
note: the button can be seen however. I want picturebox to be seen also
Set transparent basckground color of your user control. This will make picturebox visible:
UserControlDisplay myobject = new UserControlDisplay();
myobject.BackColor = Color.Transparent;
picturebox.Controls.Add(myobject);
BTW I believe you have different name of user control. And yes, as #samjudson stated, PictureBox should not be used this way: try to use Panel with background image instead (approach will stay same - use transparent color to see parent control):
panel.BackgroundImage = // your image
UserControlDisplay myobject = new UserControlDisplay();
myobject.BackColor = Color.Transparent;
panel.Controls.Add(myobject);
Try this:
UserControl myobject = new UserControl();
Button but = new Button();
but.BackColor = Color.Gray
pic.BackColor = Color.Green;
myobject.Controls.Add(but);
pic.Visible = true;
pic.Controls.Add(myobject);
The PictureBox control is not meant to be used as a container. Try adding a parent Panel or similar and the adding the PictureBox and your custom control to the Panel control.
How can I programmatically add a PictureBox control to a TabPage control and fit to its size?
PictureBox pb = new PictureBox();
pb.Dock = DockStyle.Fill;
tabPage.Controls.Add(pb);
The code would be look like:
var pb = new PictureBox();
pb.Image = Bitmap.FromFile(#"\\filepath");
pb.Dock = DockStyle.Fill;
yourTabPage.Controls.Add(pb);
But if your sole purpose for the tab page is displaying an image, you better use the 'BackgroundImage' of the tabpage itself and use its BackgroundImageLayout to maintain image size, this saves a bit of hassel compared to adding a PictureBox
I want to create an image as a button in code in C# WPF (not a button with BG image but an actual image). I read on this site to use a PictureBox for the image, and I've found that the WPF equivalent is Image. The problem is, that while i've found PictureBox has a .Click that you can set, Image does not. The two things I want to do are:
Create an array of buttons that are images and can be clicked.
Have an image for the unclicked and clicked states of the button.
Is there anything right in front of me I'm missing?
Here is my loop creating the buttons:
sideBarButtons = new Button[infoLoader.categoriesLength];
sideButtons = new Image[infoLoader.categoriesLength];
ImageBrush[] myBg = new ImageBrush[infoLoader.categoriesLength];
for (int i = 0; i < sideBarButtons.Length; i++)
{
myBg[i] = new ImageBrush();
myBg[i].ImageSource = new BitmapImage(graphicLoader.buttonUnselected[(i % myBg.Length)]);
/*sideBarButtons[i] = new Button();
sideBarButtons[i].Content = infoLoader.categories[i].name;
sideBarButtons[i].Background = myBg[i];
//sideBarButtons[i].BorderThickness = ;
sideBarButtons[i].Width = 155;
sideBarButtons[i].Height = 46;
Canvas.SetLeft(sideBarButtons[i], 30);
Canvas.SetTop(sideBarButtons[i], 10 + (46 * i));
sideBarButtons[i].Click += new RoutedEventHandler(this.SideButton_Click);
leftSideBar.Children.Add(sideBarButtons[i]);*/
BitmapImage myBmp = new BitmapImage();
myBmp.BeginInit();
myBmp.UriSource = myBg[i];
myBmp.EndInit();
sideButtons[i] = new Image();
sideButtons[i].Source = myBmp;
sideButtons[i].Width = 155;
sideButtons[i].Height = 46;
Canvas.SetLeft(sideButtons[i], 30);
Canvas.SetTop(sideButtons[i], 10 + (46 * i));
sideButtons[i].Click += new RoutedEventHandler(this.SideButton_Click);
leftSideBar.Children.Add(sideButtons[i]);
}
The first commented out area is when I was creating buttons with buttons and not images, while the second is images and it doesn't work. Thanks in advance.
Two options here:
1.) Instead of using the Click event, which doesn't exist on Image, use MouseDown, which does.
2.) Instead of using Images and repurposing them, use Buttons with a custom style on it. Then you can handle the button's click.
Personally, I'd use the latter, but really either works.