Drag and drop Text Box to List Box - vb.net

I'm working on a VB.net(2015) code that would allow me to drag and drop from a text box to a list box.
I've set up the code below, and set the allow drop in the list box to true.
I’m able to drag the text to the list box, but it doesn’t appear in the list box.
Looking at the code below I’m not sure what piece I’ve left out, are there any recommendations or suggestions on what can be done?
Public Class PotluckParty
Private Sub enterFoodTextBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles enterFoodTextBox.MouseDown
enterFoodTextBox.DoDragDrop(enterFoodTextBox.Text, DragDropEffects.Move)
End Sub
Private Sub SaladListBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles SaladListBox.DragDrop
SaladListBox.Text &= e.Data.GetData(DataFormats.Text).ToString
enterFoodTextBox.Text = ""
End Sub
Private Sub SaladListBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles SaladListBox.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text)) Then
e.Effect = DragDropEffects.Move
End If
End Sub

Related

Visual Basic 2008 Check if label is inside a panel

So i've been trying to make a game with arrow keys.
The label is the character, if the label is inside the object, it will do something...
But i got a very hard problem i cannot find on the internet.
How do i check if a label is inside of an object? Example: Picture box, and Panel.
I've tried this one.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Label1.Location.X = Panel1.Location.X And Label1.Location.Y = Panel1.Location.Y Then
Me.Close() 'Any code.
End If
End Sub
Doesn't work,
any help would be appreciated.
I am a beginner by the way, i only make simple applications. Like escape the room, maze... etc.
I would use the label's parent property to see if it is inside the panel.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Label1.Parent IsNot Nothing AndAlso Label1.Parent Is Panel1 Then
Me.Close() 'Any code.
End If
End Sub
If you are looking to see if the Label is over the Panel I would try something like this. The ClientRectangle property is the rectangle the control takes up. I am assuming the panel is bigger than the Label.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Panel1.ClientRectangle.Contains(Label1.ClientRectangle) Then
Me.Close() 'Any code.
End If
End Sub
If it is smaller you can check if a point is in the Panel's rectangle. For example top left corner
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Panel1.ClientRectangle.Contains(new Point(Label1.Left, Label1.Top)) Then
Me.Close() 'Any code.
End If
End Sub

VB.NET optimizing drag & drop code

I'm currently struggling with drag & drop code. I have 3 images that are at the top, and I want to add them in a random order in a flow-layout-panel.
I have this code for adding the square image into the flow-layout-panel, but I have got the feeling that this is not 100% correct. Is it possible to add these with 1 sub instead of 3?
And how do you write a sub that detects what object in being dragged? Now my sub just adds a square with each dragdrop event. But I need it to drop a Square only when a Square is being dragged and drop a Trapezium or round when it's being dragged.
Public Class Form2
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
' Initiate dragging.
PictureBox1.DoDragDrop(PictureBox1, DragDropEffects.Copy)
End Sub
Private Sub PictureBox2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove
' Initiate dragging.
PictureBox2.DoDragDrop(PictureBox2, DragDropEffects.Copy)
End Sub
Private Sub PictureBox3_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox3.MouseMove
' Initiate dragging.
PictureBox3.DoDragDrop(PictureBox2, DragDropEffects.Copy)
End Sub
Private Sub FlowLayoutPanel1_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(GetType(PictureBox))) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub FlowLayoutPanel1_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragDrop
Dim oPB As New PictureBox()
oPB.Image = Image.FromFile("C:\Users\Jef\Desktop\square.jpg")
oPB.Visible = True
oPB.Width = 100
oPB.Height = 100
oPB.SizeMode = PictureBoxSizeMode.CenterImage
FlowLayoutPanel1.Controls.Add(oPB)
End Sub
You already got the answer in your previous question:
Private Sub FlowLayoutPanel1_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragDrop
Dim oPB As New PictureBox()
Dim pb = CType(e.Data.GetData(GetType(PictureBox)))
oPB.Image = pb.Image
pb.Image = Nothing '' Optional
'' etc...
End Sub
You do have a bug, the probable reason why you did not use the answer:
Private Sub PictureBox3_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox3.MouseMove
' Initiate dragging.
PictureBox3.DoDragDrop(PictureBox2, DragDropEffects.Copy)
End Sub
Note how it drags the wrong control, PictureBox2 instead of PictureBox3. You avoid bugs like this by writing DRY code, Do not Repeat Yourself. The sender argument of a MouseMove event already gives you a reference to the control. So you just need one event handler for all three controls:
Private Sub PictureBox_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove, PictureBox2.MouseMove, PictureBox3.MouseMove
' Initiate dragging.
Me.DoDragDrop(sender, DragDropEffects.Copy)
End Sub
With the detail that we now let the Form support DoDragDrop(). Which only matters if you implement the GiveFeedback or QueryContinueDrag events.

