C# - Code in form constructor not executed - c#

Relatively new to C#; hopefully I'm just overlooking something simple.
I have a form named 'Exercise1' which contains a picture box called 'drawingArea' and a few buttons. The code for the constructor of Exercise1 is as follows:
public Exercise1()
{
InitializeComponent();
paper = drawingArea.CreateGraphics();
balloon = new Balloon("redBalloon", Color.Red, drawingArea.Width / 2,
drawingArea.Height / 2, 30);
paper.Clear(Color.White);
balloon.Display(paper);
}
...
'paper' and 'balloon' are created as globals above the constructor for use in the other methods on the form. Both 'paper' and 'balloon' work as initialized in the constructor in the other methods defined on the form.
For whatever reason, the commands
paper.Clear(Color.White);
and
balloon.Display(paper);
Which should clear the picture box and show a red ellipse, don't execute (at least visibly). What gives?
UPDATE:
Think I'm going to like this website... You guys are quick!
#Nitesh: The constructor for Exercise1 is called from another form. Code is as follows:
private void button1_Click(object sender, EventArgs e)
{
int exSelector = (int)numericUpDown1.Value;
switch (exSelector)
{
case 1:
Exercise1 form1 = new Exercise1();
form1.Show();
break;
...
#Sean Dunford: Yes and yes it is.
#RBarryYoung: Was playing around with that a bit, but had no luck. What command triggers a Form_Load event for Exercise1?
UPDATE: This altered code works as expected:
public Exercise1()
{
InitializeComponent();
paper = drawingArea.CreateGraphics();
drawingArea.BackColor = Color.White;
drawingArea.Paint += new PaintEventHandler(this.drawingArea_Paint);
balloon = new Balloon("redBalloon", Color.Red, drawingArea.Width / 2, drawingArea.Height / 2, 30);
}
private void drawingArea_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
balloon.Display(e.Graphics);
}
...
Thanks for all the help!

You cannot do drawing in the constructor. To do proper drawing, you need to have the form shown on the screen. You can try using the Shown event to do your rendering (this may get lost when the form is redrawn, though).
Usually the best way is to set whatever flags you need in the constructor and then use the Paint event of the form to do all painting. Later on, when you need to repaint something, set up whatever state needs to be rendered, invalidate your form (this results in a Paint event) and then you can repaint the new state.
If you try to do customized drawing (outside your Paint event) you'll run the risk of things randomly going blank or your drawing may disapper when you resize/minimize your form.

You use Graphics in a constructor, that means that you draw on the paper only once, any redraw for whatever reason that happens after constructor will draw the drawingArea in its original way. Try to add PaintEventHandler to drawingArea and then call inside balloon.Display(e.Graphics);
public Exercise1()
{
InitializeComponent();
balloon = new Balloon("redBalloon", Color.Red, drawingArea.Width / 2,
drawingArea.Height / 2, 30);
drawingArea.Paint += new PaintEventHandler(drawingArea_Paint);
}
void drawingArea_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
baloon.Display(e.Graphics);
}

You should be overriding the forms OnPaint event handler. In doing so, you are able to get the graphics context which will redraw your paper and balloon areas.

Related

Show() method will not show .gif in picturebox that was hidden a second ago

Okay so I have created a Flowlayoutpanel in which about 70 usercontrols, all squares of 50x50 pixels with an animated grass gif inside a picturebox inside of it. I want to make these hide-able but not be removed so there's a white space left. I've been able to do that quite consistently by just using Hide() and changing the background colour to be transparant so I don't have to hide the entire UserControl.
Problem comes up here, I want these gifs to reappear on a timer, so I've created a timer that references a method which just uses a show() again to show said gif and yet it refuses to do so if I create a seperate button to do so or not.
I have searched quite a bit but I think this is a problem only because I'm consistently missing something in my code so no other question really seems to answer my troubles. Because it's such a simple question I feel kind of embarrassed even asking it but oh well.
This is the timer event that calls the method in the other class
private void GrasTerugkeerTimer_Tick(object sender, EventArgs e)
{
var instance = new GrasVeldUC();
instance.TerugKeerGras();
}
This is the events and methods I've set up to show and hide the picturebox that the gif is inside of
public partial class GrasVeldUC : UserControl
{
public GrasVeldUC()
{
InitializeComponent();
this.BackColor = Color.White;
}
private void GrasVeldUC_Click(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(0, 255, 255, 255);
PIBGrasUC.Hide();
}
private void PIBGrasUC_Click(object sender, EventArgs e)
{
this.BackColor = Color.FromArgb(0, 255, 255, 255);
PIBGrasUC.Hide();
}
public void TerugKeerGras()
{
this.BackColor = Color.FromArgb(100, 255, 255, 255);
PIBGrasUC.Show();
}
}
Now I expected this to just work and just show the picturebox again and yet whatever I tried, creating a seperate method that references the method in the other class, it would not work. And the method in the class "GrasVeldUC" does work I put a messagebox inside of it to test and it worked perfectly.
You can just toggle the picturebox.visible as needed.

