Getting a specific Settings in Sub and manipulating it - vb.net

I want a sub which gets a already existing variable from My.Settings and save it with NewValue variable. The example below shows what I need, but the problem is My.Settings.a is not from a Settings property, but a String one.
Private Sub Testing()
ThisSettings(My.Settings.a, "123")
ThisSettings(My.Settings.b, "456")
ThisSettings(My.Settings.c, "789")
End Sub
Private Sub ThisSettings(Sett As Settings, NewValue As String)
Sett = NewValue
My.Settings.Save()
End Sub

Related

Removing items in a collection based on listbox string

Having issues when clicking the remove button. If more of my code is needed, let me know. I get this error on the line AddressList.Remove(selectedName):
System.ArgumentException: 'Argument 'Key' is not a valid value.
I've tried many variations but can't figure out why this doesn't work. I think it has something to do with how the strings are concatenated in the listbox. I need to be able to remove entries from the collection and the listbox. Any help would be greatly appreciated.
Module EmailCollection
Public AddressList As New Collection
Public Sub AddRecord(ByVal a As cAddress)
Try
AddressList.Add(a)
Catch ex As Exception
MessageBox.Show("Error: inputs must be characters valid in string format")
End Try
End Sub
End Module
public class form1
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim frmAdd As New AddNewName
frmAdd.ShowDialog()
UpdateListBox()
End Sub
Private Sub UpdateListBox()
lstAddress.Items.Clear()
Dim a As cAddress
For Each a In AddressList
lstAddress.Items.Add(String.Concat(a.strName, a.strEmail, a.strPhone, a.strComment))
Next
If lstAddress.Items.Count > 0 Then
lstAddress.SelectedIndex = 0
End If
End Sub
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
Dim selectedName As String
Try
' Get the selected value.
selectedName = lstAddress.SelectedItem.ToString()
' Remove the selected name from the list box and the collection.
If MessageBox.Show("Are you sure?",
"Confirm Deletion",
MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
lstAddress.Items.Remove(selectedName)
AddressList.Remove(selectedName)
End If
Catch ex As NullReferenceException
MessageBox.Show("Select an item to remove.", "Selection Needed")
End Try
End Sub
end class
In your Module I changed AddressList from the old VB6 Collection type to the .net List(Of T). The T stands for Type.
Module EmailCollection
Public AddressList As New List(Of cAddress)
Public Sub AddRecord(ByVal a As cAddress)
AddressList.Add(a)
End Sub
End Module
I guessed that your cAddress class looks something like this. I added a custom .ToString method so the list box will display the data you wish but the item, itself, will be a cAddress object.
Public Class cAddress
Public Property strName As String
Public Property strEmail As String
Public Property strPhone As String
Public Property strComment As String
Public Overrides Function ToString() As String
Return $"{strName}, {strEmail}, {strPhone}, {strComment}"
End Function
End Class
In the Form...
Instead of adding a string to the list box I added the cAddress object. The list box calls .ToString on the object to get the display value.
Private Sub UpdateListBox()
ListBox1.Items.Clear()
For Each a As cAddress In AddressList
ListBox1.Items.Add(a)
Next
If ListBox1.Items.Count > 0 Then
ListBox1.SelectedIndex = 0
End If
End Sub
In the remove button I cast the selected item to its underlying type, cAddress. This is the item removed from the AddressList. Then simply remove the selected item from the list box.
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
If MessageBox.Show("Are you sure?",
"Confirm Deletion",
MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
AddressList.Remove(DirectCast(ListBox1.SelectedItem, cAddress))
ListBox1.Items.Remove(ListBox1.SelectedItem)
End If
End Sub
I changed the name of the list box to ListBox1 to match my test project.
Here is something to try, use a BindingSource for setting up the ListBox. In the class override ToString to what is to be shown in the ListBox rather than what you are doing now without a DataSource.
My version of your class has name and property name changes.
Public Class Address
Public Property Name() As String
Public Property Email() As String
Public Property Phone() As String
Public Property Comment() As String
Public Overrides Function ToString() As String
Return $"{Name} {Email} {Phone} {Comment}"
End Function
End Class
Mocked data is used to populate the ListBox
Public Class Form1
Private ReadOnly _bsAddresses As New BindingSource
Private Sub UpdateListBox()
Dim AddressList = New List(Of Address) From
{
New Address() With {
.Name = "John",
.Email = "john#gmail.com",
.Phone = "555-444-3456",
.Comment = "AAA"},
New Address() With {
.Name = "Mary",
.Email = "mary#gmail.com",
.Phone = "888-333-2222",
.Comment = "BBB"},
New Address() With {
.Name = "Bob",
.Email = "bob#gmail.com",
.Phone = "111-555-9999",
.Comment = "CCC"}
}
_bsAddresses.DataSource = AddressList
lstAddress.DataSource = _bsAddresses
lstAddress.SelectedIndex = 0
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) _
Handles Me.Shown
UpdateListBox()
End Sub
Private Sub RemoveButton_Click(sender As Object, e As EventArgs) _
Handles RemoveButton.Click
If lstAddress.Items.Count > 0 AndAlso lstAddress.SelectedItem IsNot Nothing Then
Dim address = CType(_bsAddresses.Current, Address)
If My.Dialogs.Question($"Remove {address.Name}") Then
_bsAddresses.RemoveCurrent()
RemoveButton.Enabled = _bsAddresses.Count > 0
End If
End If
End Sub
End Class
Code module for asking a question
Namespace My
<ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Never)>
Partial Friend Class _Dialogs
Public Function Question(text As String) As Boolean
Return (MessageBox.Show(
text,
My.Application.Info.Title,
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2) = MsgBoxResult.Yes)
End Function
End Class
<HideModuleName()>
Friend Module WinFormsDialogs
Private instance As New ThreadSafeObjectProvider(Of _Dialogs)
ReadOnly Property Dialogs() As _Dialogs
Get
Return instance.GetInstance()
End Get
End Property
End Module
End Namespace
Karen's post seems quite comprehensive. My response is an attempt to focus on your direct question.
I don't see all of the type definitions shown in your code, but in answer to your question, which I believe is "Why am I getting System.ArgumentException: 'Argument 'Key' is not a valid value":
In the offending line of code:
AddressList.Remove(selectedName)
AddressList is a collection. To use .Remove, you must pass in an object of the AddressList collection to remove it. You are passing a simple string, and that is not an AddressList object. You need to create an object based on your string selectedName to pass into .Remove and that line should work. The rest of what you are doing seems more complex.

