Reorder toolstrip items programmatically?

skip21

Member
Joined
Oct 21, 2012
Messages
6
Programming Experience
1-3
Hi Guys, I have a tools bar that has regular buttons on, and I have programatically added a datetimepicker. Ive been looking for a fair while now and can find no way to put the move the datetimepicker so that it is the first item on the toolstrip. Does anyone know a way of programatically moving items around in a toolstrip?

Hopefully this makes some sense!

Cheers

Skip :)
 
Hi,

I did not know how to do this but after a quick glance around the web what you need to do is add the existing ToolStripItems to a List of some type, sort it in whatever order you want and then re-add the items back to the ToolStrip. So, to solve your problem, you can follow this logic and then add the new item to the start of that List and then re-add that List back to the ToolStrip. Here is a quick example:-

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  Dim listOfCurrentToolStripItems As List(Of ToolStripItem) = ToolStrip1.Items.Cast(Of ToolStripItem).ToList
  Dim newToolStripComponent As New ToolStripControlHost(New DateTimePicker)
 
  listOfCurrentToolStripItems.Insert(0, newToolStripComponent)
  ToolStrip1.Items.Clear()
  ToolStrip1.Items.AddRange(listOfCurrentToolStripItems.ToArray)
End Sub


I am not sure if this is the most efficient method but it certainly does the trick.

Hope that helps.

Cheers,

Ian
 
You probably used the Add method that appends the item to the collection, instead use the Insert method where you can specify the index where you want the item inserted.
 
Thanks thats awesome. Is there an easy way to reorder items that are have been added to the toolbar manually? I need to do this programmatically as well if its possible!
 
Use the Insert method for that as well.
 
Back
Top