Pass information from one form to another RRS feed

  • Question

  • I have a simple form that is loaded from the main function in Module1. The user can launch another form that allows the user the ability to make changes to custom settings.

    When the user closes the settings form I would like to pass information to update the main form. Currently, the user must exit the program and re launch it to read the updated settings.

    Monday, July 27, 2020 3:14 PM

All replies

  • What kind of custom settings? How are the changes done?

    If you're talking about UserSettings, then you need to call RefreshSection(sectionName) for all changed sections.

    Monday, July 27, 2020 3:24 PM
  • The program displays Form1. On Form1, there are some check boxes with predetermined settings. the settings are read using My.Settings.XYZ. From Form1 the user can click a button to launch Form2.

    The user can then make some adjustments on Form2 and clicking ok updates My.Settings.XYZ and then closes the form. 

    Is it possible to have Form1 read the updated My.seetings when the focus is returned to Form1?

    Monday, July 27, 2020 5:20 PM
  • create a new class that would hold the values being transported from form1 and form2. 

    declare a property of said class in form2. Let's say it is named Data. Now in form1, create an object of said class, initialize it with some value,  and assign the object to form2.Data between your code that creates form2, and your code that launches form2. 

    In Form2 you can populate the form with the property and save changes to the property before closing the form. This may or may not be completely done with data binding, depending on how complex your controls are. 

    it sounds like you have a modal form2, so after the form returns, if the return value of ShowDialog is DialogResult.OK, you should do something about the values stored in form2.Data. Of course if form2 returned DialogResult.Cancel it is safe to discard it.

    If your form2 is not model, then you need some kind of notification to know values got changed in Form2. You can declare an event on either Form2 or the data transport object itself, and hookup the event with an event handler in form1. Raise the event in form2 when you need to tell others the data has changed. 

    Visual C++ MVP

    Monday, July 27, 2020 10:56 PM
  • Then there should be nothing to do, cause My.Settings are serving this purpose.


    Public Class MainForm
        Private Sub btnDisplaySetting_Click(sender As Object, e As EventArgs) Handles btnDisplaySetting.Click
            Me.Text = My.Settings.SettingTest
        End Sub
        Private Sub btnShowOtherForm_Click(sender As Object, e As EventArgs) Handles btnShowOtherForm.Click
        End Sub
    End Class


    Public Class OtherForm
        Private Sub btnChangeSetting_Click(sender As Object, e As EventArgs) Handles btnChangeSetting.Click
            My.Settings.SettingTest = txtSetting.Text
        End Sub
        Private Sub OtherForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            txtSetting.Text = My.Settings.SettingTest
        End Sub
    End Class

    Tuesday, July 28, 2020 8:08 AM
  • The program displays some default settings on Form1. I added a settings button on Form1 which displays Form2.

    On Form2 the user can change the settings but when Form2 is closed Form1 settings do not change.

    I don't know how to have Form1 read the My.settings once it gets focus again.

    Friday, August 7, 2020 12:07 AM
  • Well, without posting a concise and complete example showing that behavior, it is hard to tell...

    Monday, August 10, 2020 8:17 AM
  • Hi JRDumont,

    Thanks for your feedback.

    You can consider creating a constructor for Form2 that accepts your value of setting as an parameter.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            My.Settings.XYZ = "My Setting"
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim frm2 = New Form2(My.Settings.XYZ)
        End Sub
    End Class
    Public Class Form2
        Sub New(ByVal txt As String)
            Me.TextBox1.Text = txt
        End Sub
    End Class

    Hope it could be helpful.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 10, 2020 8:46 AM
  • There is no constructor needed. In VB.NET My.Settings is global. Thus changes in one place (Form2) are readable at once in another (Form1).
    Monday, August 10, 2020 10:11 AM