Changing Picture Box, BackgroundImage from an user control using another user control

I have some buttons from User Control(UserCtrl2) and want to dynamically change UserCtrl1,PictureBox BackgroundImage. From my code below, UserCtrl2 PictureBox BackgroundImage property did changed but winform is still showing the previous BackgroundImage.
I have tried the following methods,
me.refresh in UserCtrl1, still nothing happen.
Not sure how to implement {Get and Set} or dispose function.
Thanks in advance for any advise or reference.
Here is my code for UserCtrl1:
Public Class UserCtrl1
Public Sub UserCtrl1_Task(LEDno As UShort, LEDState As Boolean)
Select Case LEDno
Case 0 : Exit Sub
Case 1
If LEDState Then
PicBox_A.BackgroundImage = My.Resources.ResourceManager.GetObject("Blue_ON")
Else
PicBox_A.BackgroundImage = My.Resources.ResourceManager.GetObject("Blue_OFF")
End If
End Select
End Sub
End Class
Here is my Code for UserCtrl2:
Public Class UserCtrl2
Private ButtonClick(4) As Boolean
Private Sub Btn_A_Click(sender As Object, e As EventArgs) Handles Btn_A.Click
Dim InputControl = New UserCtrl1
If Not ButtonClick(0) Then
ButtonClick(0) = True
Btn_A.BackgroundImage = My.Resources.ResourceManager.GetObject("Switch1_ON")
InputControl.UserCtrl1_Task(1, True)
Else
ButtonClick(0) = False
Btn_A.BackgroundImage = My.Resources.ResourceManager.GetObject("Switch1_OFF")
InputControl.UserCtrl1_Task(1, False)
End If
End Sub
End Class