How to call Paint event on MouseHover?

I have a panel's Paint event which draws a rectangle around the panel to highlight it.
Paint event-
private void deal_details_panel_Paint(object sender, PaintEventArgs e)
{
int thickness = 2;//it's up to you
int halfThickness = thickness / 2;
using (Pen p = new Pen(Color.GreenYellow, thickness))
{
e.Graphics.DrawRectangle(p, new Rectangle(halfThickness,
halfThickness,
deal_details_panel.ClientSize.Width - thickness,
deal_details_panel.ClientSize.Height - thickness));
}
}
Initially i dont want this rectangle to be shown, I have a button on which if i hover, only then i want to show this rectangle and on MouseLeave event i want to make it invisible again.
Button_Hover-
private void add_MouseHover(object sender, EventArgs e)
{
deal_details_panel.Invalidate();
}
I saw some answers which were to use Invalidate(), but, Invalidate() is not working for me.
Use Refresh() instead of Invalidate().
The Invalidate() method does not force repaint, it simply marks the control as "need repainting", leaving it up to the OS to repaint when needed.
The Refresh() method will force repainting immediately.
For more information, read Whats the difference between Control.Invalidate, Control.Update and Control.Refresh? blog post, where it clearly states:
Control.Invalidate( ) / Control.Invalidate(bool) / Control.Invalidate(Rectangle) / Control.Invalidate(Rectangle, bool) / Control.Invalidate(Region) / Control.Invalidate(Region, bool)
....
The important thing to note here is that these functions only “invalidate” or “dirty” the client area by adding it to the current update region of the window of the control. This invalidated region, along with all other areas in the update region, is marked for painting when the next WM_PAINT message is received. As a result you may not see your control refreshing (and showing the invalidation) immediately (or synchronously).
Update
In order for the panel to show the YellowGreen border only when the mouse is hovering over the button, you need to change your code a little bit.
First, add a bool field to your form, to indicate whether to draw the border or not, initialized to false:
private bool _drawBorder = false;
Then, change the paint method to take that value into consideration:
private void deal_details_panel_Paint(object sender, PaintEventArgs e)
{
if(_drawBorder)
{
int thickness = 2;//it's up to you
int halfThickness = thickness / 2;
using (Pen p = new Pen(Color.GreenYellow, thickness))
{
e.Graphics.DrawRectangle(p, new Rectangle(halfThickness,
halfThickness,
deal_details_panel.ClientSize.Width - thickness,
deal_details_panel.ClientSize.Height - thickness));
}
}
}
Finally, don't use the mouse_Hover event on the button, use MouseEnter and MouseLeave instead.
This is for two reasons - 1: Mouse_Hover have a built in delay and 2: using Mouse_Hover you don't get an indication when the mouse stops hovering over the button.
In the MouseEnter and MouseLeave event handlers, all you need to do is update the value of _drawBorder and call deal_details_panel.Refresh():
private void add_MouseEnter(object sender, EventArgs e)
{
_drawBorder = true;
deal_details_panel.Refresh();
}
private void add_MouseLeave(object sender, EventArgs e)
{
_drawBorder = false;
deal_details_panel.Refresh();
}
Zohar pointed correct solution but I would add a suggestion to change:
using (Pen p = new Pen(Color.GreenYellow, thickness))
to:
using (Pen p = new Pen(Color.Transparent, thickness))
This will set correct state from the start so no need to repaint.

How to pass EventArgs to a function

