Приветствую участников! В прилагаемом примере элемент списка из ListBox1 методом Drag and drop переносится в один из текстовых полей. В то же время данный элемент сохраняется в в ListBox1. Вопрос: что надо изменить, чтобы после переноса элемента он удалялся из исходного ListBox1 ??? или хотябы менял цвет или вроде того...
В том же примере, как добиться, чтобы в textBox можно было вставить только один элемент, а не как в примере???
Приветствую участников! В прилагаемом примере элемент списка из ListBox1 методом Drag and drop переносится в один из текстовых полей. В то же время данный элемент сохраняется в в ListBox1. Вопрос: что надо изменить, чтобы после переноса элемента он удалялся из исходного ListBox1 ??? или хотябы менял цвет или вроде того...
В том же примере, как добиться, чтобы в textBox можно было вставить только один элемент, а не как в примере???Мур
если отказаться от RowSource, то можно попробовать так: [vba]
Код
Option Explicit Dim arr
Private Sub UserForm_Initialize() arr = Application.Transpose(Range("A1", Cells(Rows.Count, 1).End(xlUp))) Me.ListBox1.RowSource = "" Me.ListBox1.List = arr End Sub
Private Sub ListBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal Y As Single) Dim MyDataObject As DataObject Dim Msg As String Dim Effect As Integer If Button = 1 Then Set MyDataObject = New DataObject MyDataObject.SetText ListBox1.Value Effect = MyDataObject.StartDrag(fmDropEffectMove) 'fmDropEffectCopy arr = Filter(arr, ListBox1.Value, False, vbTextCompare) Me.ListBox1.List = arr If Effect = 0 Then MsgBox (Msg) End If End Sub
[/vba]
если отказаться от RowSource, то можно попробовать так: [vba]
Код
Option Explicit Dim arr
Private Sub UserForm_Initialize() arr = Application.Transpose(Range("A1", Cells(Rows.Count, 1).End(xlUp))) Me.ListBox1.RowSource = "" Me.ListBox1.List = arr End Sub
Private Sub ListBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal Y As Single) Dim MyDataObject As DataObject Dim Msg As String Dim Effect As Integer If Button = 1 Then Set MyDataObject = New DataObject MyDataObject.SetText ListBox1.Value Effect = MyDataObject.StartDrag(fmDropEffectMove) 'fmDropEffectCopy arr = Filter(arr, ListBox1.Value, False, vbTextCompare) Me.ListBox1.List = arr If Effect = 0 Then MsgBox (Msg) End If End Sub
Мур, мне кажется, лучше вот такой вариант (выбираем месяц, нажимаем кнопку; текстбоксы заполняются последовательно) [vba]
Код
Private Sub CommandButton1_Click() If Me.ListBox1.ListIndex < 0 Then MsgBox "Не выбран месяц", 64: Exit Sub Dim i& For i = 1 To 4 If Len(Me("Textbox" & i)) = 0 Then Me("Textbox" & i).Value = ListBox1.Value arr = Filter(arr, ListBox1.Value, False, vbTextCompare) Me.ListBox1.List = arr Exit For End If Next End Sub
[/vba] возможно, не так изящно, как с ListBox1_MouseMove, зато надежно и проверено, на века
Мур, мне кажется, лучше вот такой вариант (выбираем месяц, нажимаем кнопку; текстбоксы заполняются последовательно) [vba]
Код
Private Sub CommandButton1_Click() If Me.ListBox1.ListIndex < 0 Then MsgBox "Не выбран месяц", 64: Exit Sub Dim i& For i = 1 To 4 If Len(Me("Textbox" & i)) = 0 Then Me("Textbox" & i).Value = ListBox1.Value arr = Filter(arr, ListBox1.Value, False, vbTextCompare) Me.ListBox1.List = arr Exit For End If Next End Sub
[/vba] возможно, не так изящно, как с ListBox1_MouseMove, зато надежно и проверено, на века nilem
Вот так вроде работает: Создайте форму с двумя листбоксами:[vba]
Код
Private mobjFromList As MSForms.ListBox Private mlFrom As Long
Private Sub UserForm_Initialize() Dim L As Long For L = 0 To 50 Me.ListBox1.AddItem "Item " & L Next End Sub
Private Sub ListBox1_MouseMove(ByVal Button As Integer, _ ByVal Shift As Integer, _ ByVal X As Single, _ ByVal Y As Single)
Dim objData As DataObject Dim lEffect As Long
Const lLEFTMOUSEBUTTON As Long = 1
If Button = lLEFTMOUSEBUTTON Then Set objData = New DataObject Set mobjFromList = Me.ListBox1 objData.SetText Me.ListBox1.Text mlFrom = Me.ListBox1.ListIndex lEffect = objData.StartDrag End If End Sub
Private Sub ListBox1_BeforeDragOver(ByVal Cancel As MSForms.ReturnBoolean, _ ByVal Data As MSForms.DataObject, _ ByVal X As Single, _ ByVal Y As Single, _ ByVal DragState As MSForms.fmDragState, _ ByVal Effect As MSForms.ReturnEffect, _ ByVal Shift As Integer)
Cancel = True Effect = fmDropEffectMove End Sub
Private Sub ListBox1_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, _ ByVal Action As MSForms.fmAction, _ ByVal Data As MSForms.DataObject, _ ByVal X As Single, _ ByVal Y As Single, _ ByVal Effect As MSForms.ReturnEffect, _ ByVal Shift As Integer)
Dim lTo As Long
With Me.ListBox1 lTo = .TopIndex + Int(Y * 0.85 / .Font.Size) If lTo >= .ListCount Then lTo = .ListCount Cancel = True Effect = fmDropEffectMove .AddItem Data.GetText, lTo If mobjFromList = Me.ListBox1 And lTo < mlFrom Then mobjFromList.RemoveItem (mlFrom + 1) Else mobjFromList.RemoveItem mlFrom End If Set mobjFromList = Nothing End With End Sub
'--------------------- Private Sub listbox2_MouseMove(ByVal Button As Integer, _ ByVal Shift As Integer, _ ByVal X As Single, _ ByVal Y As Single)
Dim objData As DataObject Dim lEffect As Long
Const lLEFTMOUSEBUTTON As Long = 1
If Button = lLEFTMOUSEBUTTON Then Set objData = New DataObject Set mobjFromList = Me.ListBox2 objData.SetText Me.ListBox2.Text mlFrom = Me.ListBox2.ListIndex lEffect = objData.StartDrag End If End Sub
Private Sub listbox2_BeforeDragOver(ByVal Cancel As MSForms.ReturnBoolean, _ ByVal Data As MSForms.DataObject, _ ByVal X As Single, _ ByVal Y As Single, _ ByVal DragState As MSForms.fmDragState, _ ByVal Effect As MSForms.ReturnEffect, _ ByVal Shift As Integer)
Cancel = True Effect = fmDropEffectMove End Sub
Private Sub listbox2_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, _ ByVal Action As MSForms.fmAction, _ ByVal Data As MSForms.DataObject, _ ByVal X As Single, _ ByVal Y As Single, _ ByVal Effect As MSForms.ReturnEffect, _ ByVal Shift As Integer)
Dim lTo As Long
With Me.ListBox2 lTo = .TopIndex + Int(Y * 0.85 / .Font.Size) If lTo >= .ListCount Then lTo = .ListCount Cancel = True Effect = fmDropEffectMove .AddItem Data.GetText, lTo If mobjFromList = Me.ListBox2 And lTo < mlFrom Then mobjFromList.RemoveItem (mlFrom + 1) Else mobjFromList.RemoveItem mlFrom End If Set mobjFromList = Nothing End With End Sub
[/vba] Код не мой. Взято отсюда и скопирована часть внутри.
Вот так вроде работает: Создайте форму с двумя листбоксами:[vba]
Код
Private mobjFromList As MSForms.ListBox Private mlFrom As Long
Private Sub UserForm_Initialize() Dim L As Long For L = 0 To 50 Me.ListBox1.AddItem "Item " & L Next End Sub
Private Sub ListBox1_MouseMove(ByVal Button As Integer, _ ByVal Shift As Integer, _ ByVal X As Single, _ ByVal Y As Single)
Dim objData As DataObject Dim lEffect As Long
Const lLEFTMOUSEBUTTON As Long = 1
If Button = lLEFTMOUSEBUTTON Then Set objData = New DataObject Set mobjFromList = Me.ListBox1 objData.SetText Me.ListBox1.Text mlFrom = Me.ListBox1.ListIndex lEffect = objData.StartDrag End If End Sub
Private Sub ListBox1_BeforeDragOver(ByVal Cancel As MSForms.ReturnBoolean, _ ByVal Data As MSForms.DataObject, _ ByVal X As Single, _ ByVal Y As Single, _ ByVal DragState As MSForms.fmDragState, _ ByVal Effect As MSForms.ReturnEffect, _ ByVal Shift As Integer)
Cancel = True Effect = fmDropEffectMove End Sub
Private Sub ListBox1_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, _ ByVal Action As MSForms.fmAction, _ ByVal Data As MSForms.DataObject, _ ByVal X As Single, _ ByVal Y As Single, _ ByVal Effect As MSForms.ReturnEffect, _ ByVal Shift As Integer)
Dim lTo As Long
With Me.ListBox1 lTo = .TopIndex + Int(Y * 0.85 / .Font.Size) If lTo >= .ListCount Then lTo = .ListCount Cancel = True Effect = fmDropEffectMove .AddItem Data.GetText, lTo If mobjFromList = Me.ListBox1 And lTo < mlFrom Then mobjFromList.RemoveItem (mlFrom + 1) Else mobjFromList.RemoveItem mlFrom End If Set mobjFromList = Nothing End With End Sub
'--------------------- Private Sub listbox2_MouseMove(ByVal Button As Integer, _ ByVal Shift As Integer, _ ByVal X As Single, _ ByVal Y As Single)
Dim objData As DataObject Dim lEffect As Long
Const lLEFTMOUSEBUTTON As Long = 1
If Button = lLEFTMOUSEBUTTON Then Set objData = New DataObject Set mobjFromList = Me.ListBox2 objData.SetText Me.ListBox2.Text mlFrom = Me.ListBox2.ListIndex lEffect = objData.StartDrag End If End Sub
Private Sub listbox2_BeforeDragOver(ByVal Cancel As MSForms.ReturnBoolean, _ ByVal Data As MSForms.DataObject, _ ByVal X As Single, _ ByVal Y As Single, _ ByVal DragState As MSForms.fmDragState, _ ByVal Effect As MSForms.ReturnEffect, _ ByVal Shift As Integer)
Cancel = True Effect = fmDropEffectMove End Sub
Private Sub listbox2_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, _ ByVal Action As MSForms.fmAction, _ ByVal Data As MSForms.DataObject, _ ByVal X As Single, _ ByVal Y As Single, _ ByVal Effect As MSForms.ReturnEffect, _ ByVal Shift As Integer)
Dim lTo As Long
With Me.ListBox2 lTo = .TopIndex + Int(Y * 0.85 / .Font.Size) If lTo >= .ListCount Then lTo = .ListCount Cancel = True Effect = fmDropEffectMove .AddItem Data.GetText, lTo If mobjFromList = Me.ListBox2 And lTo < mlFrom Then mobjFromList.RemoveItem (mlFrom + 1) Else mobjFromList.RemoveItem mlFrom End If Set mobjFromList = Nothing End With End Sub
[/vba] Код не мой. Взято отсюда и скопирована часть внутри.SkyPro
skypro1111@gmail.com
Сообщение отредактировал SkyPro - Четверг, 23.01.2014, 18:05