Form1 button to accept values from two different forms

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 4475 Views - Last Post: 25 March 2012 - 12:48 PM Rate Topic: -----

#1 mzu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 12

Form1 button to accept values from two different forms

Posted 18 March 2012 - 07:27 AM

Hi,

In my application I have Form1 which is the main form of the app, in this form I have two buttons that open two other forms (1 button opens one form and the other button opens the other form). In Form1 I have a button called LoadData. Basically, the two forms that can be opened have a TextBox in them. Now I want to pass the text in the form's textbox to the textbox of Form1 (the main form) by clicking the LoadData button. I know how to do this with one form (Form1 + one additional form) but here in this, the LoadData button should accept either from the first additional form or the other additional form depending on which one is open. So I guess that in the button click event I will have to program some kind of -if, else- statement which says, if first additional form is open use this value, else if the second additional form is open use this value. For info, these forms will never be open at the same time, it's one of the two forms. Again, if there is only one additional form I know how to pass the data but can't think of a way to have the Load button click event to see which form is open and use the data from that form.

I'm still learning C# and I have gone through the chapter and the tutorials on this forum on passing data between forms which is an easy thing now, but to have the button get the data from one of the two forms depending on which is open.

Hope anybody can help with this. There is not much code yet, just Form1 which has the button, and Form2 and Form3 that contain the initial value in a textbox which should be transferred to the Form1 textbox.

Any help is greatly appreciated.

Regards

Is This A Good Question/Topic? 0
  • +

Replies To: Form1 button to accept values from two different forms

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5498
  • View blog
  • Posts: 11,804
  • Joined: 02-June 10

Re: Form1 button to accept values from two different forms

Posted 18 March 2012 - 07:39 AM

See FAQ # 3. (Click the SHOW button below)


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Mar 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5498
  • View blog
  • Posts: 11,804
  • Joined: 02-June 10

Re: Form1 button to accept values from two different forms

Posted 18 March 2012 - 07:50 AM

Quote

the tutorials on this forum on passing data between forms


It sounds like you are still stuck on this thinking of sending from one box to the other. You need to get past that.

You need start thinking in a more event driven manner. Think less like a phone call from a to b, and more like a bingo caller yelling out numbers. The bingo caller just yells N-14, and all the subscribing players check their cards. The caller doesn't reach out to their cards and do it for them.


You should be raising an event with the text as the arguments.
Don't think you need to send it specifically to a destination.
When you make your new form have it subscribe to the event on your main form. Its the job of the child form to put that textargs in the right textbox.

Here's the logic. You said you've done the tutorials, so take a stab at coding it. If it is still giving you issues, post your code and we'll get you straight.

Form1
button_click()
{
   Make new form2
   Subscribe form2 event handler to form1 event
}

sendData()
{   
    Raise event with string argument
}



form2
eventHandler()
{ 
    Take received args and do something with them
}

Was This Post Helpful? 1
  • +
  • -

#4 mzu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 12

Re: Form1 button to accept values from two different forms

Posted 18 March 2012 - 08:21 AM

That event driven thinking makes a lot more sense. I will try to program it this way and let you know. But thanks for the hint so far. Appreciated !! Again, I'm still learning and this site has been very helpful so far.
Was This Post Helpful? 0
  • +
  • -

#5 mzu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 12

Re: Form1 button to accept values from two different forms

Posted 18 March 2012 - 09:52 AM

OK I have something like this now:

Form1

private void btnMessier_Click(object sender, EventArgs e)
        {
            frmMessier Mes = new frmMessier();
            Mes.passControl1 = new frmMessier.PassControl(PassData);
            Mes.Show();
        }       

private void PassData(Object sender)
        {
            txtRA.Text = ((TextBox)sender).Text;
        }




and in frmMessier

public delegate void PassControl(object sender);

public PassControl passControl1;

private void btnClose_Click(object sender, EventArgs e)
        {
            if (passControl1 != null)
            {
                passControl1(txtRA);
            }        
            this.Hide();
        }



This works great with one TextBox in frmMessier and one TextBox in Form1, but what do I have to modify if I want to send two TextBoxes in frmMessier to two TextBoxes in Form1 ?

Any help would be great

Regards
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5498
  • View blog
  • Posts: 11,804
  • Joined: 02-June 10

Re: Form1 button to accept values from two different forms

Posted 18 March 2012 - 10:36 AM

This is not an event being raised, passing an argument.

All you are doing is making the form then setting a property on the form directly before showing it.

You need to make an event on form 1
Make an arguements class to hold whatever properties you want to send.
Then raise that event when you need to

In form2 you 'hear' that event with the arguments and react accordingly.

Work your way through these tutorials and you'll see what I'm talking about.

Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Was This Post Helpful? 1
  • +
  • -

#7 mzu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 12

Re: Form1 button to accept values from two different forms

Posted 18 March 2012 - 02:03 PM

I have gone through the tutorials which are quite interesting. But I'm totally stuck now. Here is the code I have. Don't forget that Form1 is the main form which has 3 TextBoxes and two buttons. The TextBoxes should be filled by either the form that opens when pressing the first button or from the form that comes up with the second button. So far, I have only done the first button to have a start.

Form1:
namespace DobControl
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();            
        }

        public event EventHandler<TextArgs> ReceiveData;

        private void Form1_Load(object sender, EventArgs e)
        {
            //Add the event to update the TextBoxes
            ReceiveData += new EventHandler<TextArgs>(Receive_Data);
        }

        //Link TextBoxes to data from frmMessier
        void Receive_Data(object sender, TextArgs e)
        {
            txtObject.Text = e.Object;
            txtRA.Text = e.RightAscension;
            txtDec.Text = e.Declination;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnMessier_Click(object sender, EventArgs e)
        {
            frmMessier Mes = new frmMessier();
            Mes.Show();
        }        

        private void btnNGC_Click(object sender, EventArgs e)
        {
            frmNGC NGC = new frmNGC();
            NGC.Show();
        }
             
    }
}



frmMessier:
namespace DobControl
{
    public partial class frmMessier : Form
    {
        public frmMessier()
        {
            InitializeComponent();           
        }      

        private void frmMessier_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'astroObjects_DataDataSet.Messier' table. You can move, or remove it, as needed.
            this.messierTableAdapter.Fill(this.astroObjects_DataDataSet.Messier);

            //Variables in Event linked to TextBox
            string nsRightAscension = this.txtRA.Text;
            string nsDeclination = this.txtDec.Text;
            string nsObject = this.cboBox1.Text;
        }
      
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //Database Binding
        public object ObjectDBDataSet { get; set; }
        private void fillToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.messierTableAdapter.Fill(this.astroObjects_DataDataSet.Messier);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

      
    }

    //New Event
    public class TextArgs : EventArgs
    {
        //Declared Variables
        private string sRightAscension;
        private string sDeclination;
        private string sObject;

        //Constructor
        public TextArgs(string nsRightAscension, string nsDeclination, string nsObject)
        {
            this.sRightAscension = nsRightAscension;
            this.sDeclination = nsDeclination;
            this.sObject = nsObject;
        }

        //Properties which are available to listener
        public string RightAscension
        {
            get { return sRightAscension; }
        }

        public string Declination
        {
            get { return sDeclination; }
        }

        public string Object
        {
            get { return sObject; }
        }
    }

}



This is an app that I'm trying to make to see if I understood the things explained in the book but I seem to have a big problem passing data between forms. Certainly when the main form's TextBoxes could receive data from several forms.

Any help would be awesome.

Thanks in advance

