Results 1 to 6 of 6

Thread: [RESOLVED] focus of list box

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    England
    Posts
    283

    Resolved [RESOLVED] focus of list box

    sorry i know this is an easy question i just don't know how to do it...

    I have a list box that continuously lists what the code is doing, as it does it.

    This all works fine, but i want the focus of the list box to always be on the last item added.

    At the moment it adds items and continues to add items off the bottom of the visible list box, the scroll bar has to be used to get to the bottom.

    i hope you understand my problem.

    Thanks

    GTJ

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: focus of list box

    I'd actually suggest using a TextBox for that. You can make it ReadOnly and then just use AppendText to add more text and automatically scroll to the end. A ListBox is supposed to be so the user can select an item or items from a list. Unless you want the user to be able to select the items in the ListBox a TextBox would be more apprpriate.
    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

  3. #3
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: focus of list box

    Quote Originally Posted by greythej
    sorry i know this is an easy question i just don't know how to do it...

    I have a list box that continuously lists what the code is doing, as it does it.

    This all works fine, but i want the focus of the list box to always be on the last item added.

    At the moment it adds items and continues to add items off the bottom of the visible list box, the scroll bar has to be used to get to the bottom.

    i hope you understand my problem.

    Thanks

    GTJ
    If your really want to use listbox then you could use the selectedindex property to get focus to the latest item that are add.
    Code:
    //in adding items to your listbox
    private void button1_Click(object sender, System.EventArgs e)
    		{
    			this.listBox1.Items.Add("My Germs");
    			this.listBox1.SelectedIndex=listBox1.Items.Count-1;
    		}

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    England
    Posts
    283

    Re: focus of list box

    ok, i took ur advice and changed it to a text box.

    But i still have a similar problem.

    If while the program is running it adds to the text box it automatically scrolls to the end. fine. But when the program starts up (form load event) there are a number of things added to the text box and the focus is not on the last thing added, it is still on the first.

    Is there any way to sort this out??

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: focus of list box

    Hmmm... That's a tricky one. You can set the SelectionStart to the Length of the Text and call ScrollToCaret, but the TextBox has to be visible for this to have an effect. You can use the Activated event like this if you're using VS.NET 2003:
    VB Code:
    1. Private firstActivation As Boolean = True
    2.  
    3.     Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
    4.         If Me.firstActivation Then
    5.             Me.TextBox1.SelectionStart = Me.TextBox1.Text.Length
    6.             Me.TextBox1.ScrollToCaret()
    7.             Me.firstActivation = False
    8.         End If
    9.     End Sub
    This works because the Activated event isn't raised until the form becomes visible. Things seem to have changed in .NET 2.0 as the Activated event semms to be raised before the form becomes visible.
    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
    Hyperactive Member
    Join Date
    May 2005
    Location
    England
    Posts
    283

    Re: focus of list box

    cheers that works great

Posting Permissions

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



Click Here to Expand Forum to Full Width