locked
Please Help - updating a form from another form. C# RRS feed

  • Question

  • Hey there,

    I have two forms opened.

    One Form has a RichTextBox in it. = Form1

    The other form has a listbox with items in it. = Form2

    Form2 is opened through form one with a button, when this button is clicked it opens Form2 while keeping Form1 opened in the background.

    In Form2 I have setup that when you click on a listbox item and then Click the "Open" Button it saves the item to a string to be passed into Form1, Which works but does not work because you have to completely Close and open Form1 again for it to Register. 

    How can i completely reload Form1 after clicking the "Open" Button.

    Form1:

    NOTHING IN FORM1 TO BE ABLE TO SHOW CODE! 

    Form2:

            private void IDD_BUTTON_OPEN_Click(object sender, EventArgs e)
            {
                try
                {
                    foreach (var item in IDD_VIEWKEYWORDS.SelectedItems)
                    {
                        string encryped_Data = item.ToString();
                        Encryption DisplayKeywordData = new Encryption();
                        DisplayKeywordData.encrypted_Data = encryped_Data;
                        DisplayKeywordData.Refresh();
    
                        DisplayKeywordData.ShowDialog();
                    }
                }
                catch
                {
                    MessageBox.Show("Please pick a keyword.");
                }
            }

    Anyone have any ideas? please help!

    -Scott



    • Edited by ScottD97 Wednesday, June 29, 2016 12:07 PM
    • Moved by CoolDadTx Wednesday, June 29, 2016 1:39 PM Winforms related
    Wednesday, June 29, 2016 9:54 AM

Answers

  • Sharing data in WinForms has always been an issue. Encapsualtion works in your disfavor here. WPF is set up a lot better by doing the legwork in the background.

    It is possible, but you need:

    1. Modify Form1 to have some public functions to call/events to register/properties to set

    2. Hand Form2 a reference to the Form1 innstance. If form1 creates form2, just modify the constructor accordingly to take the refernce and create/show it like this:

    Form2 temp = new Form2(this);
    temp.Show();

    The one thing you should absolutely aviod is the temptation of creating a static class to work as data repository. Replacing those can be a pain in the ass:
    https://social.msdn.microsoft.com/Forums/en-US/d7417367-09d7-446c-a7da-3faff87247ba/override-configuration-manager-connection-string?forum=csharpgeneral

    Maybe a static field in a static class that contains a single instance wich acts as repository. That way at least you can change out the implementation and use different instances in the same application context.

    Wednesday, June 29, 2016 12:40 PM
  • Hi ScottD97,

    >>But in order to make fluid and look nice i need to be able to just refresh or reopen Form1 in the same location as before.

    Take a look at below code(I use 'FormClosing' event and 'Visible', 'Owner' properties):

                Encryption DisplayKeywordData = new Encryption();
                DisplayKeywordData.FormClosing += (obj, arg) =>
                {
                    (obj as Encryption).Owner.Hide();
                    Thread.Sleep(500);
                    (obj as Encryption).Owner.Show();
                };
                DisplayKeywordData.ShowDialog(this);

    Regards,

    Moonlight



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, June 30, 2016 2:56 AM

All replies

  • Hi,

    can you give more details what you are doing? Without any details we can only help with some generic information.

    a) Connecting the 2 forms to each other. I would use events to do this. That way you do not have a thight coupling between forms. So Form2 will define an event with an EventArg that contains a string or whatever data you want to transfer. So when the user in form2 clicks the open button, the specific event is fired.
    So all that Form1 has to do is: create the form2 instance, register itself for the event and show it.

    b) Display of the data: Your Form1 seems to have some code already which is done when the form is created / loaded / shown. This code (or parts) should be moved to a separate method which is then called from the constructor / load event / show event ... That way you have a dedicated method which you could also call inside the event handler of the event that is fired by form2. 

    With kind regard,

    Konrad

    Wednesday, June 29, 2016 11:15 AM
  • I will post code now.
    Wednesday, June 29, 2016 12:02 PM
  • Code has been posted.
    Wednesday, June 29, 2016 12:07 PM
  • Sharing data in WinForms has always been an issue. Encapsualtion works in your disfavor here. WPF is set up a lot better by doing the legwork in the background.

    It is possible, but you need:

    1. Modify Form1 to have some public functions to call/events to register/properties to set

    2. Hand Form2 a reference to the Form1 innstance. If form1 creates form2, just modify the constructor accordingly to take the refernce and create/show it like this:

    Form2 temp = new Form2(this);
    temp.Show();

    The one thing you should absolutely aviod is the temptation of creating a static class to work as data repository. Replacing those can be a pain in the ass:
    https://social.msdn.microsoft.com/Forums/en-US/d7417367-09d7-446c-a7da-3faff87247ba/override-configuration-manager-connection-string?forum=csharpgeneral

    Maybe a static field in a static class that contains a single instance wich acts as repository. That way at least you can change out the implementation and use different instances in the same application context.

    Wednesday, June 29, 2016 12:40 PM
  • I can get this to work, Ive gotten the data to show by manually changing the String in From1 after a break point. But in order to make fluid and look nice i need to be able to just refresh or reopen Form1 in the same location as before.

    Can this be done?

    Wednesday, June 29, 2016 1:03 PM
  • Hi ScottD97,

    >>But in order to make fluid and look nice i need to be able to just refresh or reopen Form1 in the same location as before.

    Take a look at below code(I use 'FormClosing' event and 'Visible', 'Owner' properties):

                Encryption DisplayKeywordData = new Encryption();
                DisplayKeywordData.FormClosing += (obj, arg) =>
                {
                    (obj as Encryption).Owner.Hide();
                    Thread.Sleep(500);
                    (obj as Encryption).Owner.Show();
                };
                DisplayKeywordData.ShowDialog(this);

    Regards,

    Moonlight



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, June 30, 2016 2:56 AM