Vb.net ArgumentNullException when removing from dictionary

I'm having an issue where I'm trying to delete an element from my dictionary at the selected index in a listbox. I added 3 elements to a SortedDictionary and printing them into a list box. I'm trying to delete the item from the dictionary that is highlighted in the listbox, however when I click remove I get the System.ArgumentNullException: 'Value cannot be null. Parameter name: key' within my Sub btnDeleteLibrary_Click Why is this?
Error occurs at the line Libraries.Remove(lstLibraries.SelectedValue)
Public Class frmManager
Dim Libraries As New SortedDictionary(Of String, String)
Private Sub frmManager_Load(sender As Object, e As EventArgs) Handles Me.Load
Libraries.Add("Zahnow Library", "SVSU")
Libraries.Add("Fleschner Memorial Library", "BR")
Libraries.Add("Scott D. James Technical Repository", "SDJ")
lstLibraries.Items.Clear()
populatelstLibrary()
End Sub
Sub populatelstLibrary()
For Each library In Libraries
lstLibraries.Items.Add(vbCrLf & library.Key & " --- " & library.Value)
Next
End Sub
Private Sub btnDeleteLibrary_Click(sender As Object, e As EventArgs) Handles btnDeleteLibrary.Click
Libraries.Remove(lstLibraries.SelectedValue)
lstLibraries.Items.Clear()
populatelstLibrary()
End Sub
End Class
Since you are constructing a custom display string for your ListBox items it makes it harder to map that directly to the dictionary.
The easiest solution would be to create a custom class that you store in the ListBox and set the ListBox's DisplayMember and ValueMember properties, telling it how to display each item as well as which property it should get from an item when you call SelectedValue.
Class:
Public Class LibraryItem
Public Property Name As String
Public Property Code As String
Public ReadOnly Property DisplayName As String
Get
Return vbCrLf & Me.Name & " --- " & Me.Code
End Get
End Property
Public Sub New()
End Sub
Public Sub New(ByVal Name As String, ByVal Code As String)
Me.Name = Name
Me.Code = Code
End Sub
End Class
Initial setup:
Private Sub frmManager_Load(sender As Object, e As EventArgs) Handles Me.Load
'Tell the ListBox which properties to use for display and value.
lstLibraries.DisplayMember = "DisplayName"
lstLibraries.ValueMember = "Name"
'Your code...
End Sub
Filling the ListBox:
Sub populatelstLibrary()
For Each library In Libraries
lstLibraries.Items.Add(New LibraryItem(library.Key, library.Value))
Next
End Sub
Now SelectedValue will get you the value of the selected item's Name property, which corresponds to the key in the dictionary.
I would change some things in your code. First it seems that you have your sorted dictionary built with the wrong values for Key and Value, change it to
Libraries.Add("SVSU","Zahnow Library")
Libraries.Add("BR", "Fleschner Memorial Library")
Libraries.Add("SDJ", "Scott D. James Technical Repository")
' and call immediately
populatelstLibrary()
Now in populatelstLibrary change the code to
Sub populatelstLibrary()
lstLibraries.DataSource = Nothing
lstLibraries.DisplayMember = "Value"
lstLibraries.ValueMember = "Key"
lstLibraries.DataSource = Libraries.ToList()
End Sub
finally in the button click just check for null and remove the SelectedValue
Private Sub btnDeleteLibrary_Click(sender As Object, e As EventArgs) Handles btnDeleteLibrary.Click
If lstLibraries.SelectedValue IsNot Nothing Then
Libraries.Remove(lstLibraries.SelectedValue)
populatelstLibrary()
End If
End Sub

VB.net ref value seem to not work

