Reset My.Settings to values last saved - vb.net

I'm currently using a "Settings" form to set various settings for my application. What I'm trying to do is to revert settings back to before any changes the user has made before opening and changing a field. I have a text box with a data binding to a setting and when I make a change and click okay it gets saved the next time I open it. When I hit cancel it also gets saved. Not quite sure if I'm approaching this correctly.
Public Class frmSettings
Private _mysettings As Configuration.SettingsBase
Private Sub frmSettings_Load(...) Handles Me.Load
_mysettings = My.Settings
End Sub
Private Sub btnCancel_Click(...) Handles btnCancel.Click
For Each p As Configuration.SettingsPropertyValue In _mysettings.PropertyValues
My.Settings(p.Name) = p.PropertyValue
Next
Me.Close()
End Sub
Private Sub btnOkay_Click(...) Handles btnOkay.Click
My.Settings.Save()
Me.Close()
End Sub
End Class

Rather than using databound control you can simply load the value of the setting when the settings form loads. It is simple doing it that way, and it works. Otherwise you would have to make a clone of My.Settings: doing _mysettings = My.Settings is only creating a pointer to My.Settings, not a copy of it.
For example, I have a Form named ChangeConnectionString which has OK/Cancel buttons and a TextBox control named connString:
Public Class ChangeConnectionString
Private Sub bnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnOK.Click
My.Settings.connectionString = connString.Text
My.Settings.Save()
Me.Close()
End Sub
Private Sub bnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnCancel.Click
Me.Close()
End Sub
Private Sub ChangeConnectionString_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
connString.Text = My.Settings.connectionString
End Sub
End Class

Related

Trouble switching between projects/forms in vb.net

I am working on a project in Visual Studio Community 2019 using VB.Net. I am developing a user interface wherein users start on one form (a sort of index, call it form 1) and select from their the next form they want to go to (say form 2). This part is working fine. The trouble is that after users finish in form 2, they should be directed back to form 1 to continue from here, and I can't get this to happen. I need somehow to make the main menu a public shared form so that the other projects can access it but I can’t figure out how to do that.
Here are the relevant lines from form 1:
Public Class frmMainMenu
Public Shared inst As frmMainMenu
Private Sub frmMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
inst = Me
Timer1.Start()
Me.Show()
End Sub
Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles
btnGeology.Click
Dim formNew As New Geology.frmGeologyMenu
formNew.Show()
Timer1.Stop()
Me.Hide()
End Sub
And here are the relevant lines from form 2. I want lines 23 and 24 to return the user to the main menu, but it doesn't work.
Public Class frmGeologyMenu
Public Sub frmGeologyMenu_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lblTime.Text = 10
Me.Show
End Sub
Private Sub BtnMainMenu_Click(sender As Object, e As EventArgs) Handles
btnMainMenu.Click
inst.frmMainMenu.Show()
inst.frmMainMenu.Timer1.Start()
Me.Close()
End Sub
I appreciate any help you can offer!
From my comments above:
When you create your instance of your second form called "formNew",
use the AddHandler command to wire up its FormClosed event to a method
in the main form. Then in the second form you can just call Me.Close()
and the method in the main form will fire where you can call
Me.Show()..
In main menu:
Public Class frmMainMenu
Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles btnGeology.Click
Dim formNew As New Geology.frmGeologyMenu
AddHandler formNew.FormClosed, AddressOf formNew_FormClosed
Timer1.Stop()
Me.Hide()
formNew.Show()
End Sub
Private Sub formNew_FormClosed(sender As Object, e As FormClosedEventArgs)
Me.Show()
Timer1.Start()
End Sub
End Class
In frmGeologyMenu:
Public Class frmGeologyMenu
Public Sub frmGeologyMenu_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lblTime.Text = 10
End Sub
Private Sub BtnMainMenu_Click(sender As Object, e As EventArgs) Handles btnMainMenu.Click
Me.Close()
End Sub
End Class
In VB.NET, your startup form is always the default instance of its type. That means that you can simply use the type name to refer to your startup form anywhere in your app. If you use Me.Hide() in your startup form then frmMainMenu.Show() can be used anywhere to redisplay it. This would not be the case in C# as default form instances are a VB-specific feature. See here for more info.

data/item inside my combo box keeps multiplying whenever i click on it

after adding items to the combo box whenever I run it and click on the combobox the data inside the box just multiply itself, anyhelp pls … This is my code
Private Sub productcombobox_Click(sender As Object, e As EventArgs) Handles productcombobox.Click
productcombobox.Items.Add("Manage Product")
productcombobox.Items.Add("Add Product")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
productcombobox.Items.Add("Manage Product")
productcombobox.Items.Add("Add Product")
End Sub
No discussion necessary considering the comments already posted.
you can just add the clear() methods before you add the data into the combo box
Private Sub productcombobox_Click(sender As Object, e As EventArgs) Handles productcombobox.Click
productcombobox.Clear()
productcombobox.Items.Add("Manage Product")
productcombobox.Items.Add("Add Product")
End Sub
this is your code so just add the Clear() method before other things ..

Open another form when clicking an item in ListBox VB.net

