Click here to Skip to main content
16,005,697 members
Home / Discussions / C#
   

C#

 
AnswerRe: Switch, case? Pin
0x3c021-Aug-09 6:00
0x3c021-Aug-09 6:00 
GeneralRe: Switch, case? Pin
nhqlbaislwfiikqraqnm21-Aug-09 6:10
nhqlbaislwfiikqraqnm21-Aug-09 6:10 
AnswerRe: Switch, case? Pin
Ennis Ray Lynch, Jr.21-Aug-09 6:10
Ennis Ray Lynch, Jr.21-Aug-09 6:10 
GeneralRe: Switch, case? Pin
nhqlbaislwfiikqraqnm21-Aug-09 6:21
nhqlbaislwfiikqraqnm21-Aug-09 6:21 
QuestionHow to store and retrieve ZIP files from oracle 10g. ZIP files consist of heavy satellite images. Pin
Antim Singh Parmar21-Aug-09 5:47
Antim Singh Parmar21-Aug-09 5:47 
AnswerRe: How to store and retrieve ZIP files from oracle 10g. ZIP files consist of heavy satellite images. Pin
Hristo-Bojilov21-Aug-09 8:43
Hristo-Bojilov21-Aug-09 8:43 
Questiondrawing multi line Pin
mgroses21-Aug-09 5:45
mgroses21-Aug-09 5:45 
AnswerRe: drawing multi line [modified] Pin
Henry Minute21-Aug-09 6:46
Henry Minute21-Aug-09 6:46 
There are several problems with the code that you have posted.

1) you should be aware that panel1_Paint happens every time that something happens to your form, which can be several times a second.
2) because of 1) you are creating two new PictureBoxes (PictureBox2 and PictureBox1) several times a second. This will make your code very, very slow if MaxRows is a large number. Since PictureBox2 is always in the same place, always the same size and always contains the same image why not create it once in the Form.Load() event handler, or better than that in the designer. If you are worried about it being in the correct place when your Form is resized, use the Anchor property.

The parameter not valid refers to the Graphics object(g). Delete the g.Dispose() entirely. YOU DIDN'T CREATE IT, THEREFORE YOU MUSTN'T DISPOSE IT.
You could also move the Pen myPen = new Pen(Color.Black, 1); outside the for loop, put it on the line above the for statement, and move the myPen.Dispose() call to after the closing brace of the for loop. No need to create it and then destroy it each time through the loop. Create it before the loop starts, then destroy it after the loop completes.

[Edit]
I have just realized that the problems with your code are worse than I noticed after my first brief reading of it.
this section of code, from inside the for loop:
PictureBox PictureBox1 = new PictureBox();
PictureBox1.Image = Image.FromFile(@"C:\retailer.bmp");
...........
...........
...........
...........
PictureBox1.Location = new Point(x1 + x, y1 - y);
panel1.Controls.Add(PictureBox1);         //<=========================== PB is added to panel1s Controls Collection


means that the first time your Form is painted (assuming that MaxRows is 10, just for discussiion), there will be 11 PictureBoxes in panel1. The next time it is painted there will be 22 PictureBoxes stacked 2 deep in the same locations, the next time 33 PictureBoxes and so on. All stacked one on top of the other. You can mitigate this partly by turning off drawLine as the last statement of the:
if (drawLine == true)
{
  PictureBox PictureBox2 = new PictureBox();
  int x1 = panel1.Width / 2;
  int y1 = panel1.Height / 2;
  PictureBox2.Location = new Point(x1, y1);
  PictureBox2.Size = new System.Drawing.Size(50, 30);
  PictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;
  panel1.Controls.Add(PictureBox2);

  PictureBox2.Image = Image.FromFile(@"C:\warehouse.bmp");


    Pen myPen = new Pen(Color.Black, 1);
    for (int i = 0; i < MaxRows; i++)
    {

        PictureBox PictureBox1 = new PictureBox();
        PictureBox1.Image = Image.FromFile(@"C:\retailer.bmp");
        int x = coordinates[i, 0];
        int y = coordinates[i, 1];

        PictureBox1.Size = new System.Drawing.Size(25, 25);
        PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
        PictureBox1.Location = new Point(x1 + x, y1 - y);
        panel1.Controls.Add(PictureBox1);

        Graphics g = e.Graphics;
        g.DrawLine(myPen, x1, y1, x1+x, y1-y);
    }
    myPen.Dispose();
    drawLine = false;     <==================== NEW CODE
}

but that won't stop them multiplying each time the button is clicked. You would be far better drawing the images directly onto panel1, as you do with the lines, unless there is a special reason for using a PictureBox.
[/Edit]

Henry Minute

Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”

modified on Friday, August 21, 2009 3:45 PM

GeneralRe: drawing multi line [modified] Pin
mgroses21-Aug-09 10:34
mgroses21-Aug-09 10:34 
GeneralRe: drawing multi line Pin
Henry Minute21-Aug-09 11:41
Henry Minute21-Aug-09 11:41 
GeneralRe: drawing multi line Pin
mgroses22-Aug-09 6:18
mgroses22-Aug-09 6:18 
AnswerRe: drawing multi line Pin
TAFIN22-Aug-09 20:21
TAFIN22-Aug-09 20:21 
QuestionHow to select right size icon from imagelist with multiple resolution icons Pin
shultas21-Aug-09 4:43
shultas21-Aug-09 4:43 
AnswerRe: How to select right size icon from imagelist with multiple resolution icons Pin
Saksida Bojan21-Aug-09 5:31
Saksida Bojan21-Aug-09 5:31 
GeneralRe: How to select right size icon from imagelist with multiple resolution icons Pin
shultas22-Aug-09 2:26
shultas22-Aug-09 2:26 
QuestionHow to insert the values in to two database tables in C# Pin
himanshu_roy8121-Aug-09 3:29
himanshu_roy8121-Aug-09 3:29 
AnswerRe: How to insert the values in to two database tables in C# Pin
Pete O'Hanlon21-Aug-09 3:37
mvePete O'Hanlon21-Aug-09 3:37 
AnswerRe: How to insert the values in to two database tables in C# Pin
Keith Barrow21-Aug-09 3:38
professionalKeith Barrow21-Aug-09 3:38 
AnswerRe: How to insert the values in to two database tables in C# Pin
PIEBALDconsult21-Aug-09 4:38
mvePIEBALDconsult21-Aug-09 4:38 
Questionhow to redirect the page when the particular cell in the datagridview is double clikced Pin
Anjani Poornima21-Aug-09 3:26
Anjani Poornima21-Aug-09 3:26 
AnswerRe: how to redirect the page when the particular cell in the datagridview is double clikced Pin
Henry Minute21-Aug-09 5:51
Henry Minute21-Aug-09 5:51 
GeneralRe: how to redirect the page when the particular cell in the datagridview is double clikced Pin
Anjani Poornima21-Aug-09 22:03
Anjani Poornima21-Aug-09 22:03 
GeneralRe: how to redirect the page when the particular cell in the datagridview is double clikced Pin
Henry Minute22-Aug-09 2:30
Henry Minute22-Aug-09 2:30 
AnswerRe: how to redirect the page when the particular cell in the datagridview is double clikced Pin
Saksida Bojan21-Aug-09 6:32
Saksida Bojan21-Aug-09 6:32 
Questionhow to call method of mdi child form from mdi parent form Pin
Tridip Bhattacharjee21-Aug-09 3:21
professionalTridip Bhattacharjee21-Aug-09 3:21 

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.