I'm passing a value by reference in this code
Private Sub login()
Dim login As New login(lListClients, bConnected)
login.ShowDialog()
login.Dispose()
If (bConnected = True) Then
Console.WriteLine("Mokmeuh")
Button3.Visible = True
Button4.Visible = True
Button7.Visible = True
End If
End Sub
And this is the login form
Public Class login
Private lListClients As List(Of Client)
Private bConnected As Boolean
Sub New(ByRef lListClients As List(Of Client), ByRef bConnected As Boolean)
InitializeComponent()
Me.lListClients = lListClients
Me.bConnected = bConnected
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sID = TextBox1.Text, sPassword As String = TextBox2.Text
For Each cClient As Client In lListClients
If (Equals(cClient.getID, sID)) Then
If (Equals(cClient.getPassword, sPassword)) Then
bConnected = True
MessageBox.Show("Vous ĂȘtes connectĂ© vous pouvez continuez")
Me.Close()
End If
Else
MessageBox.Show("Votre ID n'existe pas")
TextBox1.Clear()
TextBox2.Clear()
TextBox1.Focus()
End If
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each m As Object In Me.Controls
If TypeOf m Is TextBox Then
CType(m, TextBox).Text = Nothing
End If
Next
Me.Close()
End Sub
Private Sub login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Select()
End Sub
End Class
Whenever I launch it the bConnected value in form 1 is always false but in the login form it's true upon destruction so I'm really confuse here I've passed the value by reference it should, when set as true in the login.vb, be true as well in the form1.vb but thw condition If (bConnected = True) is never true.
So I need some help thank you
BTW : Sorry for my bad english
Although you can pass parameters by reference, you cannot store these references. If you want to change the parameter's value, you have to do it in the called method. Otherwise, the runtime cannot ensure that the variable is still alive.
A List(Of T) is already a reference type. So it is usually not reasonable to pass this parameter by reference. The variable lListClients holds a reference to the actual list object. When passing the variable by value, this reference is copied and passed to the method, resulting in another reference to the very same object. The only reason why you would want to pass this by reference is to change the variable's value from the called method, i.e. assign a new list.
The solution for your problem is quite simple. Create a public property:
Public Class login
Public Property Connected As Boolean
'...
Connected = True
'...
End Class
And use the login object to check the value:
If login.Connected Then
Of course, you should not dispose of the object until you check the value.

Programmatically adding a commandbutton to a userform

In excel vba I have added a commandbutton to userform... like below
Set ctrl = Me.Controls.Add( _
bstrProgID:="Forms.CommandButton.1", _
Name:="CommandButton1", Visible:=True)
Now I wanted to know how would I tell it what to do when it is clicked?
This is one of those techniques that vba will let you do, but you probably shouldn't. For all the same reasons you shouldn't use code that alters your code.
That said, here is how to do what you want. First insert a class module and name it DynBtn, then paste this code into it:
Private WithEvents mobjBtn As MSForms.CommandButton
Private msOnAction As String
''// This has to be generic or call by name won't be able to find the methods
''// in your form.
Private mobjParent As Object
Public Property Get Object() As MSForms.CommandButton
Set Object = mobjBtn
End Property
Public Function Load(ByVal parentFormName As Object, ByVal btn As MSForms.CommandButton, ByVal procedure As String) As DynBtn
Set mobjParent = parentFormName
Set mobjBtn = btn
msOnAction = procedure
Set Load = Me
End Function
Private Sub Class_Terminate()
Set mobjParent = Nothing
Set mobjBtn = Nothing
End Sub
Private Sub mobjBtn_Click()
CallByName mobjParent, msOnAction, VbMethod
End Sub
Now to use this in your form, create a blank user form and paste this code into it:
Private Const mcsCmdBtn As String = "Forms.CommandButton.1"
Private mBtn() As DynBtn
Private Sub UserForm_Initialize()
Dim i As Long
ReDim mBtn(1) As DynBtn
For i = 0 To UBound(mBtn)
Set mBtn(i) = New DynBtn
Next
''// One Liner
mBtn(0).Load(Me, Me.Controls.Add(mcsCmdBtn, "Btn1", True), "DoSomething").Object.Caption = "Test 1"
''// Or using with block.
With mBtn(1).Load(Me, Me.Controls.Add(mcsCmdBtn, "Btn2", True), "DoSomethingElse").Object
.Caption = "Test 2"
.Top = .Height + 10
End With
End Sub
Public Sub DoSomething()
MsgBox "It Worked!"
End Sub
Public Sub DoSomethingElse()
MsgBox "Yay!"
End Sub
Private Sub UserForm_Terminate()
Erase mBtn
End Sub