new to .net, need to draw a line on form

Code A
05-09-2006, 10:15 AM
I have a very simple question (I hope) and I actually feel silly asking it b/c I'm sure I am just missing something obvious.

How can I draw a line on my form at design time in .net? In 6.0 there were shape/line features that allowed you to draw on your form at design time. Are these features still available in .net?

anthony_n
05-09-2006, 10:33 AM
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.DrawLine(new System.Drawing.Pen(Color.Black,2),0,0,250,250);

}


let you convert it to vb ;)

Code A
05-09-2006, 11:18 AM
Thank You for the quick response. I am actually trying to do in Visual C++, but am more familiar with VB so I thought i would inquire there first.

I have converted your code to c++ (i think), but I am getting nothing. Any ideas?
private: void Form1_OnPaint(System::Object * sender, System::Windows::Forms::PaintEventArgs * e)
{

e->Graphics->DrawLine(gcnew System::Drawing::Pen(Color::Black),0,0,250,250);
}
Additionallly, whenever I use gcnew I get an error that it is undefined, what do I need to do to make VS recognize it?

jo0ls
05-09-2006, 01:00 PM
Try in the sister forum for other languages ( http://www.xtremedotnettalk.com/ )
I'm only on chapter 5 of c++ for noobs, but this works:

private: System::Void Form1_Paint( Object^ /*sender*/, System::Windows::Forms::PaintEventArgs^ e )
{
e->Graphics->DrawLine(gcnew Pen(Color::DeepSkyBlue) ,0,0,100,100);
}


private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
this->Paint += gcnew System::Windows::Forms::PaintEventHandler( this, &Form1::Form1_Paint );
}

Actually, if gcnew is coming up undefined, then are you starting a CLR windows application? gcnew is like the older new statement but creates objects in the CLR managed heap. And you use tracking handles ^, with the CLR as it may move the objects around in memory.

Code A
05-09-2006, 01:39 PM
Thanks for the response, but now you have me confused :)

I started a Visual C++ Windows Forms Application. What type of project do I need to start in order to use gcnew? All I am trying to do is display a form with a button, and to draw a line when a button is clicked.

I appreciate any insight.

jo0ls
05-09-2006, 01:52 PM
I think we may have different VS versions, i'm learning 2005, hence all the rubbish just spoken... ( ^ doesn't exist in 2003, and they aren't called CLR projects either)
gcnew is new in 2005.

Code A
05-09-2006, 02:00 PM
so does that mean I should use new instead of gcnew? this is confusing me b/c all examples I find use gcnew instead of new and the ^ instead of *. :confused:

Here is my current code (which doesn't work):
private: System::Void Form1_Paint( Object* sender, System::Windows::Forms::PaintEventArgs* e )
{
e->Graphics->DrawLine(new Pen(Color::DeepSkyBlue) ,0,0,100,100);
}


private: System::Void Form1_Load(System::Object* sender, System::EventArgs* e)
{
this->Paint += new System::Windows::Forms::PaintEventHandler(this, &Form1::Form1_Paint );
}
any ideas. and yes I am using VS 2003.

Thanks!

jo0ls
05-09-2006, 03:06 PM
That works for me with 2003, pasted into in a new .Net windows forms application.

Code A
05-09-2006, 03:29 PM
strange. i wonder what is causing mine to not work :confused:

My app compiles fine and runs, but it doesn't draw a line on the form at run time.

So all you did was create a new Visual C++ Windows Forms App, pasted my code right in the form and when you ran the app, it drew a line on your form?

jo0ls
05-09-2006, 04:33 PM
yeah just that. Try the attached, it's got a bit more drawing and I added the resize event to trigger a redraw when the form resizes. You'd be better ask in the other forum if you are still stuck, or this thread will end up in the bin...

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum