Dynamically creating a picturebox from another class into a form - c#

I'm having troubles with this part trying to create a picturebox into another form from another class, i hope I've provided enough information c:
Catelogue.cs <-- class that loads the picturebox
class Catelogue
{
public void loadCatelogue()
{
mainPageGUI u = new mainPageGUI();
PictureBox pictureBox1 = new PictureBox();
pictureBox1.Location = new System.Drawing.Point(0, 0);
pictureBox1.Name = "pictureBox1";
pictureBox1.Size = new System.Drawing.Size(500, 500);
pictureBox1.BackColor = Color.Red;
u.Controls.Add(pictureBox1);
MessageBox.Show("HI");
}
}
mainmenuGUI.cs < --- form that's calling loadcatelogue() to load picturebox
private void catelogueButton_Click(object sender, EventArgs e)
{
Catelogue a = new Catelogue();
a.loadCatelogue();
}

You are creating a new instance of the mainPageGUI form and add, to that instance, the new picturebox. This instance is not the one that calls your method and it is never showed. So your original instance remain unchanged and you don't see anything. (Just to demonstrate the problem try to change your MessageBox line with u.Show();)
To fix, just change your calling code and pass the form instance on which the picturebox should be created
private void catelogueButton_Click(object sender, EventArgs e)
{
Catelogue a = new Catelogue();
// pass this instance to the method....
a.loadCatelogue(this);
}
and of course use the instance passed
public void loadCatelogue(mainPageGUI u)
{
PictureBox pictureBox1 = new PictureBox();
pictureBox1.Location = new System.Drawing.Point(0, 0);
pictureBox1.Name = "pictureBox1";
pictureBox1.Size = new System.Drawing.Size(500, 500);
pictureBox1.BackColor = Color.Red;
u.Controls.Add(pictureBox1);
}

Related

How to draw in a panel calling the method from another class

I'm trying to call a method which draws something in a panel from another class, but something doesn't work.
This is the drawing method:
class Dibujo
{
public void Dibujar(Object sender, PaintEventArgs e)
{
Form2 ventana2 = new Form2();
Graphics g = ventana2.panel1.CreateGraphics();
Pen Boli = new Pen(Brushes.Green);
Point Inicio = new Point(200, 100);
Point Final = new Point(400, 300);
g.DrawLine(Boli, Inicio, Final);
}
}
}
And here is were I call it:
private void btnDibujar_Click(object sender, EventArgs e){
Graphics g = panel1.CreateGraphics();
Rectangle rect = new Rectangle();
PaintEventArgs pe = new PaintEventArgs(g,rect);
Pen Boli = new Pen(Brushes.Red);
g.DrawLine(Boli,100,100,100,300);
Dibujo panel = new Dibujo();
panel.Dibujar(sender,pe);
}
So the idea is that when I click the button btnDibujar_click a simple line is drawn in the panel but it doesn't work. I know that the code probably has a lot of mistakes but I'm a beginner and I'm learning by myself so all kinds of advice is welcome.

C# : how to modify properties of dynamically created controls like panels etc

I need help with modifying the properties of a dynamically created controls.
First I created 2 panels on the form dynamically like this:
public void create_panels()
{
Panel panel1 = new Panel();
panel1.BackColor = Color.Red;
panel1.Location = new Point(0, 0);
panel1.Size = new Size(320, 480);
this.Controls.Add(panel1);
Panel panel2 = new Panel();
panel2.BackColor = Color.Red;
panel2.Location = new Point(320, 0);
panel2.Size = new Size(320, 480);
this.Controls.Add(panel2);
}
The problem I can't seam to find any clear information about how to modify them after they where (dynamically) created. One would thinks that simply doing something like this below would be easy and nice: (to change the panels color and position). However, this obviously doesn't work since the panel was created dynamically:
panel1.BackColor = Color.Blue;
panel1.Location = new Point(320, 0);
Can someone please help post the simplest way to modify properties of dynamically made control (panel or textbox) changing the controls location or color after it has been created in a dynamic method?
If you still have a reference to the control, you can of course just set the properties directly, as in the following example. The trick is to save the reference as a form-scope variable (instead of as a local variable).
class SomeForm : Form
{
protected Panel _panel1;
public Form_Load(object sender, EventArgs e)
{
_panel1 = new Panel
{
BackColor = Color.Red,
Location = new Point(0, 0),
Size = new Size(320, 480)
};
this.Controls.Add(panel1);
}
public void Example()
{
_panel1.BackColor = Color.Blue; //Simple to change it if you have a reference already
}
}
If you don't want to keep a reference, you can also grab one from the Controls collection, like this:
class SomeForm : Form
{
public Form_Load(object sender, EventArgs e)
{
Panel panel1 = new Panel
{
Name = "SomeNameICanUse", //Important!
BackColor = Color.Red,
Location = new Point(0, 0),
Size = new Size(320, 480)
};
this.Controls.Add(panel1);
}
public void Example()
{
var panel1 = this.Controls.Find("SomeNameICanUse") as Panel; //use the name to find it
if (panel1 != null) panel1.BackColor = Color.Blue;
}
}

Dynamically Adding Class of Controls

I am having trouble with dynamically adding a class of controls that should when working look like this:
When a new one is added it should appear in the left panel under the toolstrip.
So far I am having trouble making them appear (The one in the middle is just the design I made).
Here is the code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Problem Occurs Here
EquationBox[] EquationBoxArray = new EquationBox[12];
for (int x = 0; x < 12; x++)
{
EquationBoxArray[x] = new EquationBox();
ActiveForm.Controls.Add(EquationBoxArray[x].mainPanel);
ActiveForm.Controls.Add(EquationBoxArray[x].colorPanel);
}
}
private void add_line_Click(object sender, EventArgs e) //Add Line
{
}
private void clear_Click(object sender, EventArgs e) //Clear Lines
{
}
}
public class EquationBox
{
public Panel colorPanel = new Panel();
public Panel mainPanel = new Panel();
public TextBox equationBox = new TextBox();
public CheckBox isVisibleBox = new CheckBox();
public EquationBox()
{
mainPanel.Size = new Size(200, 72);
colorPanel.Size = new Size(33, 72);
mainPanel.Location = new Point(50, 50);
colorPanel.Location = new Point(50, 50);
colorPanel.BackColor = Color.Red;
}
}
The problem occurs here:
//Problem Occurs Here
EquationBox[] EquationBoxArray = new EquationBox[12];
for (int x = 0; x < 12; x++)
{
EquationBoxArray[x] = new EquationBox();
ActiveForm.Controls.Add(EquationBoxArray[x].mainPanel);
ActiveForm.Controls.Add(EquationBoxArray[x].colorPanel);
}
When I run it, it return with:
Additional information: Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.
And even before that started happening, the EqautionBox wouldn't appear.
Thanks in advance, this is really troubling me.
For the constructor of EquationBox:
public EquationBox()
{
mainPanel.Size = new Size(200, 72);
colorPanel.Size = new Size(33, 72);
mainPanel.Location = new Point(50, 50);
colorPanel.Location = new Point(50, 50);
colorPanel.BackColor = Color.Red;
}
First, your control appeared, but mainPanel is overlap colorPanel and you can't see mainPanel (same BG color as your form), so swap which added first solved
EquationBox[] EquationBoxArray = new EquationBox[12];
for (int x = 0; x < 12; x++)
{
EquationBoxArray[x] = new EquationBox();
this.Controls.Add(EquationBoxArray[x].colorPanel);
this.Controls.Add(EquationBoxArray[x].mainPanel);
}
I am using this.Controls, not sure about the ActiveForm.Controls part, maybe on constructing, your Form1 is not the active one, so error occured.
Ps: I suggest add colorPanel to mainPanel, and only add mainPanel to Form. And UserControl is a good solution here as Steve Wellens said.
There are various issues with EquationBox the TextBox and CheckBox are not in the panel. It would be easier to make it a UserControl.
Then to do the positioning use a FlowLayoutPanel.

panel to Imagelist in C#

Hello friends i want to create new bitmap image of panel and want to store it in the imagelist and when the controls in the panel gets changed i want the image with different name and to be added to the image list and here goes my code for this.
private void button5_Click(object sender, EventArgs e)
{
var listViewItem = listView2.Items.Add(label1.Text);
Bitmap bm = new Bitmap(panel3.Size.Width, panel3.Size.Height);
panel3.Refresh();
panel3.DrawToBitmap(bm, new Rectangle(0, 0, panel3.Size.Width, panel3.Size.Height));
imageList1.Images.Add("1", bm);
listViewItem.ImageKey = "1";
}
You should keep a variable to determine the name every time you add one.
This sample keeps a variable at class level (nextImageNumber) which value is raised with 1 every time you generate a bitmap:
int nextImageNumber = 1;
private void button5_Click(object sender, EventArgs e)
{
var listViewItem = listView2.Items.Add(label1.Text);
Bitmap bm = new Bitmap(panel3.Size.Width, panel3.Size.Height);
panel3.Refresh();
panel3.DrawToBitmap(bm, new Rectangle(0, 0, panel3.Size.Width, panel3.Size.Height));
string name = nextImageNumber.ToString();
imageList1.Images.Add(name, bm);
listViewItem.ImageKey = name;
nextImageNumber++;
}

C# WinForms problem with draw image

class OriginalImage: Form
{
private Image image;
private PictureBox pb;
public OriginalImage()
{
pb = new PictureBox {SizeMode = PictureBoxSizeMode.CenterImage};
pb.SizeMode = PictureBoxSizeMode.StretchImage;
Controls.Add(pb);
image = Image.FromFile(#"Image/original.jpg");
this.Width = image.Width;
this.Height = image.Height;
this.Text = "Original image";
this.Paint += new PaintEventHandler(Drawer);
}
public virtual void Drawer(object source, PaintEventArgs e)
{
Graphics g = pb.CreateGraphics();
g.DrawImage(image,0,0);
}
I call this create object OriginalImage in other form on button click, but image is not draw? Where is problem?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var oi = new OriginalImage();
oi.Show();
}
}
You're creating a PictureBox and adding it to your controls, but you never actually use it (you're drawing the image manually in the Paint event). Why? This control is likely obscuring the drawing area of the form, as any controls go on top of whatever you draw in the Paint event.
In addition, you're getting the Graphics object by calling CreateGraphics on the PictureBox rather than the Form itself. This is wrong, as the PictureBox's Paint event will fire after this code, erasing whatever you draw.
I would recommend changing your OriginalForm code to the following:
class OriginalImage: Form
{
private Image image;
private PictureBox pb;
public OriginalImage()
{
pb = new PictureBox();
pb.SizeMode = PictureBoxSizeMode.StretchImage;
pb.Dock = DockStyle.Fill; // this will make the PictureBox occupy the
// whole form
Controls.Add(pb);
image = Image.FromFile(#"Image/original.jpg");
this.ClientSize = new Size(image.Width, image.Height); // this allows you to
// size the form while
// accounting for the
// border
this.Text = "Original image";
pb.Image = image; // use this instead of drawing it yourself.
}
}

Categories