Click on a picturebox and go to website - c#

I have a pictureBoxes that are being created at run time and I would like to be able to click on that box and go to a webpage after the program completes. How can I create a click event for something like this?
This is what I am thinking:
PictureBox PB = new PictureBox();
PB.Name = "PB" + i.ToString();
PB.Location = new Point(51 * i, 331);
PB.Size = new Size(50, 50);
PB.ImageLocation = Sub1;
Controls.Add(PB);
PB.Click +=new EventHandler(PB_Click);
protected void PB_Click(object sender, EventArgs e)
{
MessageBox.Show("You clicked the mouse over the PictureBox");
}
Is this on the right track?

If you want open Internet explorer and navigate to the desired address automatically, use this:
Process.Start("iexplore.exe", "http://www.google.com");

After fooling around a little I figured it out and thought I would post my solution, maybe someone else could benefit. I decided to take the easy route and just open up the link using webrowser control on the form.
![private void FrmWeb_Btn_Click(object sender, EventArgs e)
{
PictureBox PB = new PictureBox();
PB.ImageLocation = "https://si0.twimg.com/profile_images/378800000038434114/f676cbea6f8500c9c15529e1d5e548c1_reasonably_small.jpeg";
PB.Size = new Size(100, 100);
Controls.Add(PB);
PB.Click +=new EventHandler(PB_Click);
}
protected void PB_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://twit.tv/");
}][2]

Related

Click event in FlowLayoutPanel

i am programming in Visual Studio 2013, c# winform. Is it posible to choose click event for each button in FLP? Im trying to do something like Steam Library, I already did a lot of things and it now looks like i want to.
This is how it looks (adding)
This is how it looks (library)
(sorry, im not able to add images)
But i don't know how to open choosed game when you click on a button in FLP (in library).
This is how my code looks:
private void btnTest_Click_1(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
if (textBox2.Text != "")
{
Button btn = sender as Button;
Button btnNew = new Button();
btnNew.Text = "";
btnNew.Height = 108;
btnNew.Width = 230;
btnNew.Image = new Bitmap(textBox1.Text);
btnNew.FlatStyle = FlatStyle.Flat;
flpContainer.Controls.Add(btnNew);
btnNew.Click += btnNew_Click;
counter1++;
label1.Text = counter1.ToString(); //label1 is that "Number of games in library:"
System.Windows.Forms.MessageBox.Show("Game was succesfully added to library!");
}
else if (textBox2.Text == "")
{
System.Windows.Forms.MessageBox.Show("You didn't choosed exe file!");
}
}
if (textBox1.Text =="")
{
System.Windows.Forms.MessageBox.Show("You didn't choosed image!");
}
}
private void btnNew_Click(object sender, EventArgs e)
{
Process.Start(textBox2.Text); //textbox2 is the path to exe file, but it change when you want to add another game to library
}
But how to do different click event for each button in FlowLayoutPanel?
Thank you for answers.
EDIT: I want to do that, when you click on a button in library it will open that game (program).
Thank you a lot!
Store the required information in Tag property for future use and get the job done.
Button btnNew = new Button();
btnNew.Text = "";
btnNew.Height = 108;
btnNew.Width = 230;
btnNew.Image = new Bitmap(textBox1.Text);
btnNew.FlatStyle = FlatStyle.Flat;
flpContainer.Controls.Add(btnNew);
btnNew.Click += btnNew_Click;
btnNew.Tag = textBox2.Text;// <--Store it in Tag
Then use it in Click event
private void btnNew_Click(object sender, EventArgs e)
{
Button clickedButton = (Button)sender;
Process.Start((string)clickedButton.Tag);
}

Setting the image of a Picturebox to the picture that the was clicked on form load

I had some trouble wording it in the title of this post so please looking here if you are confused on my question.
In the instance my question exists, my image viewer is the default for .jpg files. How would I go about setting the image of the picturebox to the .jpg file that was clicked?
I've researched a bit on how to do this but I haven't come up with anything and I believe it's because I'm not wording it right. Thanks in advance, Noah.
Also, if you need any other information or have questions, just ask.
Code:
private void Form1_Load(object sender, EventArgs e)
{
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Left, Screen.PrimaryScreen.WorkingArea.Top);
this.Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
pictureBox1.Size = new Size(this.Width - this.Width/2, this.Height - this.Height/2);
pictureBox1.Location = new Point(300, 250);
pictureBox1.Image = Image.FromFile(Environment.GetCommandLineArgs[0]);
}
EDIT:
Added current code being used
The file that was double clicked will be passed to your application as a "command line argument".
You can retrieve that value using Environment.GetCommandLineArgs() in the Load() event of your form and load it into your PictureBox from there.
The executable itself is at index 0 (zero), with the argument at index 1 (one).
With that in mind, it should look more like this:
private void Form1_Load(object sender, EventArgs e)
{
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 0)
{
try
{
pictureBox1.Image = Image.FromFile(args[1]);
}
catch (Exception ex)
{
MessageBox.Show("File: " + args[1] + "\r\n\r\n" + ex.ToString(), "Error Loading Image");
}
}
}

