Form1 button to accept values from two different forms

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 tlhIn`toq  Icon User is offline

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

Reputation: 5436
  • View blog
  • Posts: 11,656
  • Joined: 02-June 10

Re: Form1 button to accept values from two different forms

Posted 19 March 2012 - 08:37 AM

Stop. Slow down. Don't over think. Don't start out afraid that it *has* to be tough. If you try to make something complex that is actual simple it will hang you up.

Code and object instances are abstract things. I try to always *see* them. I try to think of them in terms of real, hard objects.

One metaphor I use are the bingo parlor.

Picture one man on a stage with a bingo ball cage.
Picture 50 grannies facing the stage with game cards.
You can see all the object classes needed if you were to code the entire program: BingoCaller, BingoBall, Player, card, the cage is a collection of balls List<BingoBall>
Every physical thing you see that has an action or information you need is an object in the real world. It therefore becomes an object in our virtual world.

Now, walk through it with me step by step

Class BingoCaller.
The event is "BallSelected"
The argument is the number of the ball like "N-7"

Class PlayerGranny
Handler for event BingoCaller.BallSelected(ballArgs)
-That handler with check the List<cards> for the number received
-If a winning row is matched, then raise event "iWin"

Therefore the BingoCaller will need to subscribe to each PlayerGranny.iWin event. The BingoCaller will react by confirming the ticket, and then starting a new game. So we need to update the BingoCaller to now include a handle for that

Class BingoCaller.
The event is "BallSelected"
The argument is the number of the ball like "N-7"
+ Handler for PlayerGranny.iWin event

So I advise to just slow down. Plan it out. Picture it in the real world. Draw it on a whiteboard. And take it one step at a time.
Was This Post Helpful? 2
  • +
  • -

#17 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 - 10:28 AM

So for Form1 the receiving form I have this now:

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();
            this.ReceiveData += Mes.Receive_Data; // Subscribe the child handler to our event
	        Mes.CustomEvent += this.CustomEventHandler;
            Mes.Show();
        }        

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

     
}



However, from -public event EventHandler<TextArgs> ReceiveData;- ReceiveData is underlined in green and here -this.ReceiveData += Mes.Receive_Data;- Mes.Receive_Data is underlined in red as is Mes.CustomEvent += this.CustomEventHandler;

Thanks in advance
Was This Post Helpful? 0
  • +
  • -

#18 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 - 10:37 AM

OK, I solved the first part by placing public in front of -void Receive_Data-

 public void Receive_Data(object sender, TextArgs e)
        {
            txtObject.Text = e.Object;
            txtRA.Text = e.RightAscension;
            txtDec.Text = e.Declination;
        }



But others remain giving the error
Was This Post Helpful? 0
  • +
  • -

#19 tlhIn`toq  Icon User is offline

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

Reputation: 5436
  • View blog
  • Posts: 11,656
  • Joined: 02-June 10

Re: Form1 button to accept values from two different forms

Posted 19 March 2012 - 05:06 PM

Quote

ReceiveData is underlined in green and here


Hover the mouse over the underlined portion and the tool tip will tell you WHY it did that (ie: What it is trying to tell you about the underlined portion).
Was This Post Helpful? 0
  • +
  • -

#20 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 20 March 2012 - 12:06 AM

Well for this part in the btnMessier_Click

this.ReceiveData += Mes.Receive_Data;
Mes.CustomEvent += this.CustomEventHandler;



it's asking: are you missing a using directive or an assembly reference?

I have no idea
Was This Post Helpful? 0
  • +
  • -

#21 tlhIn`toq  Icon User is offline

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

Reputation: 5436
  • View blog
  • Posts: 11,656
  • Joined: 02-June 10

Re: Form1 button to accept values from two different forms

Posted 20 March 2012 - 10:56 AM

Did you make an event called "CustomEvent" in your Mes class?

Mes.InsertNameOfYourCustomEventHere += this.InsertNameOfYourCustomEventHandlerHere;

This post has been edited by tlhIn`toq: 20 March 2012 - 10:56 AM

Was This Post Helpful? 0
  • +
  • -

#22 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 25 March 2012 - 12:48 PM

Thanks for the help so far. I'm reading up on events again to make sure I really understand what I'm doing here. I'll get back to this later on.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2