Phil_C Posted March 22, 2003 Posted March 22, 2003 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 Quote
*Experts* Bucky Posted March 22, 2003 *Experts* Posted March 22, 2003 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. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Phil_C Posted March 22, 2003 Author Posted March 22, 2003 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 Quote
*Experts* Bucky Posted March 22, 2003 *Experts* Posted March 22, 2003 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: 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 Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Phil_C Posted March 22, 2003 Author Posted March 22, 2003 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 Quote
*Experts* Bucky Posted March 22, 2003 *Experts* Posted March 22, 2003 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. If myCombo.SelectedItem Is Nothing Then ' Nothing is selected End If Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Phil_C Posted March 23, 2003 Author Posted March 23, 2003 Thanks again Bucky Is there any way to clear a combobox set to dropdownlist when a "New" button is clicked. Quote
*Experts* Bucky Posted March 23, 2003 *Experts* Posted March 23, 2003 myCombo.Items.Clear() Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Phil_C Posted March 23, 2003 Author Posted March 23, 2003 Found the answer by setting the combobox property via code to SelectedIndex = -1 Phil Quote
*Experts* jfackler Posted March 23, 2003 *Experts* Posted March 23, 2003 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 Quote
*Gurus* divil Posted March 23, 2003 *Gurus* Posted March 23, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* jfackler Posted March 23, 2003 *Experts* Posted March 23, 2003 (edited) 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. 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 Edited March 23, 2003 by jfackler Quote
*Experts* Bucky Posted March 23, 2003 *Experts* Posted March 23, 2003 SelectedIndex is an Integer, not a String, so you shouldn't assign it to "-1" here, but rather -1. (from your first code snippet) 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. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
*Gurus* divil Posted March 23, 2003 *Gurus* Posted March 23, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* jfackler Posted March 24, 2003 *Experts* Posted March 24, 2003 Whoa, no suprise apparently, my whole project lit up. Thanks....I'm teachable. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.