Display an image into windows forms

I wanted to display an image to the windows forms, but i already did this and the image did not come out.
Where did I go wrong?
Here is the code:
private void Images(object sender, EventArgs e)
{
PictureBox pb1 = new PictureBox();
pb1.Image = Image.FromFile("../SamuderaJayaMotor.png");
pb1.Location = new Point(100, 100);
pb1.Size = new Size(500, 500);
this.Controls.Add(pb1);
}
Here (http://www.dotnetperls.com/picturebox) there 3 ways to do this:
Like you are doing.
Using ImageLocation property of the PictureBox like:
private void Form1_Load(object sender, EventArgs e)
{
PictureBox pb1 = new PictureBox();
pb1.ImageLocation = "../SamuderaJayaMotor.png";
pb1.SizeMode = PictureBoxSizeMode.AutoSize;
}
Using an image from the web like:
private void Form1_Load(object sender, EventArgs e)
{
PictureBox pb1 = new PictureBox();
pb1.ImageLocation = "http://www.dotnetperls.com/favicon.ico";
pb1.SizeMode = PictureBoxSizeMode.AutoSize;
}
And please, be sure that "../SamuderaJayaMotor.png" is the correct path of the image that you are using.
There could be many reasons for this. A few that come up quickly to my mind:
Did you call this routine AFTER InitializeComponent()?
Is the path syntax you are using correct? Does it work if you try it in the debugger? Try using backslash (\) instead of Slash (/) and see.
This may be due to side-effects of some other code in your form. Try using the same code in a blank Form (with just the constructor and this function) and check.
I display images in windows forms when I put it in Load event like this:
private void Form1_Load( object sender , EventArgs e )
{
pictureBox1.ImageLocation = "./image.png"; //path to image
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
}
private void Form1_Load(object sender, EventArgs e)
{
PictureBox pb = new PictureBox();
pb.Location = new Point(0, 0);
pb.Size = new Size(150, 150);
pb.Image = Image.FromFile("E:\\Wallpaper (204).jpg");
pb.Visible = true;
this.Controls.Add(pb);
}

Dynamcially Creating a table inside a panel in C#.Net

Here is my code
protected void Button1_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
Label newLabel = new Label();
newLabel.ID = "lbltest";
newLabel.Text = "my new label..";
panel1.Controls.Add(newLabel);
}
I cant see the my label text when i click the button
Any help appreciate
Thanks
You must add your panel inside of any control which exists on your page.
You have to add the Panel to some control in your web page or your top level form element if you don't have anywhere else to put it.
protected void Button1_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
Label newLabel = new Label();
newLabel.ID = "lbltest";
newLabel.Text = "my new label..";
panel1.Controls.Add(newLabel);
this.Form.Controls.Add(panel1); // YOU ARE MISSING THIS
}
You need to add the Panel to the page:
protected void Button1_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
Label newLabel = new Label();
newLabel.ID = "lbltest";
newLabel.Text = "my new label..";
panel1.Controls.Add(newLabel);
//Do this
SomeControlOnYourPage.Controls.Add(panel1);
}

Create a button which creates button

I am a begginer in C# and I want to create a button which creates button.
but these buttons never appear...
please find my code :
private void addstrat3_i_Click(object sender, EventArgs e)
{
panel3strat.Width += 200;
Button addstrat3_2 = new Button();
addstrat3_2.Size = new Size(210, 41);
addstrat3_2.Location = new Point(50,50);
addstrat3_2.Visible = true;
}
Thanks a lot
You have to add the button (or any other controls) on the form using the Controls property, for sample:
private void addstrat3_i_Click(object sender, EventArgs e)
{
panel3strat.Width += 200;
Button addstrat3_2 = new Button();
addstrat3_2.Size = new Size(210, 41);
addstrat3_2.Location = new Point(50,50);
addstrat3_2.Visible = true;
// add control
this.Controls.Add(addstrat3_2);
}
You need to add the button to the form.
this.Controls.Add(addstrat3_2);

Categories