|
-
Feb 20th, 2006, 07:33 PM
#1
Thread Starter
Hyperactive Member
[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
-
Feb 20th, 2006, 08:01 PM
#2
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.
-
Feb 20th, 2006, 10:45 PM
#3
Re: focus of list box
 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;
}
-
Feb 21st, 2006, 08:36 AM
#4
Thread Starter
Hyperactive Member
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??
-
Feb 21st, 2006, 06:37 PM
#5
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:
Private firstActivation As Boolean = True
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
If Me.firstActivation Then
Me.TextBox1.SelectionStart = Me.TextBox1.Text.Length
Me.TextBox1.ScrollToCaret()
Me.firstActivation = False
End If
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.
-
Mar 1st, 2006, 02:21 PM
#6
Thread Starter
Hyperactive Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|