Adding picturebox at runtime and changing their properties (from another method) - c#

I'm adding a PictureBox to a Form at runtime in C# and it does work (I think). Only problem is I can't change the properties of the PictureBox from outside nor from inside the method, eg. I can't even change the BackColor.
This is my attempt:
private void Form1_Load(object sender, EventArgs e)
{
PictureBox canvas = new PictureBox();
canvas.Dock = DockStyle.Fill;
canvas.BackColor = Color.Red;
}
The PictureBox should now fill the entire Form and have a red background but it doesn't work.
Also, how do I add the PictureBox "publicly" so I can change the properties from another method?
I tried it like this:
PictureBox canvas = new PictureBox
{
Dock = DockStyle.Fill,
BackColor = Color.Red
};
private void button1_Click(object sender, EventArgs e)
{
canvas.BackColor = Color.Red;
} // Now I can use "canvas" in other methods without any errors but still nothing happens

That's good start:
PictureBox canvas = new PictureBox
{
Dock = DockStyle.Fill,
BackColor = Color.Red
};
private void button1_Click(object sender, EventArgs e)
{
canvas.BackColor = Color.Red;
}
But you have to add that box to your form:
private void Form1_Load(object sender, EventArgs e)
{
this.Controls.Add(canvas);
}
Once it's working as expected, you should call canvas field like that: _canvas as this is private field of this class (form).

Related

C# How to disable paint event of a panel when called using panel.BringToFront() method? [duplicate]

This question already has answers here:
Double Buffering when not drawing in OnPaint(): why doesn't it work?
(2 answers)
Closed 7 years ago.
I have two Panels stacked in Form, When one panel1 is showing,the panel2 is in background and If i call Panel2 using BringToFront() method to View the Panel2, The paint Event of the Panel2 is calling. So i want to prevent Panel2 from calling Paint Event to Disable Redrawing of the Panel.
I think the paint method is a default event that has any regard to "display", hmm have you tried doing a workaround like creating a separate static flag for the purpose of preventing your paint events?
(again this is just a workaround)
Sample :
public bool displayed {get; set;}
private void Form1_Load(object sender, EventArgs e)
{
displayed = false;
}
private void Panel1_Paint(object sender, PaintEventArgs e)
{
if(!displayed) {
// do paint functions
displayed = true;
}
}
EDIT : Please try this one.
private void Form1_Load(object sender, EventArgs e)
{
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(
new Pen(Color.Green, 3),
new Rectangle(10, 10, 50, 50));
//Drawing an ellipse
e.Graphics.FillEllipse(Brushes.Green, new Rectangle(60, 60, 100, 100));
//Drawing text
e.Graphics.DrawString("Text", new Font("Verdana", 14), new SolidBrush(Color.Green), 200, 200);
}
private void button1_Click_1(object sender, EventArgs e)
{
panel1.Visible = true;
panel2.Visible = false;
}
private void button2_Click_1(object sender, EventArgs e)
{
panel1.Visible = false;
panel2.Visible = true;
}
// code described below
public class MyDisplay : Panel
{
public MyDisplay()
{
this.DoubleBuffered = true;
}
}
As mentioned here Gets or sets a value indicating whether this control should redraw its surface using a secondary buffer to reduce or prevent flicker. This should now work regardless if SendToBack() BringToFront() or Visible set.

Scrolling shapes in a panel

I'm working on a program which draws shapes on a panel. I added a vertical scroll bar to the panel but when scrolling the panel, the shapes stays where they are and don't scroll. How can I make these shapes scroll? I'm using this code in the paint handler to update these shapes:
e.Graphics.FillRectangle(Brushes.Red, selectedRect);
on scrolling you should take care of the actual scroll position of the panel and consider this offset when painting your shapes again.
there are examples like this online (not exactly your case but could help to identify the problem):
private int count;
private ArrayList arrayctl = new ArrayList();
private void button1_Click(object sender, System.EventArgs e)
{
TextBox newtxt = new TextBox();
newtxt.Text = count.ToString();
count++; arrayctl.Add(newtxt);
DrawControls();
}
private void DrawControls()
{
System.Drawing.Point CurrentPoint; CurrentPoint = panel1.AutoScrollPosition;
int i = 0;
panel1.Controls.Clear();
panel1.SuspendLayout();
foreach (TextBox txt in arrayctl)
{
panel1.Controls.Add(txt);
txt.Width = panel1.ClientRectangle.Width;
txt.Top = i; i += txt.Height;
}
panel1.ResumeLayout();
panel1.AutoScrollPosition = new Point(Math.Abs(panel1.AutoScrollPosition.X), Math.Abs(CurrentPoint.Y));
}
I have also found this here in SO: Control position inside Windows.Forms.Panel with autoscroll
Don't add a VerticalScrollBar to the panel. The Panel already handles scrolling itself when you set the AutoScrollMinSize property:
Rectangle selectedRect = new Rectangle(16, 16, 64, 28);
private void Form1_Load(object sender, EventArgs e)
{
panel1.AutoScrollMinSize = new Size(panel1.ClientRectangle.Width - SystemInformation.VerticalScrollBarWidth, 1200);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.TranslateTransform(panel1.AutoScrollPosition.X, panel1.AutoScrollPosition.Y);
e.Graphics.FillRectangle(Brushes.Red, selectedRect);
}
private void panel1_Scroll(object sender, ScrollEventArgs e)
{
panel1.Invalidate();
}

