Discussions

Thursday, August 2, 2012

Sample Program #10


Let us create a program that will post a message from txtmsg with the specified message and button style after pressing cmdpreview. When cmdmsg is pressed, an input box will appear asking you a message to be written to the txtmsg textbox. Here is the code:

Private Sub cmdclose_Click()
  If MsgBox("Are you sure you want to close the application?", vbQuestion + vbYesNo, "Confirm") = vbYes Then
    End
  End If
End Sub

Private Sub cmdpreview_Click()
  Dim ans As VbMsgBoxResult
  ans = MsgBox(txtmsg.Text, Val(cmbmsgtype.Text) + Val(cmbbtntype.Text), "Message Preview")
 
  lblbtnclick.Caption = "You clicked "
  If ans = vbAbort Then
    lblbtnclick.Caption = lblbtnclick.Caption & "Abort"
  ElseIf ans = vbCancel Then
    lblbtnclick.Caption = lblbtnclick.Caption & "Cancel"
  ElseIf ans = vbIgnore Then
    lblbtnclick.Caption = lblbtnclick.Caption & "Ignore"
  ElseIf ans = vbNo Then
    lblbtnclick.Caption = lblbtnclick.Caption & "No"
  ElseIf ans = vbOK Then
    lblbtnclick.Caption = lblbtnclick.Caption & "OK"
  ElseIf ans = vbRetry Then
    lblbtnclick.Caption = lblbtnclick.Caption & "Retry"
  Else
    lblbtnclick.Caption = lblbtnclick.Caption & "Yes"
  End If
End Sub

Private Sub cmdmsg_Click()
  txtmsg.Text = InputBox("Enter your message here: ", "Input Box Message", txtmsg.Text)
End Sub