C# Graphics Drawing Problem.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anuking
    New Member
    • Dec 2009
    • 5

    C# Graphics Drawing Problem.

    Hi,

    I made a tool that compares the texts from 2 richtextboxes and then marks the characters that are different in red. An option needs me to overlap these 2 text data to show the exact difference. I used a picturebox which is contained in a panel and draw the characters in respective colors using Graphics class. Everything is working fine, but for some reason I need to click the button twice to be able to see something on picturebox. There is nothing assigned as event handlers to either picturebox or panel. Below is the code for that, in the last line I used the sleep method to actually see what happens. Surprisingly when the system is in sleep for these 2 seconds, I can actually see the data correctly, but after these 2 seconds the picturebox is clear. I click on the button again and this time it stays even after the 2 seconds sleep. Please tell me where I am going wrong ? Any help is really appreciated.

    Also, I am using the latest Visual Studio 2010 Beta, Ultimate version, maybe this is also a reason, but just want to make sure before I make a new project in older version. As no backward compatibility is supported by MS.

    Thank You Once Again in Advance...


    Code:
    private void button2_Click(object sender, EventArgs e)
            {
                
    
                
                
                richTextBox1.Font = fontDialog1.Font;
                richTextBox2.Font = fontDialog1.Font;
                richTextBox1.Visible = false;
                richTextBox2.Visible = false;
                Point ptr = new Point();
                
                pictureBox2.Show();
                pictureBox2.Size = panel1.Size;
                ptr = panel1.Location;
                ptr.X -= 100;
                ptr.Y -= 70;
    
                panel1.Show();
    
                Graphics g = pictureBox2.CreateGraphics();
               
                
                
                Graphics.FromHwnd(pictureBox2.Handle);
                g.Flush();
                Font myFont = fontDialog1.Font;
                
    
    
                if (richTextBox1.Text == "" || richTextBox2.Text == "")
                    MessageBox.Show("No Data Found !");
    
    
    
    
    
    
                label1.Text = "Old Laser Checksum:";
                label2.Text = "New Laser Checksum:";
    
                getcrcs();
    
             
    
                
                int x = 0, y = 0;
                int y1 = 0;
                int[] larr = new int[richTextBox2.Lines.GetLength(0)];
                int index = 0, maxindex = 0;
    
    
    
                foreach (String abc in richTextBox2.Lines)
                {
                    larr[maxindex] = abc.Length;
                    maxindex++;
                }
                
                index = 0;
                foreach (String s in richTextBox1.Lines)
                {
    
                    y = 0;
                    ptr.Y += (int)fontDialog1.Font.GetHeight();
    
                    Point ptr1 = new Point();
                    ptr1 = groupBox1.Location;
                    ptr.X = ptr1.X;
    
    
                    foreach (char c in s.ToCharArray())
                    {
    
                        if (index < maxindex)
                        {
                            if (larr[index] > y)
                            {
    
                                richTextBox2.SelectionStart = x + y - y1;
                                richTextBox2.SelectionLength = 1;
                                richTextBox1.SelectionStart = x + y;
                                richTextBox1.SelectionLength = 1;
                                string temp = richTextBox2.SelectedText;
    
                                ptr.X += (int)fontDialog1.Font.Size;
                                
                                
                                
    
                                foreach (char cc in temp.ToCharArray())
                                {
                                    if (cc != c)
                                    {
                                       
                                        g.DrawString(cc.ToString(), fontDialog1.Font, Brushes.Red, new Point(ptr.X, ptr.Y));
                                        g.DrawString(c.ToString(), fontDialog1.Font, Brushes.Red, new Point(ptr.X, ptr.Y));
    
                                        if(cc == 'f' || cc == 'i' || cc == 'j' || cc == 'l' || cc == 'r' || cc == 't')
                                            ptr.X -= 5;
                                        if (cc == 'm' || cc == 'w')
                                            ptr.X += 4;
    
                                        
                                    }
                                    else
                                    {
                                        
                                        g.DrawString(cc.ToString(), fontDialog1.Font, Brushes.Black, new Point(ptr.X, ptr.Y));
                                        g.DrawString(c.ToString(), fontDialog1.Font, Brushes.Black, new Point(ptr.X, ptr.Y));
                                        
                                        if (cc == 'f' || cc == 'i' || cc == 'j' || cc == 'l' || cc == 'r' || cc == 't')
                                            ptr.X -= 5;
                                        if (cc == 'm' || cc == 'w')
                                            ptr.X += 4;
    
                                    }
    
                           break;
                                }
                            }
                        }
    
                        else
                        {
                            y1++;
                        }
    
                        y++;
                    }
    
                    x += s.Length + 1;
                    index++;
    
    
                }
    
                
                panel1.Show();
                
                stat = g.Save();
                g.Flush();
                
    
                Thread.Sleep(2000);
           }[
    Last edited by anuking; Dec 1 '09, 10:32 PM.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      I might suggest getting rid of line 114. There is no reason to sleep for 2 full seconds when done.

      Comment

      • anuking
        New Member
        • Dec 2009
        • 5

        #4
        Originally posted by tlhintoq
        I might suggest getting rid of line 114. There is no reason to sleep for 2 full seconds when done.
        Thanks tlhintoq for a quick response......

        That sleep I used it for diagnosis purpose, the data is accurately displayed only during these 2 seconds, after that the picturebox goes blank. This is the case when I click this button2 first time, in the second click the data stays even after this sleep. I got rid of this sleep and then again the data is displayed in picturebox but only after 2 clicks, or 2 iterations of the above code.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          As no backward compatibility is supported by MS.
          That is true for the Project/Solution but you can always create a new project in VS08 then...
          Add.. | Existing files...
          Then direct it to the files within the VS10 project. The C-sharp doesn't change between VS08 and VS10 - just the .proj .sln files.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            That sleep I used it for diagnosis purpose, the data is accurately displayed only during these 2 seconds, after that the picturebox goes blank.
            Line 112 - you are disposing of the graphics object that contains your data.
            Try making a new bitmap - paint your text to it - then set the Picturebox image to the bitmap

            Comment

            • anuking
              New Member
              • Dec 2009
              • 5

              #7
              Originally posted by tlhintoq
              That is true for the Project/Solution but you can always create a new project in VS08 then...
              Add.. | Existing files...
              Then direct it to the files within the VS10 project. The C-sharp doesn't change between VS08 and VS10 - just the .proj .sln files.
              Tried it, build successful, but same problem........ .

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                Try not disposing of the graphics object g and see if the picture box doesn't clear. (line 112 as mentioned a couple posts back)

                Comment

                • anuking
                  New Member
                  • Dec 2009
                  • 5

                  #9
                  Removed the code for disposing, still the same thing. Click twice it works, click once it doesn't.....

                  Comment

                  • tlhintoq
                    Recognized Expert Specialist
                    • Mar 2008
                    • 3532

                    #10
                    MIght try adding picturebox2.Ref resh(); around the 143 area
                    Maybe its just not updating without a nudge

                    Comment

                    • anuking
                      New Member
                      • Dec 2009
                      • 5

                      #11
                      same thing, I believe after this function is done, something is being called, but I have added no code for any such event handling. I am attaching the entire solution project, it also has .exe, try typing same text in the two boxes with a difference of some chars to see what it does......I mean only if you have some time...:)

                      Comment

                      Working...