change form background image through a button click

I want to change the form's back ground image when I click the button. I'm stuck at this error. It says:
An object reference is required for
the non-static field, method, or
property
'System.Windows.Forms.Control.BackgroundImage.get'
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
pictureBox1.Location = new Point(25, 9);
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Location = new Point(18, 9);
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
Form1.BackgroundImage =
}
On the last part of the code, you can see that I am attempting to change the background image of the form. but it does not allow me and I don't know how to do it properly.
Use this instead of Form1:
this.BackgroundImage = ...
Form1 is a Type, not an Instance of an Object, you're looking for this.
if you want to use Form1 use this:
Form1 f1 = new Form1();
f1.BackgroundImage = ...

Obtaining the parameters of the winform form (Width & height)

I want to obtain the height and width properties of a winforms form to be able to color all of it?
I've tried this code..
private void Form1_Load(object sender, EventArgs e)
{
using (Graphics g = this.CreateGraphics())
{
Rectangle r=this.DisplayRectangle;
g.DrawRectangle(Pens.Black, new Rectangle(0, 0, r.X, r.Y));
}
}
But it doesn't do the job. How do I color the whole form in black with the graphics object and Rectangle object?
If you do like that, you will just be painting on the screen where the window happens to be. The window is not aware of that, and when the window is updated for any reason it will be redrawn without the color.
Use the Paint event to draw graphics on the window. Add an event handler for the event, and it will be called whenever the window has to be redrawn. The event arguments contains a Graphics object that you should use for drawing.
Use the Width and Height properties of the DisplayRectangle as width and height, not the X and Y properties. However, as the Graphics object is clipped to the area that is to be updated, you could just use the Clear method to fill it with a color.
I have two buttons on my form(in design view) button1_Click is to paint it black, and button2_Click is to paint form back to Control color.
public partial class Form2 : Form
{
private Brush brushToPaint;
public Form2()
{
InitializeComponent();
brushToPaint = SystemBrushes.Control;
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.FillRectangle(brushToPaint, this.DisplayRectangle);
}
private void button1_Click(object sender, EventArgs e)
{
brushToPaint = Brushes.Black;
InvokePaint(this, new PaintEventArgs(this.CreateGraphics(), this.DisplayRectangle));
}
private void button2_Click(object sender, EventArgs e)
{
brushToPaint = SystemBrushes.Control;
InvokePaint(this, new PaintEventArgs(this.CreateGraphics(), this.DisplayRectangle));
}
}
Do you have to do this using Graphics and DisplayRectangle?
The form has a property called BackColor, which you could simply set to black:
private void Form1_Load(object sender, EventArgs e)
{
this.BackColor = Color.Black;
}

changing panel colors

I created a simple code here just to play around with C#. It has 3 buttons and 1 panel. If you click on the 2nd & 3rd button the panel height changes. Is that also possible to change the color?
For example, if I click on the 2nd button, I would like to have it as yellow and at the same time the height changes as well, and the same with 3rd button.
public partial class Form1 : Form
{
public int heightPanel;
public Form1()
{
InitializeComponent();
heightPanel = panel1.Height;
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Height = heightPanel;
}
private void button2_Click(object sender, EventArgs e)
{
panel1.Height = this.Height/2;
}
private void button3_Click(object sender, EventArgs e)
{
panel1.Height = this.Height - 150;
}
}
I have an idea but I don't know where to put this. I think it would be something like this:
panel1.Height=this.BackColor.ToString();
Any inputs?
Response to reply
Yes, I would like to retain the 3 colors if I click in any of the button. I'm not sure if it's possible. For ex:
my button1 =pink
button2=yellow
button3=green
If I click on the button 1 I'll see the pink color, and if I click on the button2 I'll see the pink and yellow.
Is that possible?
All you'd need to do would be to set the BackColor on a new line. For instance...
private void button2_Click(object sender, EventArgs e)
{
panel1.Height = this.Height/2;
panel1.BackColor = Color.Yellow;
}
private void button3_Click(object sender, EventArgs e)
{
panel1.Height = this.Height - 150;
panel1.BackColor = Color.Yellow;
}
Simple as:
panel1.BackColor = Color.Red;
In button2_Click, just add another line like this:
panel1.BackColor = Color.Yellow;

Categories