In this lesson we will disscuss Command Button.
This is the normal button you usually use in every application now a days.
its Default Name is Command1, Command2 and so on...
We mostly use its Click event to code and its Enabled property to make it active or passive according to the situation.
when you drag the Command Button object on the form, its Name is Command1 and Also the caption. caption is the text that appears to the user at runtime and name is of programmer's interest.
Suppose i want to take a command button and want that when i click it, the form background color changes automatically and Randomly
so take a command button and rename it with
CmdFirst
now goto click event of this button and code like below
Private sub CmdFirst_Click()
Form1.BackColor = RGB(Rnd * 255, Rnd * 255, Rnd * 255)
End Sub
where Form1 is the Name of the form that contains this Command Button. now if you run this application and click on the button, each time it will make a color randomly from the 1-255 for each red, green and blue colors and will make a new color of this combination.
similary if i have two buttons suppose CmbFirst and CmbSecond and want that when i click CmbFirst then CmbSecond can't be clicked and when click CmbFirst again then CmbSecond can be clicked then we will change the enabled property of CmbSecond on Click Event of CmbFirst as
Private Sub CmbFirst_Click()
CmbSecond.Enabled=not CmbSecond.Enabled
End Sub
here we used "Not" operator that works as if it is Enabled then make it disable and if it is disabled then make it enable.
--------------------------------------------------------------------------------------------
Also we can change the Caption at runtime by firing of some event of another object. suppose when i click the First Button then it makes the second button Disabled and changes caption of itself to "Click To Enable Second Button"
and when we click it to Enabled the Second button then it enables the Second button and changes its caption to "click me to disable second button "
Goto Click Event of CmdFirst and code as
Private sub CmdFirst_Click()
CmdSecond.Enabled=Not CmdSecond.Enabled
if CmdSecond.Enabled=True then
CmdFirst.Caption="Click Me to Disable Second Button"
Else
CmdFirst.Caption="Click Me to Enable Second Button"
End if
End sub
here we have used if statement which we will discuss in details a bit later. for now just use it as it is. it actually checks if first condition is true then it will follow the instruction just after that and if it becomes false then it comes to the else part and executes that part.
similary we will use command button in our examples very much and will change its many properties as needed in different situations. Changing the mouse pointer when our mouse comes over it , changing picture on its top but changing background picutre or backcolor of command button in vb, you need to change the Style property to Graphical instead of Standard.
if somebody want more things to do with button, directly contact us.
we will interact with button most of the times and our many problems will get solved accordingly as we proceed further.
Really its very good. i like it. can you please send me an example application please??
ReplyDeletei like the method you explained and if possible can you come online to guide me for a small database project in the same way please???
waiting for your +ve response.