CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2011
    Posts
    20

    Passing Data between single instance Windows Forms

    Hi! I want to pass data between two textboxes in two different forms when an OK button is clicked.
    Now I have found out that there are many ways to do it (Constructor, Object, Delegates Approach etc) all of them work, no problem.
    The issue here is that whenever the data is passed a new instance of the form is created.
    i.e If I click the OK button on Form1 ten times, ten Forms2 will be created with the passed data.
    How do I overcome this?
    Thanks!

    PS .NET4 VS-2010
    Last edited by Scar88; November 28th, 2011 at 08:04 AM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Passing Data between single instance Windows Forms

    You can makw form2 modeless and just hide/show it.

  3. #3
    Join Date
    Nov 2011
    Posts
    20

    Re: Passing Data between single instance Windows Forms

    My Form2 (vk) is modeless. Problem is when ever data is passed from keyboard to the main form (Virtual keyboard, which should always be displayed) it's passed to an instance of it.
    this is my code for the OK button on vk Form
    Code:
    private void button_OK_Click(object sender, EventArgs e)
            {
                MainForm frm = new MainForm();
                delPassData del = new delPassData(frm.funData);
                del(this.display);
                frm.Show();
                this.Hide();
            }
    And the code on the Virtual keyboard form for passing the data
    Code:
    public void funData(TextBox txtForm1)
            {
                richTextBox1.Text = txtForm1.Text;
            }
            
            private void richTextBox1_Click(object sender, EventArgs e)
            {
                vk vk1 = new vk();
                vk1.Show();     
            }
    How do I prevent multi instances of the Virtual keyboard form?
    Please have a look at the attached image.
    Attached Images Attached Images
    I am learning C# .NET 4

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Passing Data between single instance Windows Forms

    Disable the button after it is clicked and handle the VisibilityChanged event of your child form. When it changes to not visible enable the button once again.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  5. #5
    Join Date
    Nov 2011
    Posts
    20

    Re: Passing Data between single instance Windows Forms

    Thank you for you suggestion but maybe due to my inexperience I fail to see how that addresses my problem.
    The child form (vk) is not the issue (it's hidden after OK is clicked ). The issue is the new Instance of the main form that is triggered after data is passed.
    How do I make the main form stay single?
    I am learning C# .NET 4

  6. #6
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Passing Data between single instance Windows Forms

    You are going about this the wrong way. You "VK" form should not be showing the main form. All it needs to do is close down. What you need to do is have you "VK" form raise an event to update the data on your main form. Here is some code that might help you.

    This example uses 2 forms. On the first form, put a label and a button.
    On the second form, put a textbox, and 2 buttons. (button2 will close the form).


    form1 Code
    Code:
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 keyboard = new Form2();
                keyboard.UpdateData += new Form2.updateDataHandler(keyboard_UpdateData);
                keyboard.ShowDialog();
                keyboard = null;
            }
    
            void keyboard_UpdateData(string data)
            {
                label1.Text += data;
            }
        }
    Notice in this form, we declare the keyboard form, subscribe to the Updatedata event, then show the form as a dialog form.

    form2 Code
    Code:
        public partial class Form2 : Form
        {
            public delegate void updateDataHandler(string data);
            public event updateDataHandler UpdateData;
            public Form2()
            {
                InitializeComponent();
            }
            protected virtual void OnUpdateData(string data)
            {
                if (UpdateData != null)
                    UpdateData(data);
            }
            private void button1_Click(object sender, EventArgs e)
            {
                OnUpdateData(this.textBox1.Text);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    In this form, anytime we click button1, we raise the event UpdateData, which form1 is subscribed to. This in turn updates the label on form1 via the event handler.

  7. #7
    Join Date
    Nov 2011
    Posts
    2

    Question Re: Passing Data between single instance Windows Forms

    Hi ALL,

    I need codes for Simplex method.I dont know how to write simplex codes on C sharp? It is my PHD class homework.

    Thanks ALL,

  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Passing Data between single instance Windows Forms

    Quote Originally Posted by eildes06 View Post
    Hi ALL,

    I need codes for Simplex method.I dont know how to write simplex codes on C sharp? It is my PHD class homework.

    Thanks ALL,
    Try google.
    Always use [code][/code] tags when posting code.

  9. #9
    Join Date
    Nov 2011
    Posts
    20

    Re: Passing Data between single instance Windows Forms

    @ sotoasty

    Thank You!
    That's exactly what I meant my application to do but I got lost.
    2 questions if I may:
    Code:
    keyboard = null;
    1) why you do that? The application seems to run fine without this line..

    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                OnUpdateData(this.textBox1.Text);
            }
    2) Why OnUpdateData and not just UpdateData?
    Again the application seems to run fine with UpdateData (this.textBox1.Text)

    Kudos mate!
    Last edited by Scar88; December 18th, 2011 at 11:05 AM.
    I am learning C# .NET 4

  10. #10
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Passing Data between single instance Windows Forms

    1. KeyBoard = null. Although technically not necessary, because the keyboard form object is only in scope on that 1 button click, it is a habit of cleaning up after myself. If you null any object you can, you won't get memory leaks.

    2. The OnUpdateFunction, is designed to make sure that the UpdateData delegate can be called. If you just call the updateFunction and no one has subscribed to the event, you will get an error. Make the following changes and see what happens when you run the code.

    in from 1, take out this line.
    Code:
                keyboard.UpdateData += new Form2.updateDataHandler(keyboard_UpdateData);
    In form2, change this line...

    Code:
    OnUpdateData(this.textBox1.Text);
    to

    Code:
    UpdateData(this.textBox1.Text);
    When you run this, show form2, click the button1 on form2, now what happens is you should get an error. Since nothing is "subscribing" to the event, there is no pointer for the delegate to call, so you will get a null reference error. That is why the "OnUpdateData" function. Notice it just checks if "UpdateData" is null and if not, calls the UpdateData delegate. It is not only good practice, but it is basically the only way you should program events in c#.

    Think of it this way, when you have a button on the form, most of the time, you will be using the buttonClick event. However, there are tons of other events that can be associated with the button (mousedown,mouseup....). Everytime one of those things happens on that button, the button needs to check and see if it should actually call the function associated with that event happening. The way they do that is to see if anything has "subscribed" to the event, by checking if it is null. If it is not null, then it calls the function specified by the subscriber.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured