Right Click options to create items for control

Movian

Member
Joined
Jul 29, 2013
Messages
8
Location
VA, USA
Programming Experience
3-5
Hey,
after getting some help recently i have a form with a flowlayoutpanel that has dynamically generated controls. Each of these controls has a collection of items (Appointments within a Calendar).

What i am trying to do is have the system add an item to the items collection (Either manually or through the Public Void CreateItemOnSelection function) but am having some difficulty in assigning that as either part of the code from the menustip selection or as the mouse down event... i seem unable to reference the calendar that has been generated dynamically. I see that there is a sender argument which i thought would have been a reference to the control that the right click menu was selected on allowing me to do e.CreateItemOnSelected however that does not seem to work.

This is how i have the calendars auto generated at this point and the snipets from the menu strip selection and the dynamically assigned event(I hope my question makes sense).

C#:
        public void AddCalendar()
        {
            numCals = numCals + 1;

            Calendar MyCal = new Calendar();

            MyCal.Name = "Calendar" + numCals.ToString();
            MyCal.Height = this.flowLayoutPanel1.Height - 5;
            MyCal.ContextMenuStrip = CalendarMenuStrip;
            MyCal.DoubleClick += cal_DoubleClick;
            MyCal.Font = new Font("Arial", 8, FontStyle.Regular);

            this.flowLayoutPanel1.Controls.Add(MyCal);
            ResizeCals();
        }

         private void cal_DoubleClick(Object sender, EventArgs e)
        {
            frmDetails appointment = new frmDetails();
            appointment.Show();
            appointment.TopMost = true;
        }

private void setPatientDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmDetails Details = new frmDetails();
            Details.Show();
            Details.SetPatDetails(patfirst, patphone, patMedicalID, patcomments);

        }
 
I see that there is a sender argument which i thought would have been a reference to the control that the right click menu was selected on allowing me to do e.CreateItemOnSelected however that does not seem to work.
That doesn't really make sense. The 'sender' is the object raised the event and if that was indeed a reference to the control then surely you'd use 'sender.CreateItemOnSelected', not 'e.CreateItemOnSelected'. That's still not right though. If you're talking about the Click event of a ToolStripMenuItem then the sender is that ToolStripMenuItem. If you want a reference to the control that was right-clicked then you can use the SourceControl property of the ContextMenuStrip. That property is type Control though, so you must still cast it as the appropriate type.
 
Could you give me an example of casting it?
Or link me to a resource so i can read up about it?

I am self taught on C# and coming from a VBA background so somethings take me a little time to get my head around how they work...
 
Back
Top Bottom