Cut cop and paste

Phil_C

Newcomer
Joined
Mar 21, 2003
Messages
11
Location
Australia
I have set up a mainmenu with cut, copy and paste buttons. On the form there are both textboxes and comboboxes. How do I stop these commands from working on the comboboxes?

Thanks in advance

Phil
 
What is the code you're using to Cut, Copy, and Paste? The
menu items won't do anything unless you add code to do them.
 
Private Sub mnuCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCut.Click
'Declare a Textbox as object and set it to the ActiveControl
Dim objTextbox As TextBox = Me.ActiveControl
'Copy the text to the clipboard and clear the field.
objTextbox.Cut()
End Sub

The copy and paste items are similar to the above.

Thanks

Phil
 
You need to compare the Type of the ActiveControl to the Type of
a textbox, and if they are equal then you can do the cut, copy,
and paste:

Visual Basic:
Private Sub mnuCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCut.Click
  Dim activeType As Type = Me.ActiveControl.GetType()

  If activeType.Equals(GetType(TextBox)) Then
    'Declare a Textbox as object and set it to the ActiveControl
    Dim objTextbox As TextBox = Me.ActiveControl

    'Copy the text to the clipboard and clear the field.
    objTextbox.Cut()
  End If
End Sub
 
Thanks Bucky that works great. One more question how do I force the user to select an option from the combobox and disable typing in the combobox.

Thanks again

Phil
 
That was two questions, not one, but I think I'll manage. :)

To disable typing the combobox, set its DropDownStyle property
to ComboBoxStyle.DropDownList.

If the user didn't select anything, then the SelectedItem property
will be equal to Nothing.

Visual Basic:
If myCombo.SelectedItem Is Nothing Then
  ' Nothing is selected
End If
 
The following will clear the field of a combobox.dropdownlist after changing it to a dropdown so that the field can be edited on a right mouse click. I change the box back to a combobox.dropdownlist after updating the dataset from which the members of the dropdown list are drawn so the combobox can act again as a simple dropdownlist....keeps a user from corrupting a member unless they really mean to.


Private Sub cmbBoxName_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cmbBoxName.MouseDown

If cmbBoxName.MouseButtons = cmbBoxName.MouseButtons.Right Then
cmbBoxName.DropDownStyle = ComboBoxStyle.DropDown


cmbBoxName.SelectedIndex = "-1"
If cmbBoxName.Text <> "" Then
cmbBoxName.SelectedIndex = "-1"

End If

End If
End Sub
 
I don't know how you came up with that code, but I wouldn't touch it. I see you're trying to assign a string to an integer property.
 
Divil,
I don't see the string to integer. Here's the whole fragment of code. I'm new to this and am sure I go way around sometimes to get to the end result I need. Let me explain: (The program is essentially a database mining and manipulation process that keeps track of employee hours worked and vacation/sick time.) I needed to add an employee to a database whose data was bound to the combobox and several text boxes that yeilded a profile of all the current employees. The form also is used to edit the employees that currently exist in that database. I suppose I could have/should have created a new form for adding new employees but I thought it might be easier on the user to simply make the fields they shouldn't edit invisible. Thus the right click...After entering the new employee name in the combobox, a btnAddNewEmployee is clicked that creates a new datarow in the dataset with the new employee name as the primamry key. Then when the new data row has been added to the dataset, the remainder of the fields are reset to visible and can be edited. I wanted the combobox to do double duty and changing it's dropdown style lets the user add a new employee without allowing for a change in the current data.
Like I said, might be the long way around but it works and my end users like the interface. I am interested in doing this not only functionally but well however, and so appreciate your criticism.
Visual Basic:
  Private Sub cmbEmployeeName_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles cmbEmployeeName.MouseDown
        If cmbEmployeeName.MouseButtons = cmbEmployeeName.MouseButtons.Right Then
            cmbEmployeeName.DropDownStyle = ComboBoxStyle.DropDown
            lblEmployeePhon.Visible = False
            txbPhone.Visible = False
            lblDateOfHire.Visible = False
            DateTimePicker1.Visible = False
            lblPTO.Visible = False
            txbPTO.Visible = False
            lblPTORemaining.Visible = False
            txbESRemainingPTO.Visible = False
            btnDeleteEmployee.Visible = False
            lblHours.Visible = False
            lblHours2.Visible = False
            lblClicktoDelete.Visible = False
            Label4.Visible = False

            cmbEmployeeName.SelectedIndex = "-1"
            If cmbEmployeeName.Text <> "" Then
                cmbEmployeeName.SelectedIndex = "-1"

            End If

        End If
    End Sub
Private Sub btnAddNewEmployee_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNewEmployee.Click
        Dim NewRow As DataRow = dataset2.Tables("employees").NewRow
        NewRow("Name") = cmbEmployeeName.Text
        NewEmployee = cmbEmployeeName.Text

        Dim trim As Integer
        Dim length As Integer
        Dim removed As Integer

        length = cmbEmployeeName.Text.Length

        trim = cmbEmployeeName.Text.IndexOf(" ")
        removed = length - trim
        NewRow("LastName") = cmbEmployeeName.Text.Substring(trim)
        NewRow("FirstName") = cmbEmployeeName.Text.Remove(trim, removed)

        NewRow("Phone") = "Enter Employee Phone"
        NewRow("BeginningPTO") = 0
        NewRow("DateHired") = Date.Now
dataset2.Tables("Employees").Rows.Add(NewRow)

        cmbEmployeeName.DropDownStyle = ComboBoxStyle.DropDownList

        lblEmployeePhon.Visible = True
        txbPhone.Visible = True
        lblDateOfHire.Visible = True
        DateTimePicker1.Visible = True
        lblPTO.Visible = True
        txbPTO.Visible = True
        lblPTORemaining.Visible = True
        txbESRemainingPTO.Visible = True
        btnDeleteEmployee.Visible = True
        lblHours.Visible = True
        lblHours2.Visible = True
        lblClicktoDelete.Visible = True
        Label4.Visible = True
        Me.BindingContext(dataset2, "Employees").Position = cmbEmployeeName.FindStringExact(NewEmployee)

        cmbEmployeeName.SelectedIndex = cmbEmployeeName.FindStringExact(NewEmployee)
        MessageBox.Show("To complete the addition of this new employee, enter the appropriate information in the data fields that are now available.", "Complete New Employee Setup", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub
 
Last edited:
SelectedIndex is an Integer, not a String, so you shouldn't assign
it to "-1" here, but rather -1.

(from your first code snippet)
Visual Basic:
cmbBoxName.SelectedIndex = -1
If cmbBoxName.Text <> "" Then
cmbBoxName.SelectedIndex = -1

It looks like Phil's question was answered, so I'm not sure that
any further discussion is necessary.
 
Go in to your project properties and turn Option Strict on. You'll suddenly get a whole load of build errors, because Option Strict doesn't let you do silly things like assign strings to integers.
 
Back
Top