468,133 Members | 1,102 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,133 developers. It's quick & easy.

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...


Expand|Select|Wrap|Line Numbers
  1. private void button2_Click(object sender, EventArgs e)
  2.         {
  3.  
  4.  
  5.  
  6.  
  7.             richTextBox1.Font = fontDialog1.Font;
  8.             richTextBox2.Font = fontDialog1.Font;
  9.             richTextBox1.Visible = false;
  10.             richTextBox2.Visible = false;
  11.             Point ptr = new Point();
  12.  
  13.             pictureBox2.Show();
  14.             pictureBox2.Size = panel1.Size;
  15.             ptr = panel1.Location;
  16.             ptr.X -= 100;
  17.             ptr.Y -= 70;
  18.  
  19.             panel1.Show();
  20.  
  21.             Graphics g = pictureBox2.CreateGraphics();
  22.  
  23.  
  24.  
  25.             Graphics.FromHwnd(pictureBox2.Handle);
  26.             g.Flush();
  27.             Font myFont = fontDialog1.Font;
  28.  
  29.  
  30.  
  31.             if (richTextBox1.Text == "" || richTextBox2.Text == "")
  32.                 MessageBox.Show("No Data Found !");
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.             label1.Text = "Old Laser Checksum:";
  40.             label2.Text = "New Laser Checksum:";
  41.  
  42.             getcrcs();
  43.  
  44.  
  45.  
  46.  
  47.             int x = 0, y = 0;
  48.             int y1 = 0;
  49.             int[] larr = new int[richTextBox2.Lines.GetLength(0)];
  50.             int index = 0, maxindex = 0;
  51.  
  52.  
  53.  
  54.             foreach (String abc in richTextBox2.Lines)
  55.             {
  56.                 larr[maxindex] = abc.Length;
  57.                 maxindex++;
  58.             }
  59.  
  60.             index = 0;
  61.             foreach (String s in richTextBox1.Lines)
  62.             {
  63.  
  64.                 y = 0;
  65.                 ptr.Y += (int)fontDialog1.Font.GetHeight();
  66.  
  67.                 Point ptr1 = new Point();
  68.                 ptr1 = groupBox1.Location;
  69.                 ptr.X = ptr1.X;
  70.  
  71.  
  72.                 foreach (char c in s.ToCharArray())
  73.                 {
  74.  
  75.                     if (index < maxindex)
  76.                     {
  77.                         if (larr[index] > y)
  78.                         {
  79.  
  80.                             richTextBox2.SelectionStart = x + y - y1;
  81.                             richTextBox2.SelectionLength = 1;
  82.                             richTextBox1.SelectionStart = x + y;
  83.                             richTextBox1.SelectionLength = 1;
  84.                             string temp = richTextBox2.SelectedText;
  85.  
  86.                             ptr.X += (int)fontDialog1.Font.Size;
  87.  
  88.  
  89.  
  90.  
  91.                             foreach (char cc in temp.ToCharArray())
  92.                             {
  93.                                 if (cc != c)
  94.                                 {
  95.  
  96.                                     g.DrawString(cc.ToString(), fontDialog1.Font, Brushes.Red, new Point(ptr.X, ptr.Y));
  97.                                     g.DrawString(c.ToString(), fontDialog1.Font, Brushes.Red, new Point(ptr.X, ptr.Y));
  98.  
  99.                                     if(cc == 'f' || cc == 'i' || cc == 'j' || cc == 'l' || cc == 'r' || cc == 't')
  100.                                         ptr.X -= 5;
  101.                                     if (cc == 'm' || cc == 'w')
  102.                                         ptr.X += 4;
  103.  
  104.  
  105.                                 }
  106.                                 else
  107.                                 {
  108.  
  109.                                     g.DrawString(cc.ToString(), fontDialog1.Font, Brushes.Black, new Point(ptr.X, ptr.Y));
  110.                                     g.DrawString(c.ToString(), fontDialog1.Font, Brushes.Black, new Point(ptr.X, ptr.Y));
  111.  
  112.                                     if (cc == 'f' || cc == 'i' || cc == 'j' || cc == 'l' || cc == 'r' || cc == 't')
  113.                                         ptr.X -= 5;
  114.                                     if (cc == 'm' || cc == 'w')
  115.                                         ptr.X += 4;
  116.  
  117.                                 }
  118.  
  119.                        break;
  120.                             }
  121.                         }
  122.                     }
  123.  
  124.                     else
  125.                     {
  126.                         y1++;
  127.                     }
  128.  
  129.                     y++;
  130.                 }
  131.  
  132.                 x += s.Length + 1;
  133.                 index++;
  134.  
  135.  
  136.             }
  137.  
  138.  
  139.             panel1.Show();
  140.  
  141.             stat = g.Save();
  142.             g.Flush();
  143.  
  144.  
  145.             Thread.Sleep(2000);
  146.        }[
Dec 1 '09 #1
10 3258
tlhintoq
3,525 Expert 2GB
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.
Dec 1 '09 #2
tlhintoq
3,525 Expert 2GB
I might suggest getting rid of line 114. There is no reason to sleep for 2 full seconds when done.
Dec 1 '09 #3
@tlhintoq
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.
Dec 1 '09 #4
tlhintoq
3,525 Expert 2GB
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.
Dec 1 '09 #5
tlhintoq
3,525 Expert 2GB
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
Dec 1 '09 #6
@tlhintoq
Tried it, build successful, but same problem.........
Dec 1 '09 #7
tlhintoq
3,525 Expert 2GB
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)
Dec 1 '09 #8
Removed the code for disposing, still the same thing. Click twice it works, click once it doesn't.....
Dec 1 '09 #9
tlhintoq
3,525 Expert 2GB
MIght try adding picturebox2.Refresh(); around the 143 area
Maybe its just not updating without a nudge
Dec 1 '09 #10
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...:)
Dec 1 '09 #11

Post your reply

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

Similar topics

12 posts views Thread by Sanjay | last post: by
3 posts views Thread by Jeroen Ceuppens | last post: by
8 posts views Thread by Mark Johnson | last post: by
8 posts views Thread by Nathan Sokalski | last post: by
27 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.