Reputation: 361
Ran into a confusing problem. I need to make a fishtank in Windows forms. Every time a button is clicked, a fish will appear. I thought about putting code in button_click
function. The problem is that the picturebox with the image doesn't appear when I click the button.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
BackColor = System.Drawing.Color.LightBlue;
}
private void button1_Click(object sender, EventArgs e)
{
//PictureBox pb = new System.Windows.Forms.PictureBox();
PictureBox pb = new PictureBox();
pb.Image = Image.FromFile("C:\\Users\\Elonas\\Desktop\\FishTank\\Photo\\Fish_right.png");
pb.Location = new Point(300, 300);
}
}
}
Upvotes: 1
Views: 668
Reputation: 361
Ow so something like this? Is this one of the controls? Never was I introduced. I get the idea. Ill try to work with that.
pb.Location = new Point(300, 300);
Upvotes: 0
Reputation: 15813
Every time you create a control (pb, in this case), you have to add it to the Controls collection of the form before you can see it.
You can also replace pb.image in the picturebox instead of creating a new picturebox every mouse click. You still need to add it to the form's controls collection when you create it as new (or create it in the Designer).
Upvotes: 2