.Net Programming Information and User Forum
September 04, 2010, 02:19:21 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Welcome all new members and guests.

"I really enjoy helping all the people learning .net and c#. I hope I can show them something new today", Ken Nipper
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Sending data from one form to another form  (Read 111 times)
Ken
Administrator
Established Member
*****
Posts: 1022



View Profile WWW
« on: April 21, 2010, 04:38:20 pm »

To send the name of a form to another form. (or you could send any other kind of data)
Do the following:



Code:



//You will use a delegate that points to a function.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;


namespace TalkingToForms
{
public partial class formMain : Form
{
          public formColorChooser()
           {
             InitializeComponent();
           }

            //this is the declaration of the delegate type class named delPassDataToColor
            //the delegate contains a parameter, a String type named FormName
            //later we will make an instance of the delegate class named delPassToColor
            public delegate void delPassDataToColor(String FormName);


         //a mouse down event to trigger the delegate
         private void frmMain_MouseDown(object sender, MouseEventArgs e)
          {
            if (e.Button == MouseButtons.Right)
            {
               //a new form instance named NewfrmColorChooser
                formColorChooser NewformColorChooser = new formColorChooser();

                //now the delegate instance of type delPassDataToColor named del
                //the NewformColorChooser must have a public member named PassData
                delPassDataToColor del = new delPassDataToColor(NewformColorChooser.PassData);

                //now pass the string "main" to the PassData member of NewfrmColorChooser and show the form with the .ShowDialog member.
                del("main");
                NewformColorChooser.Show();
            }
        }
    }
}

Now the NewfrmColorChooser is just another form with the name frmColorChooser. It must have a member named PassData that accepts a string parameter.

namespace TalkingToForms
{
    public partial class formColorChooser : Form
    {
      
        public formColorChooser()
        {
            InitializeComponent();

        }
      
        //this is where the PassData member accepts the string. We send it the word "main".
        //so now the string FormName contains the string "main"
        public void PassData(String Name)
        {
           String FormName = Name;
        }
   }
}





I will show some more complicated ones where you can have a whole class of delegate with its own members.
Hope you enjoyed it. Have fun programming
Ken
« Last Edit: May 22, 2010, 05:19:43 am by Ken » Logged
espirator
New Member
*
Posts: 2


View Profile
« Reply #1 on: May 21, 2010, 09:06:47 am »

Hi Ken;

Happy to see your new forum Smiley

i'd like to ask a question about this topic. I have two forms and i wanna connect them. First form is main form and i have a button to connect this form to the other form. I succeed it but my problem is that; in the 2nd form i have pictureBoxes and when i click these picturBoxes, i'd like to write some text in the richtextbox of the main form. I could'nt do it with changing "private" to "public static", the access way of the object.

Can u help me ? Lips sealed

Thanks
Logged
Ken
Administrator
Established Member
*****
Posts: 1022



View Profile WWW
« Reply #2 on: May 21, 2010, 01:22:03 pm »

Hey good to here from you again. I hope you are doing well.

Yes you will need a delegate to send info back to the first form. Look at how the example does it and if you still need help then past the code for the click event on the second form and I will show you how to implement.

It will look something like this:


Code:

//declare the delegate inside form2
  public delegate void PassDataDelegate(String InfoToSend);


// make instance of delegate point to the public member on form1...make sure they have the same data type for the parameters
// this code will go inside the click event of the picture on form2
  PassDataDelegate del = new PassDataDelegate(Form1.PassData);
  del("string to send to form1.PassData member");



Let me know if this helps
Ken Cool
Logged
espirator
New Member
*
Posts: 2


View Profile
« Reply #3 on: May 22, 2010, 02:50:48 am »

i get this error

How can i make it correct?..
Logged
Ken
Administrator
Established Member
*****
Posts: 1022



View Profile WWW
« Reply #4 on: May 22, 2010, 05:18:28 am »

Yes

Go to form1 and enter the following member function



Code:
//this member will accept the data string that is passed from form2
public void PassData(String StringThatsPassed)
{
   textbox1.Text = StringThatsPassed;

}



This should get rid of the error for you.

Ken
« Last Edit: May 22, 2010, 05:28:04 am by Ken » Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!