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

Wednesday, August 1, 2012

Sample Program #9


Let us create a program that will capitalize every first letter of the word and lowering the case of the remaining letters after pressing the [ENTER] button. Here is the code:

Private Sub txtw_keypress(KeyAscii As Integer)
Dim size, pos As Integer
  If KeyAscii = 13 Then
    txtw.SelStart = 0
    txtw.SelLength = 1
    txtc.Text = UCase(txtw.SelText)
    size = Len(txtw.Text) - 1
    pos = 1
 
    Do While pos <= size
      txtw.SelStart = pos
      txtw.SelLength = 1
      If txtw.SelText = " " Then
        pos = pos + 1
        txtw.SelStart = pos
        txtw.SelLength = 1
        txtc.Text = txtc.Text + " " + UCase(txtw.SelText)
      Else
        txtc.Text = txtc.Text + LCase(txtw.SelText)
      End If
      pos = pos + 1
    Loop
    txtw.SelStart = Len(txtw.Text)
  End If
End Sub