Add a checkbox to allow the labels to be updated whenever the input text changes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am very new to programming language and am in my first Programming class at my university. For a project we are creating a VERY simple project where the user enters words and the program puts them together. It's day one stuff I know but I'm trying to go for the extra credit and "Add a check box to allow the labels to be updated whenever the input text changes".
I have a program that when you type in two separate words in separate text boxes it displays each word individually and then also the two words combined at the bottom. Our professor wants my to add an check box option at the bottom that when clicked with make it so when the users types the two words they display automatically without clicking the display button.
I know this is easy stuff, but any help would be much appreciated.
Thank you for your help.
Public Class form1
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblLeft.Click
End Sub
Private Sub lbl2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblRight.Click
End Sub
Private Sub txtBoxLeft_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBoxLeft.TextChanged
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim strTxtBoxLeft As String
strTxtBoxLeft = txtBoxLeft.Text
Dim strTxtBoxRight As String
strTxtBoxRight = txtBoxRight.Text
lblLeft.Text = strTxtBoxLeft
lblRight.Text = strTxtBoxRight
lblCombo.Text = strTxtBoxLeft & " " & strTxtBoxRight
End Sub
Private Sub chkbox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbox.CheckedChanged
If chkbox.CheckState = False Then
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
Try this:
Public Class Form1
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
combineText()
End Sub
Private Sub chkbox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbox.CheckedChanged
If chkbox.CheckState = False Then
lblCombo.Text = ""
End If
End Sub
Private Sub txtBox_TextChanged(sender As Object, e As EventArgs) Handles txtBoxRight.TextChanged, txtBoxLeft.TextChanged
If chkbox.CheckState Then
combineText()
End If
End Sub
Private Sub combineText()
lblCombo.Text = txtBoxLeft.Text & " " & txtBoxRight.Text
End Sub
End Class
Note: I have consolidated your logic down to just grab the text from the text boxes themselves. There is also one handler that handles text changing in either of the text boxes. Finally, there is a single method that handles combining the values of the two text boxes together, either via the text changed event if the check box is checked or if the user clicks the update button. Also, if the user unchecks the check box, then it will clear the results. Then when the user clicks update it will display the combined text again.

How to check focused TextBox in vb.net winforms?

I have multiple textbox in a form. How do I know what textbox the cursor currently is?
Trying to do something like this:
If TextBox2.Focus() = True Then
MessageBox.Show("its in two")
ElseIf TextBox3.Focus = True Then
MessageBox.Show("its in three")
End If
But I think its not working.
TextBox.Focus actually assigns the focus to the given textbox. What you're looking for is TextBox.Focused. :)
In fact, all form controls have the Focused property.
I know this already has an accepted answer but I just think this method is a bit easier and should be up here for people who find this through Google or whatever.
Public focussedTextBox As TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each control As Control In Me.Controls
If control.GetType.Equals(GetType(TextBox)) Then
Dim textBox As TextBox = control
AddHandler textBox.Enter, Sub() focussedTextBox = textBox
End If
Next
End Sub
This way you can then just refer to the focussedTextBox at any time. You should make sure that you check that there is a focussedTextBox before you do however becuase when the application first loads there will not be. You can do this using:
If Not focussedTextBox Is Nothing Then
...
End If
Alternatively, you could set focussedTextBox to a TextBox of your choice on form load, either by setting its value or by focussing the TextBox.
Obviously, it will not work if you are calling your code in a Button_Click because when you click the Button then the focus is itself goes to the Button which you have clicked.
You can do two things:
Make a combined Focus event for all TextBoxes and check its Sender object.
Private Sub TextBox_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter, TextBox3.Enter
Dim currTextBox As TextBox = sender
If currTextBox.Equals(TextBox2) Then
MessageBox.Show("it's in two")
ElseIf currTextBox.Equals(TextBox3) Then
MessageBox.Show("it's in three")
End If
End Sub
OR
Take a global string variable, and set its value at each TextBox_Focus event, then check string value in the button click event.
Dim str As String
Private Sub TextBox2_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter
str = "two"
End Sub
Private Sub TextBox3_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.Enter
str = "three"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("it's in " & str)
End Sub

Drag drop a usercontrol in vb.net

I have, what should be , a very simple problem, but now I have used 5 hours without results.
I have a usercontrol, UserControl1, which I want to drag and drop on my form, Form1.
That’s it. It should be simple, but I have googled for hours without results. Does anybody have a sample code to fix this?
I dont know what a user control is (I'm still learning) but I found something that might help.
In this code, add two TextBox controls to a form and set the AllowDrop property of the second TextBox control to True.
Then use this code to enable drag and drop
Private MouseIsDown As Boolean = False
Private Sub TextBox1_MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
' Set a flag to show that the mouse is down.
MouseIsDown = True
End Sub
Private Sub TextBox1_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove
If MouseIsDown Then
' Initiate dragging.
TextBox1.DoDragDrop(TextBox1.Text, DragDropEffects.Copy)
End If
MouseIsDown = False
End Sub
Private Sub TextBox2_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(DataFormats.Text)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles TextBox2.DragDrop
' Paste the text.
TextBox2.Text = e.Data.GetData(DataFormats.Text)
End Sub
I hope that you can use this for a usercontrol. Good luck and comment!
Here is the code, I have used, to get it work.
Now I have a form, Form1, and a usercontrol, Usercontrol1. To drag the usercontrol, I inserted a panel in the top of the usercontrol, and only if the user pressed the panel (panel1), the control should to move - like normal windows forms.
Public Class UserControl1
Shared mypositionX As Integer
Shared mypositionY As Integer
Shared mBlnFormDragging As Boolean
Shared drawBeginX As Integer
Shared drawBeginY As Integer
Shared drawing As Boolean
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If mBlnFormDragging = True Then
Dim position As Point = Form1.PointToClient(MousePosition)
Me.Location = New Point(position)
End If
End Sub
Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
' Dim dd1 As DragDropEffects = DoDragDrop(ParentForm, DragDropEffects.Move)
mBlnFormDragging = False
Dim position As Point = Form1.PointToClient(MousePosition)
Location = New Point(position)
End Sub
Public Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
'Dim dd1 As DragDropEffects = DoDragDrop(ParentForm, DragDropEffects.Move)
mBlnFormDragging = True
End Sub