1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Drag Drop - detecting who sent item.

Discussion in 'C#' started by taylby, Aug 28, 2010.

  1. taylby

    taylby Guest

    Hi
    I am working on a project that will be used to add resources to a job, such resources could be in the form of Staff, Equipment or vehicles. I have a main form that contains a list box of jobs for a given date. It also has three list boxes for each of these resources that are still available (i.e have not been allocated for a job on that date.).

    The user selects a job from a list box and drags it into the middle of the screen. Where I have created a custom control that among other items has 3 other listboxes on the control, one for each of the resources. The user will then pull the relevant resources from the forms list boxes onto the custom controls list box. This works fine. The reason for adding the custom control dynamically is due to the fact that the user might wish to add resources to multiple jobs at the same time / swap resources between those jobs.

    What I want to do though is ensure that a Vehicle dragged item can not be placed within the list box for staff or equipment like wise for any of the resources they need to only be added to the corresponding list. We all know not to trust the user and that is why there is this need.

    -- What I have tried.

    I have decided that the most likely place to put this is on the controls dragentre event handler. I have added a break point in and tried to examine the object sender, and DragEventArgs e parameters. However I can not find any trace of the listbox name of where the item originated from.

    --included
    1) 'jobLoad' form screen shot - with one job item ('opsCentre', custom control) on screen.
    2) 'opsCentre' code.
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace PP
    {
    public partial class OpsCentre: UserControl
    {
    #region Description
    /*#############INFORMATION TO ANYONE WHO WANTS TO KNOW#####################
    * This control is used for the resource holder for jobs.
    * What will be passed to the control is the JobID.
    * The control will hold this in a Vairable called JobID.
    * This Variable will be needed when prepopulating the control or when
    * adding information to the database, it will be needed to be supplied
    * as the key. --Enjoy.
    */
    #endregion
    int EventID = 0;
    int JobID = 0;
    int EventStaffNumberNeeded = 0;

    int staffNeeded = 0;
    int totalStaff = 0;


    DataTable DTStaff = new DataTable();

    public OpsCentre()
    {
    InitializeComponent();
    lstGeneralStaff.DragDrop += new DragEventHandler(lstGeneralStaff_DragDrop);
    this.DragEnter += new DragEventHandler(OpsCentre_DragEnter);
    }

    public OpsCentre(int ThisJobID)
    {
    InitializeComponent();
    DTStaff.Columns.Add();

    JobID = ThisJobID;
    PrePopulateItems();

    lstGeneralStaff.DragDrop += new DragEventHandler(lstGeneralStaff_DragDrop);
    this.DragEnter += new DragEventHandler(OpsCentre_DragEnter);
    }

    public void PrePopulateItems()
    {
    PP.Event EV = new Event();
    PP.Staff ST = new Staff();

    // lblActivityDetailsVal.Text = EV.getEvent(EventID).Tables[0].Rows[0][1].ToString();
    // lead Staff
    //lstLeadStaff.DataSource = ST.EventStaffLeads(EventID).Tables[0];
    //lstLeadStaff.DisplayMember = ST.EventStaffLeads(EventID).Tables[0].Columns[13].ToString();
    //lstLeadStaff.ValueMember = ST.EventStaffLeads(EventID).Tables[0].Columns[0].ToString();
    // Staff

    DataTable dtGSOE = new DataTable();
    dtGSOE = ST.GeneralStaffOnSession(JobID).Tables[0];
    lstGeneralStaff.DataSource = dtGSOE;
    lstGeneralStaff.DisplayMember = dtGSOE.Columns[13].ToString();
    lstGeneralStaff.ValueMember = dtGSOE.Columns[0].ToString();


    }



    public string ActivityName
    {
    set
    {
    lblActivityDetailsVal.Text = value;
    }
    }

    public void ActivityInformation(int EventActivityID)
    {
    EventID = EventActivityID;
    StaffNumberNeeded();
    }

    public void StaffRatio(int StaffNumber, int CustNumber)
    {
    lblStaffRatio.Text = "Staff Ratio " + StaffNumber + "|" + CustNumber;
    }

    public void StaffNumberNeeded()
    {
    PP.Event EV = new PP.Event();
    DataTable DT = new DataTable();
    int eventTypeID = 0;
    DT = EV.getEvent(EventID).Tables[0];
    double NumberOFCUstomers = Convert.ToDouble(DT.Rows[0][5].ToString()) + Convert.ToInt16(DT.Rows[0][6].ToString());

    PP.EventType EVT = new PP.EventType();

    EventStaffNumberNeeded = EVT.StaffNumbersNeeded(Convert.ToInt16(DT.Rows[0][8].ToString()), NumberOFCUstomers);
    lblStaffNeeded.Text = "Staff Number needed: " + EventStaffNumberNeeded.ToString();

    }

    public void StaffToAdd()
    {
    //How many people that you need to add to get the correct ratio.
    int totalStaffNeeded = Convert.ToInt16(lblStaffNeeded.Text);
    int thisStaffNumber = (totalStaffNeeded - totalStaff);
    if (thisStaffNumber == EventStaffNumberNeeded)
    {
    lblStaffStillToAdd.ForeColor = System.Drawing.Color.Green;

    }
    else if (thisStaffNumber < EventStaffNumberNeeded)
    {
    lblStaffStillToAdd.ForeColor = System.Drawing.Color.Red;
    }
    else
    {
    lblStaffStillToAdd.ForeColor = System.Drawing.Color.Black;
    }



    lblStaffStillToAdd.Text = thisStaffNumber.ToString();


    }

    public void StaffTotal()
    {
    //int stafflead = lstLeadStaff.Items.Count;
    int GeneralStaff = lstGeneralStaff.Items.Count;
    //int staff = stafflead + GeneralStaff;
    //lblStaffListVal.Text = staff.ToString();
    //totalStaff = staff;
    }

    public void SeatsNeeded()
    {

    }

    public void StaffSeatsNeeded()
    {
    }

    public void CustomerSeatsNeeded()
    {
    }

    public void totalSeats()
    {
    }


    private void lstViewEvents_DragDrop(object sender, DragEventArgs e)
    {
    // transfer the filenames to a string array
    // (yes, everything to the left of the "=" can be put in the
    // foreach loop in place of "files", but this is easier to understand.)
    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

    // loop through the string array, adding each filename to the ListBox
    //foreach (string file in files)
    //{
    // lstActivitySummary.Items.Add(file);
    //}
    }

    public void lstViewEvents_DragEnter(object sender, DragEventArgs e)
    {
    // make sure they're actually dropping files (not text or anything else)
    //if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
    // // allow them to continue
    // // (without this, the cursor stays a "NO" symbol
    // e.Effect = DragDropEffects.All;
    }

    public void lstLeadStaff_DragDrop(object sender, DragEventArgs e)
    {
    /*****************LEAD STAFF*********************
    * This will add the Lead Staffs Details to the *
    * list box. *
    * The List box that the previous item was *
    * removed from will have to remove the item *
    * from the list of avaliable staff. *
    * On Add we will have to refresh the following *
    * Methods: *
    * 1) StaffTotal() *
    * 2) StaffToAdd()
    * Will add the member of staff to the
    * appropriate call on the database using the
    * staff.cs class.
    * Then refresh the list.
    * *
    ***********************************************/
    PP.Staff ST = new Staff();
    //1 == true

    int RecievedObject = 0;
    //The idnumber of the staff object that we will recieve.
    //The following will add the Person to the StaffEvent Table
    ST.StaffEventAdd(RecievedObject, EventID, 1);

    //This line will be used to repopulate the list box.
    lstEquipment.DataSource = ST.EventStaffLeads(EventID).Tables[0];
    lstEquipment.DisplayMember = ST.EventStaffLeads(EventID).Tables[0].Columns[13].ToString();
    lstEquipment.ValueMember = ST.EventStaffLeads(EventID).Tables[0].Columns[0].ToString();

    }

    public void lstLeadStaff_DragEnter(object sender, DragEventArgs e)
    {

    }

    public void lstLeadStaff_DragLeave(object sender, EventArgs e)
    {

    }

    private void lstGeneralStaff_DragDrop(object sender, DragEventArgs e)
    {
    // DTStaff.Rows.Add("1");
    // lstGeneralStaff.Items.Add(DTStaff.Rows[0][0].ToString());
    // string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);

    }
    private void lstGeneralStaff_DragEnter(object sender, DragEventArgs e)
    {
    e.Effect = DragDropEffects.Move;


    if (e.Data.GetDataPresent(typeof(System.Int32)))
    {

    Object item = (object)e.Data.GetData(typeof(System.Int32));

    // Perform drag-and-drop, depending upon the effect.

    // lstGeneralStaff.Items.Add(item);
    }
    else
    {
    MessageBox.Show("Sorry files are not accepted");
    }


    }
    #region listboxes
    private void lstGeneralStaff_DragLeave(object sender, EventArgs e)
    {

    }

    private void lstVehicle_DragDrop(object sender, DragEventArgs e)
    {

    }

    private void lstVehicle_DragEnter(object sender, DragEventArgs e)
    {

    }

    private void lstVehicle_DragLeave(object sender, EventArgs e)
    {

    }

    private void lstEquipment_DragDrop(object sender, DragEventArgs e)
    {

    }

    private void lstEquipment_DragEnter(object sender, DragEventArgs e)
    {

    }

    public void lstEquipment_DragLeave(object sender, EventArgs e)
    {

    }
    #endregion
    private void OpsCentre_DragEnter(object sender, DragEventArgs e)
    {
    // ListBox who = (ListBox)sender;
    Object it = (object)sender.GetType();
    MessageBox.Show(it.ToString());
    }

    //public

    }
    }
    If some one could advice me how to trace who sent the opsCentre an item I would be very much appreciated.

    kind regards

    --Taylby.

    Attached Thumbnails [​IMG]
     

Share This Page