PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2013 [RESOLVED] A better way to show progress in a TextBox-VBForums
Results 1 to 15 of 15

Thread: [RESOLVED] A better way to show progress in a TextBox

  1. #1

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    692

    Resolved [RESOLVED] A better way to show progress in a TextBox

    I have a WinForm application the performs a process and writes to a read-only TextBox to report it's activity at varying intervals that average 500 ms. There is a user defined limit as to how many lines it keeps. I maintain a list of string and once it's up to the user limit it removes the zero entity and adds one. Then I set the TextBox.Text to the list of string joined with a vbCrLf. To make it scroll to the bottom I append a space. This works but it flickers a lot, especially the scrollbar.
    I'm thinking of changing this to append a line to the TextBox as one normally does and find the end of the first line and delete that.
    I was wondering if anyone else had a better idea.

  2. #2

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    692

    Re: A better way to show progress in a TextBox

    Update: I disabled the vertical scroll bar while executing and the flicker is mostly eliminated.

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,479

    Re: A better way to show progress in a TextBox

    It sounds like you should use a ListBox control instead. Add the item to your List, remove the first item if the count exceed the user-defined maximum. Here is an example:

    Code:
    Private r As New Random
    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Set the user-defined maximum
        Dim maximum As Integer = Convert.ToInt32(NumericUpDown1.Value)
    
        'Declare the List
        Dim collection As New List(Of Integer)
    
        'Run your task
        Await Task.Run(Sub()
                           'I'm just adding random values for a random number of times and delaying the addition for 500ms
                           For counter As Integer = 0 To r.Next()
                               collection.Add(r.Next())
    
                               ListBox1.Invoke(Sub()
                                                   If counter >= maximum Then
                                                       ListBox1.Items.RemoveAt(0)
                                                   End If
    
                                                   ListBox1.Items.Add(collection.Last)
                                                   ListBox1.TopIndex = ListBox1.Items.Count - 1
                                               End Sub)
    
                               Threading.Thread.Sleep(500)
                           Next
                       End Sub)
    End Sub
    I had absolutely no flickering with this process.

    Edit - I forgot that you wanted to display the last result, so I added the TopIndex line to do just that. Still, no flickering.
    Last edited by dday9; Jul 11th, 2018 at 05:10 PM.

  4. #4

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    692

    Re: A better way to show progress in a TextBox

    Good suggestion. I've been playing with this and I think I like it better. Adding an item doesn't affect the position however so I added "ListBox1.TopIndex = ListBox1.Items.Count - 1" and that keeps it scrolled to the bottom. Is there a better way to do that? I might add a 'pause scrolling' feature for the UI. Cool.
    The only thing now is I would like to disable user selection. When I was using the TextBox I could set it to read-only. I know the collection of items is read-only to the user but I would rather have it something they can't interact with at all. I tried setting enabled to false but then the text is a light gray. The best I have found so far is to keep it enabled and set the background to gray. Any ideas on that?

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,026

    Re: A better way to show progress in a TextBox

    I'm not sure that there isn't a better option but one thing that comes to mind immediately is to owner draw your ListBox items and draw selected items in the same way as you draw the other items. The user can still select an item but they'll get no visual feedback so it will not appear selected to them.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    692

    Re: A better way to show progress in a TextBox

    I've heard of drawing but I don't know anything about it. I think I'll just live with the oddity for now. Too bad the TextBox.Lines collection is read only. Thanks all the same.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,026

    Re: A better way to show progress in a TextBox

    Quote Originally Posted by cory_jackson View Post
    I've heard of drawing but I don't know anything about it.
    It's not hard and will be very simple in your case. You simply set the DrawMode to OwnerDrawFixed and then handle the DrawItem event. You're not doing anything out of the ordinary so can simply call standard methods top draw the items. You'd usually test whether the current item was selected and draw it differently but, in your case, that's exactly what you want to avoid.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,026

    Re: A better way to show progress in a TextBox

    I just used this code and I got a ListBox that appeared to have no reaction to being clicked, i.e. no change in text or background colour and no focus rectangle:
    vb.net Code:
    1. Private Sub ListBox2_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ListBox2.DrawItem
    2.     e.Graphics.DrawString(ListBox1.GetItemText(ListBox1.Items(e.Index)).ToString(),
    3.                           e.Font,
    4.                           Brushes.Black,
    5.                           e.Bounds,
    6.                           StringFormat.GenericDefault)
    7. End Sub
    That code is a slightly-modified version of the example code in the documentation for the DrawItem event.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    692

    Re: A better way to show progress in a TextBox

    Thank you sir! I'll try it in the morning.

  10. #10

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    692

    Re: A better way to show progress in a TextBox

    I decided I wanted the read-only so I stuck with the TextBox. I kept my List of String as I described and modified my methodology slightly. Instead of setting the text property to the joined string value, I used the append method which causes it to scroll to the bottom. The only other thing I needed was to call the TextBox.Clear() method prior to that. Thanks again to everyone for their help.

  11. #11
    Frenzied Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    1,077

    Re: [RESOLVED] A better way to show progress in a TextBox

    What do you mean you wanted read only? List-box is read-only already! It is not a textbox.

    Could you elaborate more what depicts the "read only" factor between the two controls?

    You can change properties to customize the look and feel of a Listbox, you can even make it look close to a Textbox.

    Performance and quality sometimes outdo our wants and desires.

    Have you tried disabling the control? That prevents items being selected from the end user.
    Disclaimer: When code is given for example - it is merely a example.
    See my browser project BeffsBrowser
    Try my new Browser project LightBrowse "A Browser and not much else" Screen Capture with Imgur API


    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  12. #12
    Frenzied Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    1,077

    Re: A better way to show progress in a TextBox

    Quote Originally Posted by jmcilhinney View Post
    I just used this code and I got a ListBox that appeared to have no reaction to being clicked, i.e. no change in text or background colour and no focus rectangle:
    vb.net Code:
    1. Private Sub ListBox2_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ListBox2.DrawItem
    2.     e.Graphics.DrawString(ListBox1.GetItemText(ListBox1.Items(e.Index)).ToString(),
    3.                           e.Font,
    4.                           Brushes.Black,
    5.                           e.Bounds,
    6.                           StringFormat.GenericDefault)
    7. End Sub
    That code is a slightly-modified version of the example code in the documentation for the DrawItem event.
    All you really need is to set the Enabled property to false, it has its disadvantages, but it isn't that bad
    Disclaimer: When code is given for example - it is merely a example.
    See my browser project BeffsBrowser
    Try my new Browser project LightBrowse "A Browser and not much else" Screen Capture with Imgur API


    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  13. #13

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    692

    Re: [RESOLVED] A better way to show progress in a TextBox

    I understand the items are read-only. I should have used better words. Now that I think about it, I should have said "disabled". I don't want the user to be able to select and interact with it.
    I tried disabling it and it worked great but it changed the font to light gray and I couldn't find any way to change that font.
    It just seemed to get what I wanted was not natural or intended. I try to be a conformist in order to avoid future troubles. To get what I wanted, the TextBox was the control that required the least amount of tweaking.
    Thank you for your suggestion. I learned a few things.

  14. #14
    Frenzied Member jdc20181's Avatar
    Join Date
    Oct 2015
    Location
    Indiana
    Posts
    1,077

    Re: [RESOLVED] A better way to show progress in a TextBox

    I was using a newer framework, but I did not have the traditional grayed out look when I set the enabled property to false.


    I think it was like 4.6.1, I know what you mean for the older frameworks, like the one I have used since 2015.

    You could also make your own control, and customize entirely.

    Example with a button: https://social.msdn.microsoft.com/Fo...rum=vblanguage

    Another way is to simply do it yourself If Control.enabled = false then backcolor/forecolor = color.somethingnice
    Disclaimer: When code is given for example - it is merely a example.
    See my browser project BeffsBrowser
    Try my new Browser project LightBrowse "A Browser and not much else" Screen Capture with Imgur API


    Unless said otherwise (e.g. From one of my projects thus, those are mostly licensed under MIT) - All Code snippets advice or otherwise that I post on this site, are expressly licensed under Creative Commons Attribution 4.0 International Please respect my copyrights.

  15. #15

    Thread Starter
    Fanatic Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    692

    Re: [RESOLVED] A better way to show progress in a TextBox

    That's interesting. I might experiment with that later. Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width