I want to do a simple thing: create windows form with a button and get line drawn when I click button. So far no luck with button click event, e.g. how do I make that line be drawn via private void button1_Click(object sender, EventArgs e)?
Is there a more appropriate beginners forum or I can post here?
thanks
Jonas
namespace Example1_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.BackColor = Color.White;
}
public void OnPaint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.PageUnit = GraphicsUnit.Inch;
Pen blackPen = new Pen(Color.Black, 5 / g.DpiX);
g.DrawLine(blackPen, 0, 0, 1, 1);
}
private void button1_Click(object sender, EventArgs e)
{
Application.Run(new Onpaint());
}
}
}
Loading
Jonas BaltrusaitisPosted Feb 18, 2011, 7:37 PM
I went on to create a bit more complicated example. It works, but for some reason in the middle of debuggin it throws out this Parameter is not valid exception and I just have no idea why. could you take a look at it? The problem is in Program.cs for some reason with the problematic line being Application.Run(new Form1());
Below is Form1.cs
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 Example1_2
{
public partial class Form1 : Form
{
//Define the drawing area
private Rectangle PlotArea;
//Unit defined in world coordinate system:
private float xMin = 0f;
private float xMax = 10f;
private float yMin = 0f;
private float yMax = 10f;
//Define the offset in pixel:
private int offset = 30;
private bool m_bPressed;
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.BackColor = Color.White;
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
//Calculate the location and size of the drawing area
//within which we want to draw the graphics:
Rectangle rect = ClientRectangle;
PlotArea = new Rectangle(rect.Location, rect.Size);
PlotArea.Inflate(-offset, -offset);
//Draw ClientRectangle and PlotArea using Pen:
g.DrawRectangle(Pens.Red, rect);
g.DrawRectangle(Pens.Black, PlotArea);
if (m_bPressed)
{
//draw a line from point (3,2) to point (6,7)
//using a pen with a width of 3 pixels:
Pen aPen = new Pen(Color.Green, 3);
g.DrawLine(aPen, Point2D(new PointF(3, 2)), Point2D(new PointF(6, 7)));
aPen.Dispose();
g.Dispose();
}
}
private PointF Point2D(PointF ptf)
{
PointF aPoint = new PointF();
aPoint.X=PlotArea.X +(ptf.X-xMin)*
PlotArea.Width/(xMax - xMin);
aPoint.Y=PlotArea.Bottom-(ptf.Y-yMin)*
PlotArea.Height/(yMax-yMin);
return aPoint;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
m_bPressed = true;
Invalidate();
}
}
}
Mike GoldPosted Feb 17, 2011, 9:29 PM
Hi Jonas,
glad it worked for you finally. Your solution is correct.
-Mike
Jonas BaltrusaitisPosted Feb 17, 2011, 9:10 PM
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 Example1_1
{
public partial class Form1 : Form
{
private bool m_bPressed;
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.BackColor = Color.White;
}
private void Form1_Load(object sender, EventArgs e)
{
}
protected override void OnPaint(PaintEventArgs e)
{
if (m_bPressed)
{
Graphics g = e.Graphics;
g.PageUnit = GraphicsUnit.Inch;
Pen blackPen = new Pen(Color.Black, 4 / g.DpiX);
g.DrawLine(blackPen, 0, 0, 1, 1);
}
}
private void button1_Click(object sender, EventArgs e)
{
m_bPressed = true;
Invalidate();
}
}
}
Jonas BaltrusaitisPosted Feb 17, 2011, 8:51 PM
Then I deleted everything from
public partial class Form1 : Form
down and paste your code, I am getting compilation error
Error 1 'Example1_13.Form1' does not contain a definition for 'Form1_Load' and no extension method 'Form1_Load' accepting a first argument of type 'Example1_13.Form1' could be found (are you missing a using directive or an assembly reference?) c:\documents and settings\jonas\my documents\visual studio 2010\Projects\Example1_13\Example1_13\Form1.Designer.cs 40 55 Example1_13
this paqrticularly I don;t understand as my Form1 : Form didn;t change names, why wouldn;t it find it all of a sudden?..
Mike GoldPosted Feb 16, 2011, 11:38 PM
I omitted nothing obvious, this code was pasted from an app I made specifically to illustrate how to do this. I ran the code before I sent it to you, so I know it works. It draws a red line from the upper left hand corner of the screen to the bottom after you press the button. I did not, however, paste the whole project, but I'll list the steps to recreate it.
1. Go to New Projects and choose window forms application
2. REplace the Form class generated with the class I sent you above.
3. compile and run
4. press the button
5. see the diagnol red line appear from upper left to bottom right corner of the form
Jonas BaltrusaitisPosted Feb 16, 2011, 9:58 PM
Mike GoldPosted Feb 16, 2011, 9:37 PM
namespace LineOnButtonPress
{
public partial class Form1 : Form
{
private bool m_bPressed;
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (m_bPressed)
{
e.Graphics.DrawLine(Pens.Red, 0,0, ClientRectangle.Width, ClientRectangle.Height);
}
}
private void button1_Click(object sender, EventArgs e)
{
m_bPressed = true;
Invalidate();
}
}
}