This post has been edited by tlhIn`toq: 18 March 2012 - 02:29 PM
Reason for edit:: Fixed broken [/code] tag

Was This Post Helpful? 1
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5498
  • View blog
  • Posts: 11,804
  • Joined: 02-June 10

Re: Form1 button to accept values from two different forms

Posted 18 March 2012 - 02:25 PM

nice re-work! Give me a second or two to read through it.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5498
  • View blog
  • Posts: 11,804
  • Joined: 02-June 10

Re: Form1 button to accept values from two different forms

Posted 18 March 2012 - 02:34 PM

Ok. Really well done. The goal here is to get the values from form1 to the messier for right?

So what you want to do is have the messier form subscribe to the event in the form1. Right now you are raising the event, and reacting to it in the same form. The messier form is not hearing the event.

In other words: Mom is yelling out instructions, but only mom is hearing and reacting to them. Baby is deaf.


You just need a method handler in the messier form, and then subscribe to your event when you make the instance.

Form1
	        private void btnMessier_Click(object sender, EventArgs e)
        {
            frmMessier Mes = new frmMessier();
            this.ReceiveData += Mes.Receive_Data; // Subscribe the child handler to our event
            Mes.Show();
        }  



Messier form
        public void Receive_Data(object sender, TextArgs e)
        {
            // When form1 yells out "This your TextArgs", react to it here.
            txtObject.Text = e.Object;
            txtRA.Text = e.RightAscension;
            txtDec.Text = e.Declination;
        }



This post has been edited by tlhIn`toq: 18 March 2012 - 02:35 PM

Was This Post Helpful? 1
  • +
  • -

#10 mzu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 12

Re: Form1 button to accept values from two different forms

Posted 19 March 2012 - 12:21 AM

First of all, thanks for your nice words !! The problem I'm having is that sometimes I don't see where to put the code exactly. I guess I have some misunderstanding here too.

In fact, Form1 from which I can open frmMessier is the form to receive data. So Form1 receives data from frmMessier.

Thanks again for your help. I googled around a little looking for events and passing data and apparently people think this looks difficult but is not as difficult as it looks. Just have to understand what I'm doing here. So I'm still thinking about this a lot as this seems the right procedure to work with.

So again, from Form1 we can open frmMessier, in frmMessier I select an object and its data is shown in one combobox and two textboxes. Then this data should be passed to three textboxes in Form1.

Thanks again,
Michael
Was This Post Helpful? 0
  • +
  • -

#11 mzu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 12

Re: Form1 button to accept values from two different forms

Posted 19 March 2012 - 12:36 AM

OK, I've been thinking a little more about your last reply. So to get the data from frmMessier to Form1, could I just inverse your two examples ? I mean:

public void Receive_Data(object sender, TextArgs e)
{
    // When form1 yells out "This your TextArgs", react to it here.
    txtObject.Text = e.Object;
    txtRA.Text = e.RightAscension;
    txtDec.Text = e.Declination;
}



I have placed this code in Form1 already because this is the receiving form. And in frmMessier, instead of saying this.Close(); for the Close button, I could use:

  private void btnClose_Click(object sender, EventArgs e)
{
   Form1 f1 = new Form1();
   this.ReceiveData += f1.Receive_Data; // Subscribe the child handler to our event
   f1.Show();
}



Would this be correct to get the data in the frmMessier textboxes into the Form1 textboxes ?
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5498
  • View blog
  • Posts: 11,804
  • Joined: 02-June 10

Re: Form1 button to accept values from two different forms

Posted 19 March 2012 - 07:24 AM

Right concept, slightly wrong execution.
You wouldn't make a new instance of form1 if you already have a form1.
you could just do a subscription going the other way when you make your messier instance. Using my earlier example its just one more line to get information coming back the other direction.

private void btnMessier_Click(object sender, EventArgs e)
{
    frmMessier Mes = new frmMessier();
    this.ReceiveData += Mes.Receive_Data; // Subscribe the child handler to our event
    Mes.CustomEvent += this.CustomEventHandler;
    Mes.Show();
}  



I'd maybe clean up event names and handler names before going much further to avoid confusion. "ReceiveData" isn't really an event. Think about other events you deal with on controls... .Click SelectedIndexChanged BackgroundColorChanged

