Visual Studio - Perform a event action for "X" number of checkboxes? - visual-studio

I have 20 checkboxes.
This is the event for the checkbox nÂș1 :
Public Sub C1CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles C1CheckBox1.CheckedChanged
If C1CheckBox1.Checked = True Then My.Settings.box1_selected = "Y" Else My.Settings.box1_selected = "N"
Dim checkedpath1 = C1CheckBox1.Text
End Sub
I know I can add all the checkboxes in the "handles", but my number of checkboxes is undetermined and this is what I want to do:
(Pseudocode)
Public Sub ALL_THE_CHECKBOXES_CheckedChanged(sender As Object, e As EventArgs) Handles ALL_THE_CHECKBOXES.CheckedChanged
If ANY_CHECKBOX.Checked = True
My.Settings.boxNUMBER_OF_THIS_SELECTED_CHECKBOX_selected = "Y"
Else
My.Settings.boxNUMBER_OF_THIS_SELECTED_CHECKBOX_selected = "N"
End If
Dim checkedpathNUMBER_OF_THIS_SELECTED_CHECKBOX = C1CheckBoxNUMBER_OF_THIS_SELECTED_CHECKBOX.Text
End Sub
I need to generate a event that handles an undetermined number of checkboxes,
I need to do the same action if any of the checkboxes is selected, but only in that selected checkbox.
Basically I want to remember in the settings which checkboxes was selected and which not...
UPDATE:
At form load I create ALL the form checkboxes with this code:
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim List As Integer = 0
Dim posy As Integer = 10
filesystem = CreateObject("Scripting.FileSystemObject")
ThisDir = filesystem.GetFolder(My.Settings.folderpath)
For Each folder In ThisDir.Subfolders
List = List + 1
posy = posy + 20
Dim newCheckBox As New CheckBox()
Panel1.Controls.Add(newCheckBox)
newCheckBox.Name = "checkbox" & List.ToString()
newCheckBox.Text = folder.name
newCheckBox.Location = New Point(10, posy)
Next
End Sub
That creates a checkboxes named "checkbox1", "checkbox2", "checkbox3", etc...

All checkboxes are inside of another control. might be form, panel whatever. So you can cycle through these checkboxes and manually assign the eventhandler for each checkbox you find
Revised Sample code:
You need a setting of type StringCollection named MyCBs - or you can use whatever name you like, just make the necessary changes to the code.
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
My.Settings.Save()
End Sub
Private Sub AnyCB_CheckedChanged(sender As Object, e As EventArgs)
Dim cb = DirectCast(sender, CheckBox)
If cb.Checked AndAlso Not My.Settings.MyCBs.Contains(cb.Name) Then
My.Settings.MyCBs.Add(cb.Name)
ElseIf Not cb.Checked AndAlso My.Settings.MyCBs.Contains(cb.Name) Then
My.Settings.MyCBs.Remove(cb.Name)
End If
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
If My.Settings.MyCBs Is Nothing Then My.Settings.MyCBs = New Collections.Specialized.StringCollection
For Each s In My.Settings.MyCBs
DirectCast(Me.Controls(s), CheckBox).Checked = True
Next
For Each cb In Me.Controls.OfType(Of CheckBox)()
AddHandler cb.CheckedChanged, AddressOf AnyCB_CheckedChanged
Next
End Sub
This sample saves the checked status of the checkboxes in my.settings inside a StringCollection. If a CB is checked it's name is added to the collection, if it's unchecked, it's name is removed. This means on startup you can simply check which CB name is in the collection and sets its checked stats to true. Finally you use AddHandler to execute the code for any CB contained in your Form.
Note that there's NO error handling inside the code, which means that this is your part ...

Related

show confirm close message in all project forms vb.net?

