Loading pictures with code - vb.net

Private Sub frmPegSolitaire_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each pictire As PictureBox In Me.Controls
If Not pictire.Tag.Equals("n") Then
pictire.Image = Image.FromFile("peg.png")
End If
Next
End Sub
Here is my code that does not work. What am I doing wrong?

You're looping over all the Controls contained in Me (probably the Form)
In that collection there is more than just the PictureBoxes so you need to filter to only get those :
(see OfType)
Private Sub frmPegSolitaire_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each pictire As PictureBox In Me.Controls.OfType(Of PictureBox)
If Not pictire.Tag.Equals("n") Then
pictire.Image = Image.FromFile("peg.png")
End If
Next
End Sub

Related

Add/modify handler in multiple ComboBoxes

I want to disable the mousewheel to prevent scrolling in the ComboBoxes.
For one ComboBox this works:
Private Sub CmbDienst_MouseWheel(sender As Object, e As MouseEventArgs) Handles CmbDienst.MouseWheel
Dim HMEA As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
HMEA.Handled = True
End Sub
But how can I add this to ALL ComboBoxes? There are a lot of them in the form.
I was looking for something like
Private Sub Combo_Mouse()
For Each c As Control In Me.Controls.OfType(Of ComboBox)()
'And then...?
Next
End Sub
Thanks!
It works. The problem I had was that the comboboxes are in several containers, such as Panels and Datagridviews. Then is "me.controls, etc" not enough.
So I finally made this out of it:
In the Form load:
EnumControls(Me)
In the Programm:
Private Sub ComboBoxes_MouseWheel(sender As Object, e As MouseEventArgs)
Dim hmea = DirectCast(e, HandledMouseEventArgs)
hmea.Handled = True
End Sub
Private Sub EnumControls(ByVal ctl As Control)
If ctl.HasChildren Then
For Each c As Control In ctl.Controls
For Each comboBox In c.Controls.OfType(Of ComboBox)()
AddHandler comboBox.MouseWheel, AddressOf ComboBoxes_MouseWheel
Next
EnumControls(c)
Next
End If
End Sub
It works. Suggestions are welcome!
Try this
Private Sub buttonHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
mwe.Handled = True
End Sub
and in FormLoad
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each c As Control In Me.Controls.OfType(Of ComboBox)()
'You cann acces to ComboBox her by c
AddHandler c.MouseWheel, AddressOf buttonHandler
Next
End Sub

How to use array when declaring sub VB

I was wondering is there was a way to use an array when declaring a sub.
Example:
Public Class Form1
Private PictureBox(2) as PictureBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBox(0) = PictureBox1
PictureBox(1) = PictureBox2
PictureBox(2) = PictureBox3
Private Sub PictureBox_mouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox(1).MouseDown
'do stuff here
end sub
This is the method Plutonix suggested in the comments applied to your code. Note that no array is needed.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is PictureBox Then
AddHandler DirectCast(ctrl, PictureBox).MouseDown, AddressOf PictureBox_MouseDown
End If
Next
End Sub
Private Sub PictureBox_MouseDown(sender As Object, e As MouseEventArgs)
'Code here will apply to every PictureBox in your form
End Sub
End Class
Also note that instead of DirectCast(ctrl, PictureBox).MouseDown, you could just use ctrl.MouseDown. Using DirectCast simply helps you avoid making mistakes by cluing IntelliSense and the compiler in on what you're doing.

Backcolor not Coloring Entire Cell only Text

For the internet I pulled and amended some code to change the backcolor of the current cell in a datagridview upon editing the text. The problem is, the backcolor only seems to change within the cells text and not the entire cell (there seems to be a 3 pixel margin in the cell that has the original color). Below is the code I'm using:
Public Class Form1
Dim a, b As Integer
Dim myvalue As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
a = 0
Do While a < 10
Me.DataGridView1.Rows.Add()
a += 1
Loop
End Sub
Private EditingControl As DataGridViewTextBoxEditingControl
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
EditingControl = e.Control
AddHandler EditingControl.TextChanged, AddressOf EditingControl_TextChanged
End Sub
Private Sub EditingControl_TextChanged(sender As Object, e As EventArgs)
EditingControl.BackColor = Color.White
End Sub
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
If EditingControl IsNot Nothing Then
RemoveHandler EditingControl.TextChanged, AddressOf EditingControl_TextChanged
End If
EditingControl = Nothing
End Sub
End Class
Does anybody know what I've done wrong with the code?
The EditingControl is put on a panel and has a 3 pixels margin.
Colouring the parent panel solves your problem :
Private Sub EditingControl_TextChanged(sender As Object, e As EventArgs)
EditingControl.BackColor = Color.White
EditingControl.Parent.BackColor = Color.White
End Sub
By the way, DataGridView class already has an EditingControl property.

