Click here to Skip to main content
15,900,108 members
Home / Discussions / C#
   

C#

 
GeneralRe: Showing Main Form Quickly? Pin
OriginalGriff11-Jul-10 3:46
mveOriginalGriff11-Jul-10 3:46 
AnswerRe: Showing Main Form Quickly? Pin
Pete O'Hanlon10-Jul-10 21:46
mvePete O'Hanlon10-Jul-10 21:46 
QuestionMessage Removed Pin
10-Jul-10 4:31
isprog110-Jul-10 4:31 
AnswerRe: Graphics Object Using text box for x & y axis. Pin
OriginalGriff10-Jul-10 4:44
mveOriginalGriff10-Jul-10 4:44 
GeneralMessage Removed Pin
10-Jul-10 6:12
isprog110-Jul-10 6:12 
GeneralRe: Graphics Object Using text box for x & y axis. Pin
OriginalGriff10-Jul-10 10:22
mveOriginalGriff10-Jul-10 10:22 
GeneralMessage Removed Pin
10-Jul-10 21:02
isprog110-Jul-10 21:02 
GeneralRe: Graphics Object Using text box for x & y axis. [modified] Pin
OriginalGriff10-Jul-10 21:26
mveOriginalGriff10-Jul-10 21:26 
Not too bad - it works! There are a couple of things you should look at:
1) If you must use CreateGraphics (and you should avoid it if you can), then you must Dispose it. Graphics handles are scarce resources, and should not be created willy-nilly. The easiest way is:
Pen gPen;
SolidBrush nBrush;

using (Graphics fx = CreateGraphics())
    {
    gPen = new Pen(Color.Green);
    nBrush = new SolidBrush(Color.Blue);

    float x = 0, y = 0;
    x = float.Parse(textBox1.Text);
    y = float.Parse(textBox2.Text);

    fx.Clear(Color.Black);
    fx.DrawLine(gPen, 200 + x, y, 0 + x, 100 + y);
    fx.DrawLine(gPen, 200 + x, y, 400 + x, 100 + y);
    fx.DrawRectangle(gPen, x, 100 + y, 400, 200);
    fx.DrawRectangle(gPen, 25 + x, 175 + y, 150, 125);
    fx.DrawEllipse(gPen, 150 + x, 225 + y, 10, 10);
    fx.DrawRectangle(gPen, 250 + x, 150 + y, 100, 100);
    fx.DrawLine(gPen, 250 + x, 200 + y, 300 + x, 200 + y);
    fx.DrawLine(gPen, 300 + x, 150 + y, 300 + x, 250 + y);
    }

2) You don't need to use floats - all the screen coordinates are int, so use those instead.

3) Please don't use Parse on textboxes (or other user input) - it will throw an exception when it fails to convert. For example, when the user empties the textbox, or types an alphabetic character. Use TryParse instead:
using (Graphics fx = CreateGraphics())
    {
    gPen = new Pen(Color.Green);
    nBrush = new SolidBrush(Color.Blue);

    int x = 0, y = 0;
    if (int.TryParse(textBox1.Text, out x) && int.TryParse(textBox2.Text, out y))
        {
        fx.Clear(Color.Black);
        ...
        }
    }
TryParse never throws an exception, it returns true or false to say the conversion worked.

