473,385 Members | 1,934 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 simple triangle in C#? Help!

Paul Johnson
Hi,

I have a winform and am trying to just draw a triangle on it. I don't want it inside of an event.

Currently, the code I have is just this

Expand|Select|Wrap|Line Numbers
  1.     public partial class Triangles : Form
  2.     {
  3.         public Triangles()
  4.         {
  5.             InitializeComponent();
  6.             drawTriangle();
  7.         }
  8.  
  9.         private void drawTriangle()
  10.         {
  11.             Graphics surface;
  12.             surface = this.CreateGraphics();
  13.             SolidBrush brush = new SolidBrush(Color.Blue);
  14.             Point[] points = {new Point(10, 10), new Point(100, 10), new Point(50, 100) };
  15.             surface.FillPolygon(brush, points);
  16.         }
  17.     }
  18.  
Nothing draws. I've added an invalidate to the end incase that was the hitch, but still, nada.

Could any kind soul please point the way on this? Line and filled polygons are something I've never needed to play with - until now...

Thanks

Paul
Oct 1 '11 #1

✓ answered by GaryTexmo

You say you don't want your draw inside an event, but you kind of have to if you want it to persist on your form. WinForms uses events to control when an object gets drawn, it uses this so it knows to repaint after a window moves, something obscures it, or it becomes visible.

If you don't want this, you could probably get away with what you're doing if you draw and your window doesn't get repainted, but you don't have a lot of control over that. You can manually trigger a repaint, and you can stop them from occurring at all, but other than that windows will paint when it feels it needs to.

With the code you have here, you're doing your paint in the constructor before your form ever draws. So your triangle will disappear as soon as your form shows. If you are dead set against using the proper painting methods, try putting your triangle draw in the Load event on your form, this might happen after the last refresh but I'm not sure. Another alternative is to have it paint in response to a button click.

The best way is to just override the Paint event and do all your drawing in there. If you need that paint to occur repeatedly (ie, for animation), just put a timer on your form and call the form's Invalidate method to trigger a redraw.

If you have any questions or need help getting the code going, please feel free to ask more questions :)

2 29256
GaryTexmo
1,501 Expert 1GB
You say you don't want your draw inside an event, but you kind of have to if you want it to persist on your form. WinForms uses events to control when an object gets drawn, it uses this so it knows to repaint after a window moves, something obscures it, or it becomes visible.

If you don't want this, you could probably get away with what you're doing if you draw and your window doesn't get repainted, but you don't have a lot of control over that. You can manually trigger a repaint, and you can stop them from occurring at all, but other than that windows will paint when it feels it needs to.

With the code you have here, you're doing your paint in the constructor before your form ever draws. So your triangle will disappear as soon as your form shows. If you are dead set against using the proper painting methods, try putting your triangle draw in the Load event on your form, this might happen after the last refresh but I'm not sure. Another alternative is to have it paint in response to a button click.

The best way is to just override the Paint event and do all your drawing in there. If you need that paint to occur repeatedly (ie, for animation), just put a timer on your form and call the form's Invalidate method to trigger a redraw.

If you have any questions or need help getting the code going, please feel free to ask more questions :)
Oct 3 '11 #2
Hi Gary,

Thanks. I've done as you've suggested and life is good :)

Paul
Oct 7 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: MX1 | last post by:
Help? This is simple but I'm drawing a blank. Have a very small table, but need specific data out of it. Here is the structure: ClientID InvDate ItemAmt 001 3/7/03 $10...
0
by: lottoman | last post by:
Hello all, I am trying to draw a triangle on a toolbarbutton to simulate a drop down button without having the extra button beside my drop downbutton. I was successfully able to draw the...
11
by: darrel | last post by:
I'm modyfying an image upload and resizing script that we've had laying around for a long while. I'm getting a NullReferenceException error (see full error at bottom) from this line: Dim g...
11
by: samuelberthelot | last post by:
Hi, I've got 3 input HTML (dropdown lists) on my page. One for selecting a Month, one for the day, one for the year. Very simple... My problem is that I'd like to update the Days one according...
7
by: sathyashrayan | last post by:
Dear group, I tryied to impliment a simple GCD algo, just print the common divisors between two numbers. The simple task I was trying with various method and till now I could not able to...
5
by: sugarflaps | last post by:
Hi im needing a wee bit of help creating a series of triangles that get bigger each time the user has to input the number of triangles they want. I have managed to get my code to produce one triange...
1
by: halo combat22 | last post by:
I want to know if it is possible to make the design below in a picture box with loops instead of just writing.... picture1.print "*****" picture1.print "****" picture1.print "***" etc... ...
6
by: jackj | last post by:
Hi, I am first time C++ student and doing the usual tasks. This one is to create a triangle based on user input of how large (how many rows) and what symbol to use. I have managed to create a...
4
by: milk242 | last post by:
I am trying to figure out how to print a triangle in the shape of this depending on the user input. If they input 4 the max height would be 4. * ** *** **** *** ** * I have no idea how to...
1
by: lindafay1123 | last post by:
Hello, I have been tring to write the visual basic program that will out put a triangle. A program the requests an odd number and start the triangle with that number and go down with odd numbers...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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.