Click here to Skip to main content
15,889,281 members
Home / Discussions / C#
   

C#

 
GeneralRe: Help ploting Excel graph in C# Pin
Mc_Topaz31-May-10 2:29
Mc_Topaz31-May-10 2:29 
GeneralRe: Help ploting Excel graph in C# Pin
Henry Minute31-May-10 2:51
Henry Minute31-May-10 2:51 
Questiondarw rectangle modification.... [modified] Pin
Nivas8230-May-10 22:42
Nivas8230-May-10 22:42 
AnswerRe: darw rectangle modification.... Pin
OriginalGriff30-May-10 23:00
mveOriginalGriff30-May-10 23:00 
GeneralRe: darw rectangle modification.... Pin
Nivas8231-May-10 18:53
Nivas8231-May-10 18:53 
GeneralRe: darw rectangle modification.... Pin
OriginalGriff31-May-10 22:13
mveOriginalGriff31-May-10 22:13 
GeneralRe: darw rectangle modification.... Pin
Nivas821-Jun-10 22:17
Nivas821-Jun-10 22:17 
GeneralRe: darw rectangle modification.... Pin
OriginalGriff1-Jun-10 22:59
mveOriginalGriff1-Jun-10 22:59 
Well done - it looks better already!

I still wouldn't draw onto your bitmap in the Paint event - don't forget the bitmap is persistent - all changes you make are kept (unlike painting on the provided graphics context in the Paint event). For the moment, just comment it out and ignore it.

You are handling the new rectangle creation a little oddly - MouseDown and MouseClick are not the events I would pick! The normal way to do it is:
MouseDown: Save the current location - you do this - but in a Point rather than a rectangle. Set a Bool to true to show you are creating a new rectangle.
MouseUp: If creating new rectangle, make new rectangle using saved point, and cancel creating mode. Add the new rectangle to the list.
You can also add MouseMove: If creating, save the current position as a new, temporary rectangle. Invalidate.
Then in Paint: if creating, paint an "elastic band" for the temporary rectangle.

I have given some sample code for this below (it is not compiled, so I can't say if it works!)

private void StoreLayoutPanel_MouseDown(object sender, MouseEventArgs e)
    {
    mouseDown = true;
    mouseDownAt = new Point(e.X, e.Y);
    Invalidate();
    }

private void StoreLayoutPanel_MouseUp(object sender, MouseEventArgs e)
    {
    if (mouseDown)
       {
       Rectangle rect = MakeRect(mouseDownAt.X, mouseDownAt.Y, e.X, e.Y);
       rectangles.Add(rect);
       Invalidate();
       }
    mouseDown = false;
    }

private void StoreLayoutPanel_MouseMove(object sender, MouseEventArgs e)
    {
    if (mouseDown)
        {
        mouseRect = MakeRect(mouseDownAt.X, e.X, mouseDownAt.Y, e.Y);
        Invalidate();
        }
    }

private void StoreLayoutPanel_Paint(object sender, PaintEventArgs e)
    {
    Pen pen = new Pen(Color.Black);
    foreach (Rectangle r in rectangles)
        {
        e.Graphics.DrawRectangle(pen, r);
        }
    if (mouseDown)
        {
        Pen p = new Pen(Color.Gray);
        p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
        e.Graphics.DrawRectangle(p, mouseRect);
        }
    }



Here are a couple of methods you may find usefull:
/// <summary>
/// Make a rectangle from two points
/// The rectangle will use the smallest X and the smallest Y as the TLHC, and
/// the largest X and the largest Y as the BRHC
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
private Rectangle MakeRect(Point p1, Point p2)
    {
    return MakeRect(p1.X, p2.X, p1.Y, p2.Y);
    }
/// <summary>
/// Make a rectangle from two points
/// The rectangle will use the smallest X and the smallest Y as the TLHC, and
/// the largest X and the largest Y as the BRHC
/// </summary>
/// <param name="X1"></param>
/// <param name="X2"></param>
/// <param name="Y1"></param>
/// <param name="Y2"></param>
/// <returns></returns>
private Rectangle MakeRect(int X1, int X2, int Y1, int Y2)
    {
    int XL = Math.Min(X1, X2);
    int XR = Math.Max(X1, X2);
    int YL = Math.Min(Y1, Y2);
    int YR = Math.Max(Y1, Y2);
    return new Rectangle(XL, YL, XR - XL, YR - YL);
    }
#endregion

Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

AnswerRe: darw rectangle modification.... [modified] Pin
Nivas822-Jun-10 19:18
Nivas822-Jun-10 19:18 
GeneralRe: darw rectangle modification.... Pin
Nivas827-Jun-10 21:39
Nivas827-Jun-10 21:39 
AnswerRe: darw rectangle modification.... Pin
Luc Pattyn31-May-10 1:57
sitebuilderLuc Pattyn31-May-10 1:57 
GeneralRe: darw rectangle modification.... Pin
Nivas8231-May-10 19:02
Nivas8231-May-10 19:02 
GeneralRe: darw rectangle modification.... Pin
Luc Pattyn1-Jun-10 1:48
sitebuilderLuc Pattyn1-Jun-10 1:48 
QuestionA Question Of Focus and Tab Indices [Solved] Pin
Roger Wright30-May-10 22:20
professionalRoger Wright30-May-10 22:20 
AnswerRe: A Question Of Focus and Tab Indices Pin
Henry Minute30-May-10 23:10
Henry Minute30-May-10 23:10 
GeneralRe: A Question Of Focus and Tab Indices [modified] Pin
Roger Wright30-May-10 23:20
professionalRoger Wright30-May-10 23:20 
GeneralRe: A Question Of Focus and Tab Indices Pin
OriginalGriff30-May-10 23:27
mveOriginalGriff30-May-10 23:27 
GeneralRe: A Question Of Focus and Tab Indices Pin
Henry Minute30-May-10 23:30
Henry Minute30-May-10 23:30 
GeneralRe: A Question Of Focus and Tab Indices Pin
Luc Pattyn31-May-10 2:02
sitebuilderLuc Pattyn31-May-10 2:02 
Questiondeclare string[] Pin
jojoba201130-May-10 20:31
jojoba201130-May-10 20:31 
AnswerRe: declare string[] Pin
Vikram A Punathambekar30-May-10 20:34
Vikram A Punathambekar30-May-10 20:34 
AnswerRe: declare string[] Pin
Abhinav S30-May-10 20:43
Abhinav S30-May-10 20:43 
QuestionRe: declare string[] Pin
jojoba201130-May-10 20:52
jojoba201130-May-10 20:52 
AnswerRe: declare string[] Pin
Luc Pattyn31-May-10 2:04
sitebuilderLuc Pattyn31-May-10 2:04 
AnswerRe: declare string[] Pin
Anıl Yıldız31-May-10 11:37
Anıl Yıldız31-May-10 11:37 

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.