I have a function that will draw a image to a form.
private void DrawImage()
{
OpenFileDialog openfiledialog = new OpenFileDialog();
if (openfiledialog.ShowDialog() == DialogResult.OK)
{
Bitmap image = (Bitmap)Image.FromFile(openfiledialog.FileName, true);
TextureBrush texturebrush = new TextureBrush(image);
texturebrush.WrapMode = System.Drawing.Drawing2D.WrapMode.Tile;
Graphics formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(texturebrush, new RectangleF(90.0F, 110.0F, 00, 300));
formGraphics.Dispose();
}
}
But i will not draw any image and same code works if i write in button click event.
private void button1_Click(object sender, EventArgs e)
{
//Same code as written in DrawImage()
}
The problem I think is it needs "EventArgs e" in it. I read some where in msdn but not sure.No idea what is the role of EventArgs in drawing image on form.
Is there any way that I can achieve this functionality.
TL;DR
You don't have to. For your case, just ignore them. They may be, however, useful sometimes. Read full answer to understand.
Full answer
When dealing with events, you receive a generic EventArgs object and if you write a custom event, you may want to receive a custom implementation of EventArgs.
To make things simple, consider it a base class that holds event-related data.
So, for instance, if you are implementing a click event, as shown, you will receive two arguments: a source for the event and a holder for the event-related data.
Sometimes this is just useless. As on your sample. You don't have to receive it, nor use it. You may, however, perform some check on the source or the event data but that's not the goal of your code so both are just useless here.
As for me, I prefer to keep my methods apart from events, and call them from the event. I do place every event binding in a "group" on the bottom of the class to keep everything clean and readable. That's just a suggestion, you have to find your very own way of keeping code.
For the sake of this answer, here follows two samples:
1. Useless arguments
This sample just closes a form or window.
public void btn1_Click(object sender, EventArgs e) { Close(); }
2. Usefull arguments
Consider a component with a data grid of some type which fires a SelectedRowsChanged event with the id's (PK) of the selected rows.
// Event declaration. You can, after that, bind it elsewhere.
public event EventHandler<SelectionEventArgs> SelectedRowsChanged;
// This is the local implementation wich will fire the event.
// Here you invoke the event with the selected rows id's.
public void OnSelectedRowsChanged() { if (SelectedRowsChanged != null) CustomSelection(this, new SelectionEventArgs(this.SelectedRows)); }
// This is the custom implementation of the EventArgs to include the
// event-related data (row id)
public class SelectionEventArgs : EventArgs
{
public int[] SelectedRows{ get; private set; }
public SelectionEventArgs(int[] selectedRows) { SelectedRows = selectedRows; }
}
// ... then, somewhere else on your code
this.myControl.SelectedRowsChanged += myControl_SelectedRowsChanged;
public void myControl_SelectedRowsChanged(object sender, SelectionEventArgs e)
{
if (e.SelectedRows.Length > 0) { /* do something */ }
}
For the strong of heart, you may play a bit with lambda too. So, instead of:
this.button1.Click += button1_Click;
public void button1_Click(object sender, EventArgs e) { DoSomething(); }
you may have just this:
this.button1.Click += (s, e) => { DoSomething(); };
As on the first sample, event args are there ((s, e)) but they are just useless for the DoSomething method.
Lambdas are available in C# 5 (.NET 4.5) and above.
Sometimes it's easier to just do this in a form constructor or something alike.
Consider these tips to solve the problem:
If you use this.CreateGraphics for drawing on your form, then your drawing will disappear if the form refershes, for example if you minimize and restore it. You should put drawing logic in Paint event.
When refactoring your method, you should pass a parameter of Graphics type to your method and use it for drawing. Also you should not dispose passed parameter. You should pass e.Graphics in Paint event to your method.
In your method you should dispose your brush. Put it in a using block.
When refactoring your method, you should move the part of code which shows a dialog to out of your method. You should call it just when you need not in Paint event handler.
Preferably Don't use Image.FromFile to load image, it locks the file until the images disposes. Instead use Image.FromStream.
You are using a rectangle with 0 as Width. So if you draw even using current method, you will not see any result because of width of rectangle.
Code
Bitmap image;
private void DrawImage(Bitmap image, Graphics g, Rectangle r)
{
using (var brush = new TextureBrush(image))
{
brush.WrapMode = System.Drawing.Drawing2D.WrapMode.Tile;
g.FillRectangle(brush, r);
}
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
using (var s = new System.IO.FileStream(dialog.FileName, System.IO.FileMode.Open))
image = new Bitmap(Image.FromStream(s));
this.Invalidate();
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (image != null)
this.DrawImage(image, e.Graphics, new RectangleF(10, 10, 200, 200));
}

redrawing code in C#

i am creating a drawing programe which Suppose to take the parameters from the user
(Radius,height,width,....etc)
i have created a class with one paramter (radius)
public int faceoutline(int r)
{
Graphic = pictureBox1.CreateGraphics();
Graphic.DrawEllipse(myPen, 0, 0, r, r);
return r;
}
then i call it :
private void button1_Click(object sender, EventArgs e)
{
faceoutline(int.Parse(textBox1.Text));
pictureBox1.Invalidate();
}
....and nothing happens
i copied the button code to a timer but it keep balnking
WHAT I AM DOING WRONG ?!
You should place the drawing code in the Paint event handler of the PictureBox, and use the Graphics provided in the event args. This way, your custom drawing code will be executed every time the control is redrawn.
If you don't do it this way, anything you draw will disappear the next time the control is redrawn. In your code, you call Invalidate right after you draw your ellipse, so the control is redrawn without the ellipse...

C# Draw Line OnPaint() vs CreateGraphics()

Question:
How do you properly draw on a winform from a method other than the OnPaint() method?
Additional Information:
The code I have now draws some background lines for a TicTacToe game in the OnPaint() method. Then I use the Mouse_Click event and am running this code which apparently is not proper:
private void TicTacToe_MouseClick(object sender, MouseEventArgs e)
Graphics g = this.CreateGraphics();
g.DrawEllipse(this.penRed, this.Rectangle);
For reasons I do not understand, it does draw the circle, but when minimizing or moving the form off screen it erases the circles but not the lines from the OnPaint() method.
You are doing a lot of "view" but no "model".
When you want to create a shape, when the mouse button goes down/up, create some DATA representing the shape.
Your data structures represent the persistent information (it is the data that allows you to save and load this information between sessions).
All your paint function needs to do is look at the DATA structures and paint it. This will therefore persist between sizing/hiding/showing.
The problem is that Windows windows (that includes WinForms) have no graphical memory of their own unless their creator provides such memory and it is only a matter of time before that particular window wilk get overwritten or hidden and eventually need to be repainted. You're painting to the screen ( you might say ) and others can do the same. The only convention you can rely on is that the OnPaint will get called when needed. Basically it's alright to use your philosophy and draw whenever you need to (not on some misterious and unpredictable schedule). For that check out my solution.
You should use a "backbuffer bitmap" like:
private Bitmap bb;
protected override void OnResize(EventArgs e) {
this.bb = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
this.InitBackBuffer();
}
private void InitBackBuffer() {
using (var g = Graphics.FromImage(this.bb)) {
// do any of the "non dissapearing line" drawing here
}
}
private void TicTacToe_MouseClick(object sender, MouseEventArgs e)
using (Graphics g = Graphics.FromImage(this.bb))
g.DrawEllipse(this.penRed, this.Rectangle);
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
e.Graphics.DrawImageUnscaled(this.bb);
}
Try that. That should do it :)
What you are doing is drawing on the form "asynchronously" (from the OnPaint method). You see, the OnPaint method is what Windows Forms relies on to draw your entire form. When something happens to your From, it is invalidated and OnPaint is called again. If something isn't drawn in that method, then it will not be there after that happens.
If you want a button to trigger something to appear permanently then what you need to do is Add that object to a collection somewhere, or set a variable related to it. Then call Refresh() which calls Invalidate() and Update() Then, during OnPaint, draw that object (ellipis).
If you want it to still be there after something happens to your form, such as minimize, you have to draw it during OnPaint.
Here's my suggestion:
public partial class Form1 : Form
{
Rectangle r = Rectangle.Empty;
Pen redPen = new Pen(Color.Red);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
r = new Rectangle(50, 50, 100, 100);
Refresh();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (r != Rectangle.Empty)
{
e.Graphics.DrawRectangle(redPen, r);
}
}
}

Categories

Resources