-1

It's me again, with a weird problem. I created a code which reads xml file and uses g.Drawstring function to get my sorted xml text to pictureBox1. So I got advanced and made the pictureBox scrollable and noticed the problem. When the text in pictureBox was off screen (invisible), when I scrolled back to it, it wasn't there anymore. Same happens if I just take my application and drag it off screen, so that text disappears, the text gets deleted, but if I load my program again (browse the xml file again) it refreshes, but that doesn't help, becuase I need to make it so that the text is always there. Here is a part of my code, I hope it helps:

     private void DrawInPictureBox(int index)
            {
                pictureBox1.Refresh();
                using (XmlReader reader = XmlReader.Create(openFileDialog1.FileNames.ElementAt(index)))
                 {
                     y = 0;
                     while (reader.Read())
                    {
                         if (reader.IsStartElement())
                         {
                             if (reader.Name == "Something")
                            {
                            string Dat = reader.GetAttribute("Dat");
                            Graphics g = pictureBox1.CreateGraphics();
                            if (reader.Read())
                            {
                                string m = reader.Value.Trim();

                                g.DrawString(m, new Font("Arial", 10), Brushes.Black, 0, y * 15 + 20);
                                g.DrawString(Dat + "ok", new Font("Arial", 10), Brushes.Black, 150, y * 15 + 20);
                                y = y + 1;
    }}}}}}

The pictureBox has a panel underneath it, so it is scrollable, but even if there is no panel the text gets deleted when taken off screen.

Thank you!

Jack
  • 1
  • 3
  • Also, it would be useful if you could tell me how to use the pictureBox to fit the xml imported string, so it is not gigantic and you aren't able to scroll 2 lines of text to the great beyond. :) – Jack Aug 03 '15 at 07:28
  • You can measure the string using `Graphics.MeasureString` or `TextRenderer.MeasureText` – TaW Aug 03 '15 at 08:17

1 Answers1

1

Some points:

  • It would be best if you can subscribe to PictureBox.OnPaint event and use the e.Graphics to draw your XML elements
  • With regards to scroll, there is a possibility that the location of your scrollable control is moving in one direction only which makes it invisible from the screen.

Try to debug the location of the scroll position by writing the values of scroll-X and scroll-Y position in output window via Console.WriteLine() or System.Debug.WriteLine(), chances are there are negative values, or out of boundary values. (In your case the value of y in the g.DrawString())

Joel Legaspi Enriquez
  • 1,236
  • 1
  • 10
  • 14