I have Project containing multi forms
main page and others inside main page
used next code to make confirm close in main page
Public Sub MyForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles MyBase.FormClosing
If MessageBox.Show("Do you want to close the form?", "Confirm", MessageBoxButtons.YesNo) <> DialogResult.Yes Then
e.Cancel = True
End If
End Sub
and want the confirm message to show in all forms not only main page !
Here is a process I use because MessageBox while it is nice I like to design my own.
You will see some variables in the code that look like this
Public gvalertType As String
Public gvRESEdit As String
These are declared in a Module
Now we need some code for the frmAlert You will use If and ElseIf to trap multiple errors. I will post a screen shot of the frmAlert
Private Sub frmAlert_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If gvalertType = "10" Then
btnNO.Visible = True
lblAI.Text = " Caution "
tbAlert.Text = "OK To Delete " & vbCrLf & vbCrLf & "NO Do NOT Delete"
End If
End Sub
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
If btnNO.Visible = True Then
gvRESDelete = "YES"
End If
Close()
End Sub
Private Sub btnNO_Click(sender As Object, e As EventArgs) Handles btnNO.Click
gvRESDelete = "NO"
Close()
End Sub
OK Now on the form and button click that calls the frmAlert.
Because you are changing forms this is why the variables are declared in a Module.
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
gvalertType = "10"
frmAlert.ShowDialog()
If gvRESDelete = "NO" Then
btnBack.Focus()
Return
End If
If gvRESDelete = "YES" Then
DeleteSiteData()
End If
End Sub

VisualBasic Make and Model ComboBox

My goal is to have this program able to tell which make is selected and offer a list of models based on that. However, when I click the ComboBox, execute the program, and select a Make, I am not getting any selections in my Model ComboBox. Can anyone help me with this? Here is my source code:
Option Explicit On
Option Strict On
Public Class Form1
Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Initialize the text property of the three Combo Boxes to a NULL string
ComboBoxMake.Text = ""
ComboBoxModel.Text = ""
ComboBoxColor.Text = ""
'Add a number of cars to the "Make" ComboBox.
ComboBoxMake.Items.Add("Honda")
ComboBoxMake.Items.Add("Toyota")
ComboBoxMake.Items.Add("Ford")
ComboBoxMake.Items.Add("Lexus")
End Sub
Private Sub ComboBoxMake_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxMake.SelectedIndexChanged
Dim SelectedItem As String
SelectedItem = CStr(ComboBoxMake.SelectedItem()) 'Return the selected item from the ComboBox
MessageBox.Show(SelectedItem) 'Display the selected item
End Sub
Private Sub ComboBoxModel_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxModel.SelectedIndexChanged
Dim SelectedItem As String
SelectedItem = CStr(ComboBoxMake.SelectedItem()) ' Return the selected item from the ComboBox
If SelectedItem = "Honda" Then
ComboBoxModel.Text = "" 'Initialize the text property to a NULL string
ComboBoxModel.Items.Clear()
ComboBoxModel.Items.Add("Accord")
ComboBoxModel.Items.Add("Civic")
ComboBoxModel.Items.Add("CRV")
ComboBoxModel.Items.Add("Pilot")
ComboBoxModel.Items.Add("Odyssey")
ElseIf SelectedItem = "Toyota" Then
ComboBoxModel.Text = "" 'Initialize the text property to a NULL string
ComboBoxModel.Items.Clear()
ComboBoxModel.Items.Add("Camrey")
ComboBoxModel.Items.Add("Avalon")
ComboBoxModel.Items.Add("4Runner")
Else
ComboBoxModel.Items.Add("Not Available")
End If
End Sub
End Class
Try using double equals when comparing the String values.

Custom MsgBox without form activate being fired

