[RESOLVED] VB6 OLEDragDrop - Drop file from Explorer to application
Hello all, new member here
I have application with many controls in it, and I can drop file from explorer to any control.
There is a code for every control.
Code:
Option Explicit
Private Sub Form_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1.Text = (Data.Files(1))
End Sub
Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1.Text = (Data.Files(1))
End Sub
Private Sub Label1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1.Text = (Data.Files(1))
End Sub
Private Sub Command1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1.Text = (Data.Files(1))
End Sub
Private Sub Combo1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1.Text = (Data.Files(1))
End Sub
My question is how can repeating code be moved into a module or class?
Re: VB6 OLEDragDrop - Drop file from Explorer to application
Welcome to the forums
Typically, you would move all repeating lines to a new sub or function, then call that sub/function, passing it whatever parameters are needed. In this case, since you only have one line that is repeated, you would be replacing that line with another line that calls the new sub/function.
Code:
Private Sub DoOLEDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1.Text = Data.Files(1)
End Sub
Private Sub Form_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
DoOLEDrop Data, Effect, Button, Shift, X, Y
End Sub
Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
DoOLEDrop Data, Effect, Button, Shift, X, Y
End Sub
Private Sub Label1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
DoOLEDrop Data, Effect, Button, Shift, X, Y
End Sub
Private Sub Command1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
DoOLEDrop Data, Effect, Button, Shift, X, Y
End Sub
Private Sub Combo1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
DoOLEDrop Data, Effect, Button, Shift, X, Y
End Sub
Edited: I wouldn't suggest considering control events as "repeating" lines; only the lines of code you manually add to an event.
Last edited by LaVolpe; Aug 2nd, 2020 at 12:02 PM.
Insomnia is just a byproduct of, "It can't be done"
Re: VB6 OLEDragDrop - Drop file from Explorer to application
Is there another way to do this?
Well, if any control is disabled, the drop action is targeted to the control behind that disabled control. In cases where this is feasible, you can disable those controls and allow the action to affect its container; thereby not requiring code for the disabled control.
If multiple controls of the same type are being used, then use a control array. For example, if 10 labels are being used, don't name them Label1, Label2, etc. The control array uses one name and an index, Label1(0), Label1(1), etc. In addition, that control array has just one event whereas if 10 unique label names were used: 10 events.
Otherwise, the easy answer is no, nothing that comes to mind readily. Not saying it can't be done, but the effort may be very significant.
Insomnia is just a byproduct of, "It can't be done"
Re: VB6 OLEDragDrop - Drop file from Explorer to application
There are more controls of same type. Making control arrays is possible, but the names of controls would be hard to read.
The names of controls are like this: lblCompany, lblModel, lblSerialNo, lblPartNo etc.
On the form there are mostly labels and text boxes for displaying and editing data from dropped file.