1

I have this code that add items to a context menu's sub-menu:

 CTX_VALUE.Enabled = True
 CTX_VALUE.Visible = True
 CTX_VALUE.Text = "List Values"

 For k As Integer = 0 To CELL.VALUE_LIST.Count - 1
     CTX_VALUE.DropDownItems.Add(CELL.VALUE_LIST(k))
 Next k

Where CTX_VALUE is a ToolStripMenuItem

and CELL.VALUE_LIST is an ArrayList (yeah, old code!) of ToolStripMenuItems

When it comes to add about 150 items, it becomes really slow, about 2.5 seconds.

Visibility before adding doesn't matter, i tried moving it after.
BTW, note that the context menu is not on screen when adding items!

I also tried suspending layout of CTX_VALUE before adding. No luck.

Teejay
  • 7,210
  • 10
  • 45
  • 76

2 Answers2

3

you should add these using CTX_VALUE.DropDownItems.AddRange() method

Sarvesh Mishra
  • 2,014
  • 15
  • 30
1

Before the add items loop I used both

ts_filter.DropDown.SuspendDrawing (see addendum note below)

and

ts_filter.DropDown.SuspendLayout

After the loop I used the corresponding resume methods. This made a huge difference to my program moving it from unworkable to instant.

Addendum:- The resumedrawing (alone) was preventing my custom textbox (inherited from toolstriptextboxfrom) from showing. I found the suspendlayout and resumelayout to be ok alone though and it kept the speed up.

Tim F.
  • 268
  • 2
  • 8
  • Quote: *the context menu is not on screen*. – Hans Passant Dec 17 '21 at 12:26
  • Good point - it wasn't in my instance either as I was still building it in memory but having checked my code I accept that the root was and so likely triggering the display events as it built. I am leaving the above in though because this is where I landed on searching as to why adding ToolstripItems was so very slow and maybe the original question might lead people to think it doesn;t matter. – Tim F. Dec 18 '21 at 12:19