I have developed a custom MsgBox that works fine in almost every way. The only problem is that when the MsgBox closes the parent form runs the Form_Activate code. A normal MsgBox does not run that code (again).
I know i could add a boolean variable to Form_Activate to check if it has fired already, but that's not the best solution when you have a dozen forms.
So, is there a way to not run Form_Activate after closing my custom MsgBox? Does the MsgBox form need to be of some special type or something? I tried all BorderStyles but that doesn't make any difference.
Are you using another form to make custom MsgBox?
You shouldn't use directly other form to show a custom messagebox.
You should create an Activex control, and Activate event won't fire again when the MsgBox is closed.
Within the control you can use a form if you want it. (Probably just have to place your code inside an ActiveX control project and use it in your forms)
I use it that way.
This is a custom MsgBox example using Activex Control, with a test form too.
http://www.codeguru.com/code/legacy/vb_othctrl/2166_CustomMsgBox.zip
I created a Class for a Custom MsgBox.
Public Class CustomMsgBox
'Creates the Main form
Dim Main As New Form
'Creates the buttons
Dim Btn1, Btn2, Btn3 As New Button
'Creates the label
Dim Lbl As New Label
'Creates the Output variable
Dim Output As Integer = 0
Private Sub Load()
'Btn1 properties
Btn1.Location = New Point(168, 69)
Btn1.AutoSize = True
Btn1.AutoSizeMode = AutoSizeMode.GrowOnly
'Btn2 properties
Btn2.Location = New Point(87, 69)
Btn1.AutoSize = True
Btn1.AutoSizeMode = AutoSizeMode.GrowOnly
'Btn3 properties
Btn3.Location = New Point(6, 69)
Btn1.AutoSize = True
Btn1.AutoSizeMode = AutoSizeMode.GrowOnly
'Lbl properties
Lbl.Location = New Point(12, 19)
Lbl.AutoSize = True
Lbl.AutoEllipsis = True
'Main form properties
Main.Size = New Size(211, 129)
Main.AutoSize = True
Main.AutoSizeMode = AutoSizeMode.GrowOnly
Main.ShowIcon = False
Main.Controls.Add(Btn1)
Main.Controls.Add(Btn2)
Main.Controls.Add(Btn3)
Main.Controls.Add(Lbl)
'Adds Handlers to the buttons
AddHandler Btn1.Click, AddressOf btn1_Click
AddHandler Btn2.Click, AddressOf btn2_Click
AddHandler Btn3.Click, AddressOf btn3_Click
End Sub
Function CstMsgBox(ByRef Msg As String, ByRef Title As String, ByRef B1 As String, Optional ByRef B2 As String = Nothing, Optional ByRef B3 As String = Nothing) As Integer
'Runs the Load() Sub
Load()
'Sets the values
Lbl.Text = Msg
Btn1.Text = B1
Btn2.Text = B2
Btn3.Text = B3
Main.Text = Title
'Checks if there is a value set to Btn2 and Btn3
If Btn2.Text = Nothing Then
Btn2.Hide()
End If
If Btn3.Text = Nothing Then
Btn3.Hide()
End If
'Shows the MsgBox
Main.Show()
'Waits until a button is pressed
Do Until Output <> 0
Application.DoEvents()
Loop
'Closes the MsgBox
Main.Close()
Return Output
End Function
Private Sub btn1_Click(ByVal sender As Object, ByVal e As EventArgs)
'Sets the Output value to 1
Output = 1
End Sub
Private Sub btn2_Click(ByVal sender As Object, ByVal e As EventArgs)
'Sets the Output value to 2
Output = 2
End Sub
Private Sub btn3_Click(ByVal sender As Object, ByVal e As EventArgs)
'Sets the Output value to 3
Output = 3
End Sub
End Class
You can use it by typing this:
Dim CMB As New CustomMsgBox
CCMB.CstMsgBox('MSG, 'TITLE, 'BUTTON1, 'Optional: BUTTON2, 'Optional: BUTTON3)
OR
Dim CMB As New CustomMsgBox
Select Case CMB.CstMsgBox('MSG, 'TITLE, 'BUTTON1, 'Optional: BUTTON2, 'Optional: BUTTON3)
Case 1
'Code to execute when button1 is pressed
Case 2
'Code to execute when button2 is pressed
Case 3
'Code to execute when button3 is pressed
End Select

SelectedIndex of a DataGridViewComboBoxCell? VB.NET

How do I set SelectedIndex of a DataGridViewComboBoxCell?
The code fill the combobox with items, but I need to select one of them
My Code:
Dim cListItems As New System.Collections.Generic.List(Of Combobox_values)
If ds.Tables("items_prices").Rows(0).Item("item_selldozen") > 0 Then
Dim item_selldozen As String = ds.Tables("items_prices").Rows(0).Item("item_selldozen")
cListItems.Add(New Combobox_values("Docena (" + item_selldozen + ")", item_selldozen))
End If
Dim dgvcbc As DataGridViewComboBoxCell = DirectCast(CType(main.ActiveMdiChild, discount_new_discount).discountitems_new_discount.Rows(last_row).Cells(3), DataGridViewComboBoxCell)
dgvcbc.DataSource = cListItems 'Fill Remote Comboboxcell
dgvcbc.DisplayMember = "Text"
dgvcbc.ValueMember = "Value"
If you have a ComboBoxColumn in your DataGridView and you want to know what is the selected index of the combo box, then you need to do this:
Handle the EditingControlShowing event of DataGridView. In this event handler, check if the current column is of our interest. Then we
create a temporary ComboBox object and get the selected index:
Private Sub dataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs)
If dataGridView1.CurrentCell.ColumnIndex = 0 Then
' Check box column
Dim comboBox As ComboBox = TryCast(e.Control, ComboBox)
comboBox.SelectedIndexChanged += New EventHandler(AddressOf comboBox_SelectedIndexChanged)
End If
End Sub
Private Sub comboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim selectedIndex As Integer = DirectCast(sender, ComboBox).SelectedIndex
MessageBox.Show("Selected Index = " & selectedIndex)
End Sub

Save Windows Form Size

I'm developing a piece in VB.NET. Inside my primary form, I'm creating a new form to use as a dialog. I was wondering if there was a way to, upon the close of the new dialog, save it's size settings for each user (probably in a file on their machine, through XML or something?)
you can save it to the settings file, and update it on the 'onclosing' event.
to make a setting goto Project Properties ->settings -> then make a setting like 'dialogsize' of type system.drawing.size.
then do this in your dialog form:
Public Sub New()
InitializeComponent()
End Sub
Public Sub New(ByVal userSize As Size)
InitializeComponent()
Me.Size = userSize
End Sub
Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
MyBase.OnClosing(e)
My.Settings.DialogSize = Me.Size
My.Settings.Save()
End Sub
do something like this to check and use the setting:
Dim dlg As MyDialogWindow
If My.Settings.DialogSize.IsEmpty Then
dlg = New MyDialogWindow()
Else
dlg = New MyDialogWindow(My.Settings.DialogSize)
End If
dlg.ShowDialog()
Although this is for C#, it will help with VB.Net as well.
You can also add a new setting to your application (size) and set it to system.drawing.size
Then, you make sure you save the current size to settings on close.
Private Sub myForm_FormClosing(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing
My.Settings.size = Me.Size
My.Settings.Save()
End Sub
and on load you apply the size you have saved in settings
Private Sub myForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
' if this is the first time to load the form
' dont set the size ( the form will load with the size in the designe)
If Not My.Settings.size.IsEmpty Then
Me.Size = My.Settings.size
End If
End Sub
Here's a solution that I found online that seems to work rather well for me.
Some of the previously mentioned solutions weren't working for me as expected. Depending on where my form was positioned at the time of closing the form wouldn't get repositioned back to that exact location when I would load it again.
This solution seems to do the trick by taking into account some other factors as well:
You need to set up these two setting under Project Properties -> settings: WindowLocation and WindowSize like so:
Then create the following function:
Private Sub LoadWindowPosition()
'Get window location/position from settings
Dim ptLocation As System.Drawing.Point = My.Settings.WindowLocation
'Exit if it has not been set (X = Y = -1)
If (ptLocation.X = -1) And (ptLocation.Y = -1) Then
Return
End If
'Verify the window position is visible on at least one of our screens
Dim bLocationVisible As Boolean = False
For Each S As Screen In Screen.AllScreens
If S.Bounds.Contains(ptLocation) Then
bLocationVisible = True
Exit For
End If
Next
'Exit if window location is not visible on any screen
If Not bLocationVisible Then
Return
End If
'Set Window Size, Location
Me.StartPosition = FormStartPosition.Manual
Me.Location = ptLocation
Me.Size = My.Settings.WindowSize
End Sub
Next, you'll need to add code to your form's load and closing events like so:
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadWindowPosition()
End Sub
Private Sub frmMain_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
My.Settings.WindowLocation = Me.Location
My.Settings.WindowSize = Me.Size
End Sub
I hope that helps.
You can also do this using the UI provided by the VB.NET IDE itself. In the properties pane for a form, look under the item called "(Application Settings)" and then under "Property Binding." You can bind just about every property of the form (including size and location) to a settings value for that application.
As it turns out, I found a way to do this using the System.IO.IsolatedStorage

Resources