An event name of "ProcessingCompleted" or "DataReceived" would read a little better in your code

Mes.ProcessingComplete += this.ProcessingCompletedHandler;

or

Mes.ProcessingComplete += this.DisplayProceesedData;
Was This Post Helpful? 1
  • +
  • -

#13 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5498
  • View blog
  • Posts: 11,804
  • Joined: 02-June 10

Re: Form1 button to accept values from two different forms

Posted 19 March 2012 - 07:30 AM

View Postmzu, on 19 March 2012 - 01:21 AM, said:

First of all, thanks for your nice words !! The problem I'm having is that sometimes I don't see where to put the code exactly. I guess I have some misunderstanding here too.

In fact, Form1 from which I can open frmMessier is the form to receive data. So Form1 receives data from frmMessier.

Thanks again for your help. I googled around a little looking for events and passing data and apparently people think this looks difficult but is not as difficult as it looks. Just have to understand what I'm doing here. So I'm still thinking about this a lot as this seems the right procedure to work with.

So again, from Form1 we can open frmMessier, in frmMessier I select an object and its data is shown in one combobox and two textboxes. Then this data should be passed to three textboxes in Form1.

Thanks again,
Michael


The advice I think I give the most that is probably also ignored the most is... Plan before typing. Draw things out on a whiteboard. It doesn't have to be proper UML diagrams. But a least a box marked Form1, list its events, draw an arrow to your Messier box, list its events, and an arrow pointing back to Form1. Now you can see where the events go (start of the arrow) and where the handlers go (point of the arrow)
Was This Post Helpful? 0
  • +
  • -

#14 mzu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 12

Re: Form1 button to accept values from two different forms

Posted 19 March 2012 - 08:07 AM

View PosttlhIn`toq, on 19 March 2012 - 07:30 AM, said:

View Postmzu, on 19 March 2012 - 01:21 AM, said:

First of all, thanks for your nice words !! The problem I'm having is that sometimes I don't see where to put the code exactly. I guess I have some misunderstanding here too.

In fact, Form1 from which I can open frmMessier is the form to receive data. So Form1 receives data from frmMessier.

Thanks again for your help. I googled around a little looking for events and passing data and apparently people think this looks difficult but is not as difficult as it looks. Just have to understand what I'm doing here. So I'm still thinking about this a lot as this seems the right procedure to work with.

So again, from Form1 we can open frmMessier, in frmMessier I select an object and its data is shown in one combobox and two textboxes. Then this data should be passed to three textboxes in Form1.

Thanks again,
Michael


The advice I think I give the most that is probably also ignored the most is... Plan before typing. Draw things out on a whiteboard. It doesn't have to be proper UML diagrams. But a least a box marked Form1, list its events, draw an arrow to your Messier box, list its events, and an arrow pointing back to Form1. Now you can see where the events go (start of the arrow) and where the handlers go (point of the arrow)


Thanks for your help. I'll try this tonight and post back the result. What you say here is exactly what the problem is. I think I understand what I'm doing here with the event but I seem to have a hard time to sometimes know where to program what. Should I program the event (which is now in frmMessier) in this form or should it be in the Main form which is Form1 ? I have the same question about when to use the Form_Load event when I double click a form ? Sometime I declare variables in there and sometimes I declare variables somewhere else. All this is still a little difficult to me and I don't really find a clear explanation for these things.
In this example here I created the event in frmMessier and typed the other things where I thought they should be. But is there some sort of explanation you can give (in easy words) where to place what when you want to transfer data ? Hope you understand what I mean. I think getting some more info on where to program what to achieve a certain transfer could make things easier.

Thanks a lot.
Michael
Was This Post Helpful? 0
  • +
  • -

#15 mzu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 08-March 12

Re: Form1 button to accept values from two different forms

Posted 19 March 2012 - 08:25 AM

Have to add another question. When I do the data passing this way, when is the data in the Form1 TextBoxes updated ? Is it when I close frmMessier or when does this happen ?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2