Display of text delayed [modified]
-
I have a treeview which lists all the chapters of a book. Whenever the treeView is selected, the program is supposed to paint the text of the entire chapter onto a tabpage. ******************************************************************************* private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { // a query is run here to produce a result of text from a certain chapter and then import it to a chap_lines string array tabPage1.Paint += new PaintEventHandler(tabPage1_Paint); } private void tabPage1_Paint(object sender, PaintEventArgs e) { y = 8; Graphics g = e.Graphics; foreach (string line in chap_lines) { g.DrawString(line, new Font("Courier New", 10, System.Drawing.FontStyle.Regular), new SolidBrush(Color.Black), 3, y); y = y+20; } } ******************************************************************************* The problem is that the text is not painted until the next chapter node is selected. Chapter 1 is okay as it is the default node. But if I then select chapter 3, those text of chapter 1 stay there. The text of chapter 3 is not displayed until another chapter is selected. The thing is when I debug the program setting the breakpoint at "tabPage1.Paint += new PaintEventHandler(tabPage1_Paint)", it gives me the correct result. But if I set the breakpoint anywhere in the tabPage1_Paint event, it can never get out of itself. Why is that? How can I display its correct text once a chapter is selected? Also, I have set the tabPage1.AutoScroll to true but it still doesn't scroll to display those lines that are not visible to the tabPage1 area. -- modified at 22:07 Tuesday 6th June, 2006
-
I have a treeview which lists all the chapters of a book. Whenever the treeView is selected, the program is supposed to paint the text of the entire chapter onto a tabpage. ******************************************************************************* private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { // a query is run here to produce a result of text from a certain chapter and then import it to a chap_lines string array tabPage1.Paint += new PaintEventHandler(tabPage1_Paint); } private void tabPage1_Paint(object sender, PaintEventArgs e) { y = 8; Graphics g = e.Graphics; foreach (string line in chap_lines) { g.DrawString(line, new Font("Courier New", 10, System.Drawing.FontStyle.Regular), new SolidBrush(Color.Black), 3, y); y = y+20; } } ******************************************************************************* The problem is that the text is not painted until the next chapter node is selected. Chapter 1 is okay as it is the default node. But if I then select chapter 3, those text of chapter 1 stay there. The text of chapter 3 is not displayed until another chapter is selected. The thing is when I debug the program setting the breakpoint at "tabPage1.Paint += new PaintEventHandler(tabPage1_Paint)", it gives me the correct result. But if I set the breakpoint anywhere in the tabPage1_Paint event, it can never get out of itself. Why is that? How can I display its correct text once a chapter is selected? Also, I have set the tabPage1.AutoScroll to true but it still doesn't scroll to display those lines that are not visible to the tabPage1 area. -- modified at 22:07 Tuesday 6th June, 2006
Don't add the eventhandler to the paint event from the AfterSelect event. That should be done only once, preferably in the Load event. Now you are adding a new handler every time you select a chapter, which means that the tablPage1_Paint will be called multiple times whenever the control needs to be redrawn. To cause a control to be redrawn, you use the Invalidate method of the control. --- b { font-weight: normal; }
-
Don't add the eventhandler to the paint event from the AfterSelect event. That should be done only once, preferably in the Load event. Now you are adding a new handler every time you select a chapter, which means that the tablPage1_Paint will be called multiple times whenever the control needs to be redrawn. To cause a control to be redrawn, you use the Invalidate method of the control. --- b { font-weight: normal; }
Thanks! But I want the user to choose what chapter they want to read. If I put the eventhandler in the Load event, it would happen only once??? Well, I think I can do it for the 1st chapter (as default page) but how about the rest of the chapters?
-
Thanks! But I want the user to choose what chapter they want to read. If I put the eventhandler in the Load event, it would happen only once??? Well, I think I can do it for the 1st chapter (as default page) but how about the rest of the chapters?
You are confusing hooking up an event with using an event. Adding an event handler will hook up the method to the event, but it will not call the method. If you do this more than once, the event will contain more than one hook to the same method. That means that every time the event is used it will call the method more than one time. The paint event is used automatically whenever the control needs to be redrawn. You don't use the paint event yourself. If you need the control to be redrawn you use the Invalidate method to tell the system to redraw the control. --- b { font-weight: normal; }
-
You are confusing hooking up an event with using an event. Adding an event handler will hook up the method to the event, but it will not call the method. If you do this more than once, the event will contain more than one hook to the same method. That means that every time the event is used it will call the method more than one time. The paint event is used automatically whenever the control needs to be redrawn. You don't use the paint event yourself. If you need the control to be redrawn you use the Invalidate method to tell the system to redraw the control. --- b { font-weight: normal; }
Yes, I was confused as I'm new to C#. Anyway, I've got it working now - thanks for your help. Could you please also advise what I can do to make the tabpage scrollable (tabPage.AutoScroll = true doesn't work and I've got missing text - those painted beyond the visible area)? -- modified at 7:48 Wednesday 7th June, 2006
-
Yes, I was confused as I'm new to C#. Anyway, I've got it working now - thanks for your help. Could you please also advise what I can do to make the tabpage scrollable (tabPage.AutoScroll = true doesn't work and I've got missing text - those painted beyond the visible area)? -- modified at 7:48 Wednesday 7th June, 2006
-
Are you sure that it doesn't work? Do you have any contents in it that would make it scroll? --- b { font-weight: normal; }
It worked if I used labels to display text instead of painting it using graphics object. Would it be that the painting on the tabpage cannot be scrollable at all??? Someone suggested to add a label outside the visible area on the tabpage. It makes the tabpage scrollable but when I do a scroll, the painting becomes messy. Also, it can only be scrolled down to the y location of the label. It is still a problem if the text to be displayed is beyond it. Any suggestion for this fix?
-
It worked if I used labels to display text instead of painting it using graphics object. Would it be that the painting on the tabpage cannot be scrollable at all??? Someone suggested to add a label outside the visible area on the tabpage. It makes the tabpage scrollable but when I do a scroll, the painting becomes messy. Also, it can only be scrolled down to the y location of the label. It is still a problem if the text to be displayed is beyond it. Any suggestion for this fix?
The scrolling area is decided by the size needed by the child objects. The graphics you draw is no child objects, so it won't affect the scrolling area. To get the scrolling area to be the size you need, you have to put a child object in the tabpage. Either place the child object at the position of the last line you draw, or place it at the top and resize it to the size of what you draw. Perhaps you should make a special control to draw your text on, instead of drawing on the tappage. That would make it easier to handle scrolling also. --- b { font-weight: normal; }
-
The scrolling area is decided by the size needed by the child objects. The graphics you draw is no child objects, so it won't affect the scrolling area. To get the scrolling area to be the size you need, you have to put a child object in the tabpage. Either place the child object at the position of the last line you draw, or place it at the top and resize it to the size of what you draw. Perhaps you should make a special control to draw your text on, instead of drawing on the tappage. That would make it easier to handle scrolling also. --- b { font-weight: normal; }
It works now. I have added a panel on a tabpage and have the text painted on the panel. Thank you very much for your help.