Advertisement

03.01.2008 at 10:46AM PST, ID: 23206426
[x]
Attachment Details

VB.net Panel and picturebox - wont display when in panel?

Asked by Matrix1000 in .NET, Microsoft Visual Basic.Net

Tags: microsoft, vb.net

I'm trying to write a little app that opens a text file and displays the contents of the text file in a RichTextBox and then use a picturebox to display the line numbers. I'm not sure if this is the best method but its the only one I could find.

The problem is that when I place the picturebox, with the line numbers, in a panel with the RichTextBox, the numbers don't show up.

How can I make it work?

The code example just uses one picture box named "MyPictureBox" and one RichTextBox named "MyRichTextBox ". Directly on the form it works great, put it in a panel and it doesn't.
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
Private Sub DrawRichTextBoxLineNumbers(ByRef g As Graphics)
      'calculate font heigth as the difference in Y coordinate between line 2 and line 1
      'note that the RichTextBox text must have at least two lines. So the initial Text property of the RichTextBox should not be an empty string. It could be something like vbcrlf & vbcrlf & vbcrlf 
      Dim font_height As Single = MyRichTextBox.GetPositionFromCharIndex(MyRichTextBox.GetFirstCharIndexFromLine(2)).Y - MyRichTextBox.GetPositionFromCharIndex(MyRichTextBox.GetFirstCharIndexFromLine(1)).Y
      If font_height = 0 Then Exit Sub
 
      'Get the first line index and location
      Dim firstIndex As Integer = MyRichTextBox.GetCharIndexFromPosition(New Point(0, g.VisibleClipBounds.Y + font_height / 3))
      Dim firstLine As Integer = MyRichTextBox.GetLineFromCharIndex(firstIndex)
      Dim firstLineY As Integer = MyRichTextBox.GetPositionFromCharIndex(firstIndex).Y
 
      'Print on the PictureBox the visible line numbers of the RichTextBox
      g.Clear(Control.DefaultBackColor)
      Dim i As Integer = firstLine
      Dim y As Single
      Do While y < g.VisibleClipBounds.Y + g.VisibleClipBounds.Height
         y = firstLineY + 2 + font_height * (i - firstLine - 1)
         g.DrawString((i).ToString, MyRichTextBox.Font, Brushes.DarkBlue, MyPictureBox.Width - g.MeasureString((i).ToString, MyRichTextBox.Font).Width, y)
         i += 1
      Loop
      'Debug.WriteLine("Finished: " & firstLine + 1 & " " & i - 1)
   End Sub
 
   Private Sub r_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyRichTextBox.Resize
      MyPictureBox.Invalidate()
   End Sub
 
   Private Sub r_VScroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyRichTextBox.VScroll
      MyPictureBox.Invalidate()
   End Sub
 
   Private Sub p_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyPictureBox.Paint
      DrawRichTextBoxLineNumbers(e.Graphics)
   End Sub
 
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      MyRichTextBox.Text = vbCrLf & vbCrLf & vbCrLf
   End Sub
[+][-]03.01.2008 at 11:07AM PST, ID: 21022983

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.01.2008 at 11:31AM PST, ID: 21023095

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.01.2008 at 11:32AM PST, ID: 21023096

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.01.2008 at 11:50AM PST, ID: 21023196

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.01.2008 at 11:52AM PST, ID: 21023204

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: .NET, Microsoft Visual Basic.Net
Tags: microsoft, vb.net
Sign Up Now!
Solution Provided By: roshkins
Participating Experts: 3
Solution Grade: A
 
 
[+][-]03.01.2008 at 01:30PM PST, ID: 21023652

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.03.2008 at 10:32AM PST, ID: 21034256

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628