I'm a network admin that have been task to do a paperless meeting system. So right now I'm looking at VB.net as my platform. My goal is populate a listbox with items inside a folder using their filenames, it's mostly PDF files. So i have achieve that goal and when I click the items in the listbox it the file in the folder opens. What I want is, if I click an item in the listbox, another form will show. This form has an embedded pdf reader and a textbox for comments. textbox will be save in a .txt file inside a another folder. here's my code
Public Class Form2
Dim MyFolderPath As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "C:\Users\ICTCAdmin\Desktop\Board Meeting\Academic")
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
For Each fullpath As String In IO.Directory.GetFiles(MyFolderPath)
ListBox1.Items.Add(IO.Path.GetFileName(fullpath))
Next
Catch ex As Exception
MsgBox(ErrorToString)
End Try
End Sub
Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Form1.Show()
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim fullpath As String = IO.Path.Combine(MyFolderPath, ListBox1.SelectedItem.ToString)
Process.Start(fullpath)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
End Class
Thank you in advance.
In your second form create a Friend property like this at the top of the child form's code window
Public Class frmWhateverYourNewFormIsCalled
Friend Property filepath As String
In your calling form edit the listbox1_SelectedIndexChanged property to this
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim fullpath As String = IO.Path.Combine(MyFolderPath, ListBox1.SelectedItem.ToString)
dim childForm as new frmWhateverYourNextFormIsCalled
childForm.filepath = ""
childForm.Show()
End Sub
In your child form add in code to open the filePath in your embedded pdf reader. Probably in the Shown event handler. Not the Load event handler.
To view pdfs within your program this might help
Displaying a PDF in a control in Visual Basic 2010

Vb.NET Windows Forms Focus Event

I am working with .Net Windows Forms application (vb.net),
I have Two Forms,
Form A = Main form
Form B = Which is being called by clicking a button on Form A
The Issue is, I want to update certain Controls(List,Grids) when ever my Form A gets Activated,
At Form_A_Load it will load controls one must, but when I open Form B and upon Exit of Exit of Form B, I want to reload Form A's controls(List,Grids).
I have tried many events
Activated,Deactivated,Enter,Leave,Enabled,Visibility changed , but could not trap any,
If I am using Activated/Deactivated with some flag to check which was triggered, then a continues loop occurs. Kindly some body suggest , the workable method
Here is the Edit code:
Public Class Form1
Private Sub Form1_Activated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Activated
MessageBox.Show("Activated")
End Sub
Private Sub Form1_Deactivate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Deactivate
' MessageBox.Show("Deactivated")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Text = "Activated/Deactivated"
MessageBox.Show("This will set focus lost")
End Sub
End Class
--If a once click on Button1_Click .. "MessageBox.Show("Activated")" appears again and again.
Basically, It was something multiple forms opened, and what I did is that at the FormClosing of last Form where my code Returns to Forma A, I have checked through Loop the Opened Forms and from there I selected my Form A and Triggered the Function Which Reloads the List.
Try this:
[UPDATED]
Public Class FormA
Friend WithEvents objectFormB As FormB
Private Sub objectFormB_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles objectFormB.FormClosed
'do whatever...
End Sub
End Class

form starting but not displaying vb.net

So I was coding a program that opens 20 web browsers on one page, for a youtube or view bot any URL really, but when I launched the program It displayed the text box I put in to see if it was actually starting and then nothing else
So my question is, Whats wrong with my code?
startup form (where you can launch the main code with button 1)
Public Class Startup
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Settings.Show()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
views.Show()
End Sub
End Class
main form
Public Class views
Private Sub views_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MessageBox.Show("Form Initalised")
WebBrowser1.Navigate(My.Settings.url)
WebBrowser2.Navigate(My.Settings.url)
WebBrowser3.Navigate(My.Settings.url)
WebBrowser4.Navigate(My.Settings.url)
WebBrowser5.Navigate(My.Settings.url)
WebBrowser6.Navigate(My.Settings.url)
WebBrowser7.Navigate(My.Settings.url)
WebBrowser8.Navigate(My.Settings.url)
WebBrowser9.Navigate(My.Settings.url)
WebBrowser10.Navigate(My.Settings.url)
WebBrowser11.Navigate(My.Settings.url)
WebBrowser12.Navigate(My.Settings.url)
WebBrowser13.Navigate(My.Settings.url)
WebBrowser14.Navigate(My.Settings.url)
WebBrowser15.Navigate(My.Settings.url)
WebBrowser16.Navigate(My.Settings.url)
WebBrowser17.Navigate(My.Settings.url)
WebBrowser18.Navigate(My.Settings.url)
WebBrowser19.Navigate(My.Settings.url)
WebBrowser20.Navigate(My.Settings.url)
Threading.Thread.Sleep(My.Settings.Time)
reload.Show()
Me.Close()
End Sub
End Class
and form reload mentioned in the main form.
Public Class reload
Private Sub reload_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Close()
views.Show()
End Sub
End Class
Try to use the Form.Shown event instead of the Form.Load event. In Form.Load the controls are often not fully initialized and if something goes wrong there the initialization of the whole form sometimes goes awry. It's better to use the Form.Shown for such lengthy procedures you have there.