Context Menu Strip in VB Net
8K views
Nov 21, 2023
Context Menu Strip in VB Net
View Video Transcript
0:00
In this video we will see how to create a context menu or pop-up menu functionality in a windows forms application using visual basic.net
0:12
So first thing you want to do is create a windows forms application using visual studio 2010
0:19
Once your application is created, what I'm going to do is first thing I'm going to create, add, drag and drop a context manuscript control onto the form
0:30
When you drag and drop this context manuscript control, you will see context manuscript 1 is added to the components tray
0:41
Now, you can set its properties by right-click and select properties or by hitting F4
0:49
As you can see, you can set background color, foreground color, image or any other properties which you can do in any other control here for the whole context menu
1:02
So, you can also add context menu items here. Keep in mind, a context menu is nothing without its items
1:13
If you just have a context menu and there are no items in it, you will see nothing
1:19
To add a context menu item, it's very simple. You just add it here and type your item name
1:26
Say I call it close. Now this context menu item is a separate control and can be added to context menu strips
1:37
So if you right click on this you will see it has its own properties
1:41
This control is actually a toolbar tool strip menu item control And as you can see it can have its own background color or any other properties like like any other control does so that that now next thing you
2:08
want to do is what a context menu strip does is when you right click on a control a pop
2:13
up menu or context menu pops up and when you click a menu item it does something so it
2:20
executes some code so what we need to do is on this close context menu item we need to
2:25
actually write an event handler and writing adding an event handler to handler to a context
2:31
menu item is very simple in visual studio 2010 you just double click on it as soon as
2:38
you double click you see the context menu item event handler is added on this what i'm
2:44
going to write is i'm going to write a simple code called me.close what this me.close does
2:51
is closes the form so now so far what we did is created a context menu strip control added
2:58
the menu item close and now the last thing we want to do is attach this context menu
3:04
strip to a control. Again, we can attach to a control even including the form itself
3:11
So what I am going to do is I right click on the form properties and look for context
3:17
menu strip property which is right here as you can see. Now I drop if I select drop and
3:26
down I see context manuscript one I select this so what it doing now it attach attaching this context sub control to the form so let hit F5 to run the application and now if I
3:39
right click on this form you will see my context menu appears if I select close
3:44
it closes the form so that's basically nutshell of how to use this context menu
3:51
context menu and how to build context menu enable applications in Windows
3:56
forms now I'm going to take a little more complex example what I'm going to do is I'm
4:01
going to create a button control drag and drop to the form now I'm going to create one
4:08
more context menu control and I call this control oops I call this control button menu what I'm
4:25
going to do is I'm going to add a menu item called color then one more thing you
4:31
can do a context menu can even add sub items I'm going to add sub items say
4:35
foreground and background like any other items each of these item menu item can
4:46
have its own event so I'm going to double click on foreground and I'm going
4:50
to double click on background but actually I'm going to do is I'm going to
4:54
call a color dialog control and select that will let you select a color and then I'm going to set the color
5:03
of that button control So you see I created a color dialog and if it is selected ok I set foreground color of
5:52
the button to the color selected in the color dialog. Now here I am going to set back color
6:00
So that will do it. Now simply F5 hit and run. Oh I forgot one thing
6:06
Now one thing I missed is I am going to attach this context menu called button menu to the
6:18
button control by right click properties go to context menu strip property and select button menu
6:26
Now I can hit F5. When you hit F5 if you right click on the button you will see color and you will see foreground
6:32
and background. select foreground color dialog pops up and you can pick any color you want say I can pick pink
6:40
color and then you say foreground and I'll say yellow boom there you go so by applying same
6:48
method you can you know add context menu or pop-up menu functionality to any windows forms
6:55
control again this is a basic tutorial and you know if you want more just keep coming back to
7:02
the site I'll have more detailed tutorial soon
#Software
#Windows & .NET