VB pop color picker - vb.net

I have a question of VB event handler and color picker.
Now I have a label, and I want when user click it, it pops up a color picker dialog and let user to change the background color of the label.
Not sure how to implement this, can anyone give me a direction?
Thank you

Use the .NET Framework's ColorDialog class in your Label's click handler.
Private Sub Label1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles Label1.Click
Dim cDialog As New ColorDialog()
cDialog.Color = Label1.BackColor ' initial selection is current color.
If (cDialog.ShowDialog() = DialogResult.OK) Then
Label1.BackColor = cDialog.Color ' update with user selected color.
End If
End Sub

here is the way to do it:
ColorDialog1.ShowDialog()
paperLabel.BackColor = Me.ColorDialog1.color

Related

How to avoid triggering MouseLeave event when hovering over a child control?

I am trying to make it so if you hover into a panel, it will change its color, and when you hover into a label, the panel will not change its color or go back to default, let me show pictures of what I am trying to explain:
If you didn't hover into anything, it would be at its default state:
If you hovered into it, panel color will change:
Then, if you hover into the label, panel color shouldn't change:
But instead, what it does is if you hover into the label, the panel color goes back to what its default was. This is my code:
Private Sub Panel13_MouseEnter(sender As Object, e As EventArgs) Handles Panel13.MouseEnter
Panel13.BackColor = Color.DarkGreen
End Sub
Private Sub Panel13_MouseLeave(sender As Object, e As EventArgs) Handles Panel13.MouseLeave
Panel13.BackColor = Color.LimeGreen
End Sub
How can I fix this problem? Is there any way to make the code clean or do I have to copy and paste the code and replace words?
You may use the GetChildAtPoint method to check if the mouse cursor is over a child control.
Replace the code in your MouseLeave event handler with the following:
Dim childControl = Panel13.GetChildAtPoint(Panel13.PointToClient(Cursor.Position))
If childControl Is Nothing Then
Panel13.BackColor = Color.LimeGreen
End If

How to change backcolor of text when textbox is readonly

I am working on windows form application.i given my text box back color is green.then i changed the textbox read only property true.but while running the back color is not appearing,
i have tried a lot to find this,but no answer ..
if any help is very appriciable.
How can I change the backcolor of the text, when the textbox is made readonly
Thanks
Add
If textBox1.ReadOnly
Then TextBox1.BackColor = Color.Blue
End If
to the event handler of the desired event.
If you want to change the back color when the form is loaded just use
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If textBox1.ReadOnly
Then TextBox1.BackColor = Color.Blue
End If
End Sub
If you want to change the color after a button is pressed or any other event is fired just implement the event handler and change the color there.
Go to code view and on the load function type in
TextBox1.BackColor = Color.Red
Obviously where TextBox1 is you'll enter the designer name for the textbox.

Setting a Button in a sub?

So, I have a little problem. Im making a application (in visual basic), and I have a way so that I can set a color when you hover over it. Now, I want to do this for all buttons, but make it a little easier by making a sub that can do this for me. The problem is, how can my sub tell which button to initialize the custom hover color? Here is my code.
Private Sub initButton(ByVal color As Color)
Button1.TabStop = False
Button1.FlatStyle = FlatStyle.Flat
Button1.FlatAppearance.BorderSize = 0
Button1.FlatAppearance.BorderColor = color
Button1.FlatAppearance.CheckedBackColor = color
Button1.FlatAppearance.MouseDownBackColor = color
Button1.FlatAppearance.MouseOverBackColor = color
End Sub
Now, how can I set Button1 to the button I want to initialize? Is there anyway to put that as an argument? If you find a answer, please reply.
As Plutonix suggested, using the Hover Event of the buttons would be the first logical choice. You will need to add a Parameter to your your Sub to pass in the button being hovered over.
Private Sub Button1_MouseHover(sender As Object, e As EventArgs)
Handles Button1.MouseHover,Button2.MouseHover 'add more buttons....
initButton(CType(sender, Button),Color.Blue)
End Sub
Private Sub initButton(hoverButton As Button, ByVal color As Color)
hoverButton.TabStop = False
hoverButton.FlatStyle = FlatStyle.Flat
hoverButton.FlatAppearance.BorderSize = 0
hoverButton.FlatAppearance.BorderColor = color
hoverButton.FlatAppearance.CheckedBackColor = color
hoverButton.FlatAppearance.MouseDownBackColor = color
hoverButton.FlatAppearance.MouseOverBackColor = color
End Sub

