I have a Winforms application where I programmatically add a number of pictureBox controls and load each one with an image.
How can I link each pictureBox control with a video file so I can run that video by clicking on that pictureBox control?
This is how I add my pictureBox controls:
if (ofd.ShowDialog() == DialogResult.OK)
{
file_address = ofd.FileName;
}
var picture1 = new PictureBox
{
Name = "pictureBox",
Size = new Size(160, 200),
Location = new Point(x, y),
SizeMode = PictureBoxSizeMode.Zoom,
Image = Image.FromFile(file_address),
};
this.Controls.Add(picture1);
I've managed to add click event handler:
picture1.Click += delegate
{
// Do something
}
You can use the Tag property to store the file_address.
this.picture1.tag = file_address;
when you want to play the video by the picture box file name, get the file name by
string file_name = (string)this.picture1.tag;
Related
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();
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.
I have a form application in which I can input a name of an image and view the image in a PictureBox after clicking a button.
private void button1_Click(object sender, EventArgs e)
{
string image = textBox1.Text + ".jpg";
PictureBox pictureBox1 = new PictureBox();
pictureBox1.ImageLocation = image;
}
This is my code but it doesn't do anything, the image I tried to search didn't appear in the PictureBox. What could possibly go wrong? Answers will be appreciated.
Because you've created a new instance of PictureBox but you didn't add it to your form. You should add it to your form's controls like this:
string image = textBox1.Text + ".jpg";
PictureBox pictureBox1 = new PictureBox();
//Set pictureBox1's location on the form
pictureBox1.Location = new Point(10 , 10);
//Add pictureBox1 to your form
Controls.Add(pictureBox1);
Now if your image variable contains a valid image path your PictureBox should show it.
Edit: To write a valid image path in your TextBox try write the full picture's path in your TextBox like below:
D:\Pics\yourPic
Or if you've added it to your project it should be like this:
D:\New folder (1)\WindowsFormsApplication1\WindowsFormsApplication1\yourPic
Just don't forget if you have already placed a PictureBox in your form you just need to call it in the code. You don't need to create a new one. And you should remove this line PictureBox pictureBox1 = new PictureBox();.
I have an mdichild, and an eventhandler for draganddrop so when I drop an image file in my form, a picturebox ( name = dpic ) is created with that image.
I have another eventhandler which is for dpic_Click, so when I click on the image, my form's text is the name of that image.
After the first time I drop an image, another dpic is created, because you can't have two cotrols with the same name. C# automatically changes the name and that makes my event handlers only work for the last image I dropped in. I think if I could make an event handler for my mdichild that gets the name of the control that is under the mouse pointer I could simply change back the image I am pointing at.
UPDATE
Here is my code , I made an event handler for droping in my mdichild :
void mdiChild_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
dpic = new PictureBox() ;
string[] filepath = (string[])e.Data.GetData(DataFormats.FileDrop);
Image image = Image.FromFile(filepath[0]);
dpic.Image = image;
dpic.Tag = Path.GetFileName(filepath[0]);
this.ActiveMdiChild.Controls.Add(dpic);
dpic.ContextMenuStrip = this.contextMenuStrip1;
this.ActiveMdiChild.Refresh();
dpic.BringToFront();
this.ActiveMdiChild.ActiveControl = dpic;
dpic.Click += new EventHandler(dpic_Click);
// _____this helped me do it_________________________________________________
foreach (Control c in this.ActiveMdiChild.Controls)
{
c.Click += new EventHandler(c_Click);
}
// ________________________________________________________________________
}
}
What you are looking for is a sender. The sender will tell which image was clicked and will allow you to get its name.
PictureBox picSender = (PictureBox)sender;
label1.Text = picSender.Name;
EDIT : You put that in the pic_Click event
I don't understand exactly what you want to do here, possibly layering new pictureboxes with dropped images on the form?
If that is so you can use a
List<PictureBox> picboxes = new List<PictureBox>();
and where you do:
dpic = new PictureBox() ;
change to
PictureBox dpic = new PictureBox() ;
picboxes.Add(dpic);
this.Controls.Add(dpic);
But please note that you cannot have unlimited controls declared.
How to navigate the image using Keyboard arrow keys in C#.
My 1st form contains listView. Listview contains 10 images in thumbnail format.Images are from perticular folder. When i double click the image in the list view, its opening in a new window as large image. I want to navigate the image in the new window as per listview order.
If i click the image randomly, want to navigate the image from that selected image.
Its like a Microsoft picture manager.
Plz give me an Idea.
Set your form's KeyPreview property to True. Then add this line of code to the top of your CS file:
using System.IO;
Next, inside the scope of your form, add these two lines:
private FileInfo[] _files;
private int _currentFile;
In your form's Load event, put this code:
DirectoryInfo dirinfo = new DirectoryInfo(
Path.Combine(Application.StartupPath, "images"));
_files = dirinfo.GetFiles();
_currentFile = 0;
Bitmap bmp = (Bitmap)Bitmap.FromFile(_files[_currentFile].FullName);
pictureBox1.Image = bmp;
Finally, in your form's KeyDown event, put this code:
if (e.KeyCode == Keys.Down)
{
_currentFile--;
if (_currentFile < 0)
{
_currentFile = _files.Length - 1;
}
}
else if (e.KeyCode == Keys.Up)
{
_currentFile++;
if (_currentFile >= _files.Length)
{
_currentFile = 0;
}
}
Bitmap bmp = (Bitmap)Bitmap.FromFile(_files[_currentFile].FullName);
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
}
pictureBox1.Image = bmp;
This code assumes that you have a PictureBox on your form named "pictureBox1", and it assumes that you have a folder named "\images" in your application folder that contains the image files you wish to display.
As o.k.w. mentioned in a comment, you may want to enhance this code by resizing the PictureBox to fit the dimensions of the image file. You can do that by setting the SizeMode property of your PictureBox to AutoSize (or set it to CenterImage, if you wish to keep the overall image centered on your form).