473,385 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Drawing a line using Graphics class

I created a Windows Form Project using VS.net.
I'm trying to draw a line between two points.
(Plotting a graph, dynamically)
This is the code I have so far:
What do I have to do to actually see the line painted on
my form?
private: System::Void button1_Click(System::Object *
sender, System::EventArgs * e)
{
Graphics* g;
Pen* myPen = new Pen(Color::Red);
myPen->Width = 5;
g->DrawLine(myPen, 1, 1, 45, 65);
}
Nov 16 '05 #1
2 1918
You are going to need a pointer to the graphics context of your form.
Typically you might want to use something like the Paint event, it gets
called any time your form needs to be redrawn for any reason - you could
call the Paint eventHandler from the button click function.

void frmPaint(Object * sender, System::Windows::Forms::PaintEventArgs * e)

{

Graphics * grfx = e->Graphics;

grfx->DrawLine(myPen, 1, 1, 45, 65);

}

Add something like this to your form constructor to tie the forms Paint
event to the frmPaint function.

this->add_Paint(new PaintEventHandler(this, frmPaint));

Adios,

January

"Coralin Feierbach" <an*******@discussions.microsoft.com> wrote in message
news:02****************************@phx.gbl...
I created a Windows Form Project using VS.net.
I'm trying to draw a line between two points.
(Plotting a graph, dynamically)
This is the code I have so far:
What do I have to do to actually see the line painted on
my form?
private: System::Void button1_Click(System::Object *
sender, System::EventArgs * e)
{
Graphics* g;
Pen* myPen = new Pen(Color::Red);
myPen->Width = 5;
g->DrawLine(myPen, 1, 1, 45, 65);
}

Nov 16 '05 #2
"Coralin Feierbach" <an*******@discussions.microsoft.com> wrote in
news:02****************************@phx.gbl:
private: System::Void button1_Click(System::Object *
sender, System::EventArgs * e)
{
Graphics* g;
Pen* myPen = new Pen(Color::Red);
myPen->Width = 5;
g->DrawLine(myPen, 1, 1, 45, 65);
}


in your example, replace with :
//
Graphics* g = this->CreateGraphics() ;
//
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Steve Long | last post by:
Hello, (total GDI newbie) I'm having trouble drawing just a simple line to display in a picturebox. I just want a straight, dotdash line. I have two methods, one works and one doesn't (it cause...
4
by: Stuart Norris | last post by:
Dear Readers, I am attempting to draw box around some text using unicode on multiline label. The label is forty characters wide and 12 lines deep. I have been trying to draw a box around text...
4
by: Peter Oliphant | last post by:
There doesn't seem to be any documentation on how to create and/or use an instance of System::Drawing::Graphics. On-line MSDN talks about this class, and says: " The Graphics class provides...
13
by: Metallicraft | last post by:
I have a vb6 application. On the main form is a picture box with one or two images and several pieces of text displayed in it. These are created on the fly using gdi32 routines that are all in a...
9
by: davetelling | last post by:
I am not a programmer, I'm an engineer trying to make an interface to a product I'm designing. I have used C# to make a form that interrogates the unit via the serial port and receives the data. I...
1
by: lab3terch | last post by:
Imports System.Drawing.Graphics Public Class Form1 Inherits System.Windows.Forms.Form Dim x1, y1, x2, y2 As Integer Dim red, black, blue, green, orange As Color Dim x, y, w, h...
11
by: dongarbage | last post by:
Hi there, I'm very much a C# novice. How do you do freehand drawing on a panel with a mouse in c#? Thanks, Don
5
by: Macias | last post by:
Hi, Please help me, how I can make a line drawing on PictureBox similar to MS paint. I thinking about this: click and hold button, move mouse and relase button. I'm trying useing this...
0
by: Chris Dunaway | last post by:
On May 8, 1:11 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com> wrote: I'm afraid that I can't offer anything more than what you have learned on your own. I only suggested that class because I...
7
by: raylopez99 | last post by:
I have a logical drawing space much bigger than the viewport (the screen) and I'd like to center the viewport (the screen) to be at the center of the logical drawing space. After following the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.