VB NET image as button

I'm going to change the image of button_original to button_clicked's image.
how do I change the image(button) when I click them ?
Should I need to use picture box anything else ?
In vb.NET, if you're using an image on a button, to change it, you can change the property Image of the button in the Button_Click() event.
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click
Button.Image = ... <- your image
End Sub

How to clear a textbox if i drag the text out

I have code to drag files into a textbox, but I would also like to have code to drag the file out of the textbox, thus clearing it.
Anybody have any ideas?
You can use the DoDragDrop method to do a drag & drop operation. It returns a DragDropEffects value that specifies if the data actually has been dropped somewhere in which case you can clear the text box.
Since a drag & drop operation shouldn't start before the mouse has been moved a bit while pressing the mouse button you need to check for that in the MouseDown and MouseMove events. SystemInformation.DragSize tells you how far the mouse should be moved before a drag & drop operation starts.
In the MouseDown event check if you actually want to start a drag (i.e left button is pressed and text box actually contains text). Then create a rectangle using the mouse location and the size given by SystemInformation.DragSize. In the MouseMove event check if the mouse is dragged outside the rectangle and call DoDragDrop:
Private _dragStart As Boolean
Private _dragBox As Rectangle
Private Sub srcTextBox_MouseDown(sender As Object, e As MouseEventArgs) Handles srcTextBox.MouseDown
' a drag starts if the left mouse button is pressed and the text box actually contains any text
_dragStart = e.Button = MouseButtons.Left And Not String.IsNullOrEmpty(srcTextBox.Text)
If _dragStart Then
Dim dragSize As Size = SystemInformation.DragSize
_dragBox = New Rectangle(New Point(e.X - (dragSize.Width \ 2),
e.Y - (dragSize.Height \ 2)), dragSize)
End If
End Sub
Private Sub srcTextBox_MouseUp(sender As Object, e As MouseEventArgs) Handles srcTextBox.MouseUp
_dragStart = False
End Sub
Private Sub srcTextBox_MouseMove(sender As Object, e As MouseEventArgs) Handles srcTextBox.MouseMove
If Not _dragStart Or
(e.Button And MouseButtons.Left) <> MouseButtons.Left Or
_dragBox.Contains(e.X, e.Y) Then Return
Dim data As New DataObject()
data.SetData(srcTextBox.Text)
' you can optionally add more formats required by valid drag destinations:
' data.SetData(DataFormats.UnicodeText, Encoding.Unicode.GetBytes(srcTextBox.Text))
' data.SetData("UTF-8", Encoding.UTF8.GetBytes(srcTextBox.Text))
' data.SetData("UTF-32", Encoding.UTF32.GetBytes(srcTextBox.Text))
Dim dropEffect As DragDropEffects = srcTextBox.DoDragDrop(data, DragDropEffects.Move)
If (dropEffect = DragDropEffects.Move) Then
srcTextBox.Text = ""
End If
_dragStart = False
_dragBox = Rectangle.Empty
End Sub
Private Sub destTextBox_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles destTextBox.DragOver
If e.Data.GetDataPresent(GetType(String)) Then
e.Effect = e.AllowedEffect And DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub destTextBox_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles destTextBox.DragDrop
If e.Data.GetDataPresent(GetType(String)) Then
destTextBox.Text = e.Data.GetData(GetType(String)).ToString()
End If
End Sub
Dragging the mouse in a TextBox usually starts text selection. The above code changes this behavior. Users can't use the mouse any more to select text. This is obviously not a good idea since users wouldn't expect that. To allow both text selection with the mouse and dragging you need to control the selection mechanism. This means you need to create your own text box class.
I would suggest a different approach: Use a label that shows the value as the dragging source and/or destination. To allow editing you can create a hidden text box. If the user double clicks on the label you hide the label and show the text box. After the user finishes editing (by hitting enter or cancel) or if the text box looses focus you hide the text box and show the label again.
Check out the DragLeave Event on the TextBox
Easiest way will probably be to store it as a variable or in clip board if leaving app.
Private Sub TheTextBox_DragLeave(sender As System.Object, e As System.EventArgs) Handles TheTextBox.DragLeave
Clipboard.SetText(TheTextBox.Text)
TheTextBox.Clear() 'Optional
End Sub
Then you'll need to code what happens when you mouse up of course, or drag into or whatever. You could clear the textbox in one of these steps. Depends on your implementation.