4) Avoid drawing everywhere (and don't create Graphics objects): draw in the Paint event. In the form designer, add a panel to your form and make it fit the available space (you can also change the Anchor properties to make it resize with the form). Double click the panel, and it will create a Paint event, and show you the code. Make it look like this:
private void panel1_Paint(object sender, PaintEventArgs e)
    {
    Graphics fx = e.Graphics;
    Pen gPen = new Pen(Color.Green);

    int x = 0, y = 0;
    // Don't check if the user values are valid.
    // Bad entries use the defaults above.
    int.TryParse(textBox1.Text, out x);
    int.TryParse(textBox2.Text, out y);
    fx.Clear(Color.Black);
    fx.DrawLine(gPen, 200 + x, y, 0 + x, 100 + y);
    fx.DrawLine(gPen, 200 + x, y, 400 + x, 100 + y);
    fx.DrawRectangle(gPen, x, 100 + y, 400, 200);
    fx.DrawRectangle(gPen, 25 + x, 175 + y, 150, 125);
    fx.DrawEllipse(gPen, 150 + x, 225 + y, 10, 10);
    fx.DrawRectangle(gPen, 250 + x, 150 + y, 100, 100);
    fx.DrawLine(gPen, 250 + x, 200 + y, 300 + x, 200 + y);
    fx.DrawLine(gPen, 300 + x, 150 + y, 300 + x, 250 + y);
    }
Because you don't create the Graphics object, you are not responsible for disposal.
Now all you need to do is create TextChanged handlers for each of the TextBoxes (by double clicking in the designer) and add one line to each:
panel1.Invalidate();

Now whenever the system needs to, your house will be drawn in the new position.

[edit]I really should check code before I post it: TryParse uses out parameters...
Sorry![/edit]

[edit#2]It works much nicer if you invalidate the panel rather than the whole form: I should run code before posting it too...[/edit#2]
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

modified on Sunday, July 11, 2010 3:34 AM

GeneralRe: Graphics Object Using text box for x & y axis. Pin
isprog110-Jul-10 21:40
isprog110-Jul-10 21:40 
GeneralRe: Graphics Object Using text box for x & y axis. Pin
OriginalGriff10-Jul-10 21:59
mveOriginalGriff10-Jul-10 21:59 
GeneralRe: Graphics Object Using text box for x & y axis. Pin
OriginalGriff10-Jul-10 23:35
mveOriginalGriff10-Jul-10 23:35 
GeneralRe: Graphics Object Using text box for x & y axis. Pin
Luc Pattyn11-Jul-10 6:04
sitebuilderLuc Pattyn11-Jul-10 6:04 
AnswerRe: Graphics Object Using text box for x & y axis. Pin
Roger Wright10-Jul-10 4:59
professionalRoger Wright10-Jul-10 4:59 
AnswerRe: Graphics Object Using text box for x & y axis. Pin
Luc Pattyn10-Jul-10 8:40
sitebuilderLuc Pattyn10-Jul-10 8:40 
Questionconcatenation of multiple value [modified] Pin
varsh1210-Jul-10 1:18
varsh1210-Jul-10 1:18 
AnswerRe: concatenation of multiple value Pin
Luc Pattyn10-Jul-10 1:24
sitebuilderLuc Pattyn10-Jul-10 1:24 
AnswerRe: concatenation of multiple value Pin
OriginalGriff10-Jul-10 1:56
mveOriginalGriff10-Jul-10 1:56 
GeneralRe: concatenation of multiple value Pin
DaveyM6910-Jul-10 2:04
professionalDaveyM6910-Jul-10 2:04 
GeneralRe: concatenation of multiple value Pin
OriginalGriff10-Jul-10 2:07
mveOriginalGriff10-Jul-10 2:07 
AnswerRe: concatenation of multiple value Pin
DaveyM6910-Jul-10 2:03
professionalDaveyM6910-Jul-10 2:03 
AnswerRe: concatenation of multiple value Pin
PIEBALDconsult10-Jul-10 3:56
mvePIEBALDconsult10-Jul-10 3:56 
AnswerRe: concatenation of multiple value Pin
Abhinav S10-Jul-10 5:17
Abhinav S10-Jul-10 5:17 
QuestionHow to mearge 2 column in dagagridview in C# application?? [modified] Pin
Sunil G9-Jul-10 20:53
Sunil G9-Jul-10 20:53 
AnswerRe: How to set Image in particular cell in DataGridView in C# Pin
Abhinav S9-Jul-10 21:01
Abhinav S9-Jul-10 21:01 
GeneralRe: How to set Image in particular cell in DataGridView in C# Pin
Sunil G9-Jul-10 21:18
Sunil G9-Jul-10 21:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.