VB.NET Save Combo Box to My.Settings

Looking to save combobox items to my.settings collection. I developed a webbrowser and a combobox will be my address bar. I am attempting to save a history of visited sites.
I tried the below code and it doesnt work. It errors out with "Object reference not set to an instance of an object":
Went into settings added MyItems for the name, and then select System.Collections.Specialized.StringCollection as the data type. Then onload is the below:
For Each i As String In My.Settings.MyItems
ComboBox1.Items.Add(i)
Next
FormClosing and ive tried FormClosed: For now i put it in a button event to save it for testing
My.Settings.MyItems.Clear()
For Each i As String In ComboBox1.Items
My.Settings.MyItems.Add(i)
Next
I love this site very much! So I came back to post the correct code that will correctly save and load combobox entries to my.setting! This has been tested as working!!!
Private Sub Form1_FormClosing(
sender As Object,
e As FormClosingEventArgs) Handles Me.FormClosing
My.Settings.Categories.Clear()
For Each item In ComboBox1.Items
My.Settings.Categories.Add(item.ToString)
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each item In My.Settings.Categories
ComboBox1.Items.Add(item)
Next
ComboBox1.SelectedIndex = 0
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ComboBox1.Items.Add(TextBox1.Text)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If ComboBox1.Items.Count > 0 Then
Dim Index As Int32 = ComboBox1.SelectedIndex
ComboBox1.Items.RemoveAt(Index)
If Index - 1 <> -1 Then
ComboBox1.SelectedIndex = Index - 1
End If
End If
End Sub

Check if mouse pointer enteres panel components

How do I check if my mouse cursor enters any component in a panel .
I stared to write this code to check when the mouse enters any check box in my panel then I realised that I had no idea how to actually check if the mouse enters the components I the panel .
Private Sub GenCheck()
For Each CheckBox In datapanel1.Controls
Next
End Sub
how do i go about doing this ?
Edit
I have an Idea but i'm not too sure about it
I could say
Private Sub GenCheck()
Dim cb As CheckBox
For Each cb In datapanel1.Controls
AddHandler cb.MouseEnter, AddressOf cb_MouseEnter
AddHandler cb.MouseLeave, AddressOf cb_MouseLeave
Next
End Sub
Private Sub cb_MouseEnter(sender As Object, e As EventArgs)
End Sub
Private Sub cb_MouseLeave(sender As Object, e As EventArgs)
End Sub
You can use MouseHover to determine when the cursor hovers above the checkbox like this.
Private Sub CheckBox1_MouseHover(sender As Object, e As System.EventArgs) Handles CheckBox1.MouseHover
MsgBox("Mouse over!")
End Sub`
Edit:
I have put a panel on a form with two checkboxes to mimic your requirements, here is what you're looking for:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
For Each pnlCheckBox As CheckBox In Panel1.Controls
AddHandler pnlCheckBox.MouseHover, AddressOf Me.CheckBoxMouseOver
Next
End Sub
Private Sub CheckBoxMouseOver()
MsgBox("Mouse over!")
End Sub
*PERFECT WAY TO ACCOMPLISH THIS *
Dim con As Control
For Each con In datapanel1.Controls
AddHandler con.MouseEnter, AddressOf con_MouseEnter
AddHandler con.MouseLeave, AddressOf con_MouseLeave
Next
End Sub
Private Sub con_MouseEnter(sender As Object, e As EventArgs)
'DO SOMETHING'
End Sub
Private Sub con_MouseLeave(sender As Object, e As EventArgs)
'DO SOMETHING'
End Sub