Lereyn 0 Newbie Poster

Hello,

First of all, I would apologize about my english which is not very good ;)
I'm a newbie in .NET, and I hope my code is correct :)

I'm on a project which consist of going on websites and retrieiving informations based on the 'criterias' the user has given to the program.

My main form contains 2 custom user controls.
The first UserControl manages the criterias, it contains a textbox (multiline) where the user enter the criterias and a ListBox which contains the criterias the user has confirmed.

The second one, contains checkboxes dynamically generated regarding the number of websites in the projects, and a submit button.

The custom event contains the checked websites and the websites names.
My problem is that I can't pass to my custom event the 'ListBox.ObjectColletion' to the event when it is fired when the button is pressed.
The event is catched in my main form.

Here is the codes:

BTNSearchJobsEventArgs.cs:

using System;
using System.Collections.Generic;

namespace gControls.CustomEvents
{
    public class BTNSearchJobsEventArgs : EventArgs
    {

        public string[] ChecboxesChecked { get; private set; }
        public int OffersToLoad { get; private set; }
        public List<string> Criterias { get; private set; }

        public BTNSearchJobsEventArgs(string[] pChecboxesChecked, int pOffersToLoad)//, List<string> pCriterias)
        {
            this.ChecboxesChecked = pChecboxesChecked;
            this.OffersToLoad = pOffersToLoad;
            //this.Criterias = pCriterias;
        }
    
    }

}

The UserControl which manages websites,

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using gControls.CustomEvents;
using WebsitesURLEnumAlias = JobSites.customEnum.WebsitesEnum;
using System.Linq;

namespace gControls
{
    public partial class UCJobsitesCheckboxes : UserControl
    {
        #region Button Search Job Delegate
        public static event EventHandler<BTNSearchJobsEventArgs> BTNSearchJobs_Clicked;
        #endregion

        #region Variables
        private int CheckedCheckboxes { get; set; }
        private int linesContainingCheckboxes = 1;
        private int xPosition = 8;
        private int yPosition = 25;
        private int myIndex = 0;
        #endregion

        #region Properties
        private List<string> CheckBoxesListing = new List<string>();
        private List<CheckBox> DrawnCheckboxes = new List<CheckBox>();

        private int Count_CheckBoxes
        {
            get
            {
                int i = 0;

                foreach (object obj in this.Controls)
                {
                    if (obj.GetType() == typeof(CheckBox))
                    {
                        i++;
                    }

                }

                return i;
            }

        }

        public Button UC_BTNSearchForJobs
        {
            get
            {
                return this.BTNSearchforJobs;
            }

        }
        #endregion

        public UCJobsitesCheckboxes()
        {
            InitializeComponent();
        }

        #region UC Controls Events
        private void NUDOffersToLoad_ValueChanged(object sender, EventArgs e)
        {
            if (this.NUDOffersToLoad.Value > 250)
            {
                this.NUDOffersToLoad.Value = 10;
            }
        
        }

        private void UCJobsitesCheckboxes_Load(object sender, EventArgs e)
        {
            addCheckboxes();
        }

        private void allChkBox_CheckedChanged(object sender, EventArgs e)
        {
            foreach (object obj in this.Controls)
            {
                if (obj.GetType() == typeof(CheckBox))
                {
                    CheckBox item = (CheckBox)obj;

                    if (item.Name != "CHKBOXSelectAll")
                    {
                        if (((CheckBox)sender).Checked)
                        {
                            item.Checked = true;
                        }
                        else
                        {
                            item.Checked = false;
                        }

                    }

                }

            }

        }

        private void checkBox_CheckedChanged(object sender, EventArgs e)
        {
            foreach (var item in this.Controls)
            {
                if (item is CheckBox && ((CheckBox)item).Checked)
                {
                    CheckedCheckboxes++;
                }

            }

            ChangeSearchButtonState();
            ResetCheckedCheckboxesTotal();

        }

        private int allCheckboxesWidth()
        {
            int width = 0;

            foreach (object obj in this.Controls)
            {
                if (obj.GetType() == typeof(CheckBox))
                {
                    width += ((CheckBox)obj).Width;
                }

            }

            return width;

        }

        private int MeasureStringWidth(CheckBox pChkBox)
        {
            Graphics g = pChkBox.CreateGraphics();
            return Convert.ToInt32(g.MeasureString(pChkBox.Text, pChkBox.Font).Width + 25);
        }

        private void ResetCheckedCheckboxesTotal()
        {
            this.CheckedCheckboxes = 0;
        }

        private void ChangeSearchButtonState()
        {
            if (this.CheckedCheckboxes < 1)
            {
                BTNSearchforJobs.Enabled = false;
            }
            else
            {
                BTNSearchforJobs.Enabled = true;
            }

        }
        #endregion

        #region Technical Method
        private void addCheckboxes()
        {
            List<string> websites = Enum.GetValues(typeof(WebsitesURLEnumAlias.WebsiteEnum)).Cast<WebsitesURLEnumAlias.WebsiteEnum>().ToList().Select(v => v.ToString()).ToList();

            foreach (var website in websites)
            {
                CheckBox CHKBox = new CheckBox();
                CHKBox.Text = website;
                CHKBox.Name = "CHKBox" + website;
                CHKBox.Location = calculateLocation(CHKBox);
                CHKBox.CheckedChanged += new EventHandler(checkBox_CheckedChanged);
                rememberThisCheckbox(CHKBox);
                this.Controls.Add(CHKBox);
            }

            addSelectAllCheckBox();
        
        }

        private void rememberThisCheckbox(CheckBox CHKBox)
        {
            DrawnCheckboxes.Add(CHKBox);
        }

        private Point calculateLocation(CheckBox chkBox)
        {
            if (myIndex > 0)
            {
                xPosition += chkBox.Width;

                // This IF statement isn't easy to read so let's explain ;)
                // First of all, let's calculate the Total Width of All CheckBoxes
                // Secundo, Divide the Total ChecBoxes Width by the Number of Lines which Contains CheckBoxes
                // Tertio, add the Current CheckBox Text to the Total CheckBoxes Width divided by the number of Lines containg CheckBoxes
                // And Finally, check if the Total of Above is bigger than the User Control Width which we must soustract the Panel Width
                //
                // NB : The panel Contains the Search For Jobs Button and a Numeric UpDown
                if (((allCheckboxesWidth() / linesContainingCheckboxes) + MeasureStringWidth(chkBox)) >= ((this.Width - this.panel1.Width)))
                {

                    // if the Total of Above is bigger than the User Control Width which we soustract the Panel Width
                    // We Go to another newLine
                    linesContainingCheckboxes = linesContainingCheckboxes + 1;
                    // Then Reset the X Position of the next Checkboxes
                    xPosition = 8;
                    // And change the Y position Value to get a coherent Graphical interface
                    yPosition = (20 * linesContainingCheckboxes) + 8;
                }

            }

            myIndex = myIndex + 1;
            return new Point(xPosition, yPosition);
        }

        private void addSelectAllCheckBox()
        {
            if (this.Count_CheckBoxes > 0)
            {
                CheckBox allChkBox = new CheckBox();
                allChkBox.Name = "CHKBOXSelectAll";
                allChkBox.Text = "Tous les Sites";

                allChkBox.Location = calculateLocation(allChkBox);

                allChkBox.CheckedChanged += new EventHandler(allChkBox_CheckedChanged);
                rememberThisCheckbox(allChkBox);
                this.Controls.Add(allChkBox);

                resetVariables();

            }

        }

        private void resetVariables()
        {
            linesContainingCheckboxes = 1;
            yPosition = 25;
            xPosition = 8;
            myIndex = 0;
        }
        #endregion

        #region Button Search Jobs Event
        public virtual void BTNSearchforJobs_Click(object sender, EventArgs e)
        {
            int offersToLoad = Convert.ToInt32(this.NUDOffersToLoad.Value);

            for (int i = 0; i < this.Controls.Count; i++)
            {
                if (this.Controls[i].GetType() == typeof(CheckBox) && ((CheckBox)this.Controls[i]).Checked && ((CheckBox)this.Controls[i]).Name != "CHKBOXSelectAll")
                {
                    this.CheckBoxesListing.Add((this.Controls[i] as CheckBox).Text);
                }

            }

            if (BTNSearchJobs_Clicked != null)
            {
                BTNSearchJobsEventArgs BTNSearchJobClicked_Event = new BTNSearchJobsEventArgs(CheckBoxesListing.ToArray(), offersToLoad);
                BTNSearchJobs_Clicked.Invoke(this, BTNSearchJobClicked_Event);
            }

        }
        #endregion

    }

}

and finally the last UserControl, which manages the criterias:

using System;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using gControls.CustomEvents;

namespace gControls
{
    public partial class UCJobSearchCriteriaManager : UserControl
    {
        public int CheckedCheckboxes { get; private set; }
        private List<string> Criterias { get; set; }

        public UCJobSearchCriteriaManager()
        {
            InitializeComponent();
        }

        private void UCJobSearchCriteriaManager_Load(object sender, EventArgs e)
        {
            this.CHKBoxExactTerm.Checked = true;
        }

        #region UC Events
        private void CHKBox_CheckedChanged(object sender, EventArgs e)
        {
            string senderName = ((CheckBox)sender).Name;

            bool senderIsChecked = ((CheckBox)sender).Checked;

            if (!senderIsChecked && senderName == this.CHKBoxAllWords.Name)
            {
                this.CHKBoxExactTerm.Checked = !senderIsChecked;
            }

            if (!senderIsChecked && senderName == this.CHKBoxExactTerm.Name)
            {
                this.CHKBoxAllWords.Checked = !senderIsChecked;
            }

            changeCriteriaManagerButtonsState();
        
        }

        private void BTNAddCriteria_Click(object sender, EventArgs e)
        {
            if (this.TXTBoxCriteria.Text != string.Empty)
            {
                foreach (string criteria in Regex.Split(TXTBoxCriteria.Text, Environment.NewLine))
                {
                    if (CHKBoxExactTerm.Checked && !CHKBoxAllWords.Checked)
                    {
                        listBoxCriterias_addCriteria(criteria);
                    }
                    else if (CHKBoxAllWords.Checked && !CHKBoxExactTerm.Checked)
                    {
                        listBoxCriterias_addCriteria(criteria.Split(' '));
                    }
                    else
                    {
                        listBoxCriterias_addCriteria(criteria);
                        listBoxCriterias_addCriteria(criteria.Split(' '));
                    }

                }

            }

            cleanCriteriaTXTBox();

        }

        private void BTNRemoveAllCriterias_Click(object sender, EventArgs e)
        {
            if (this.LSTBoxWithAddRemoveEvents.Items.Count > 0)
            {
                if (MessageBox.Show("Etes-vous sûr de vouloir réinitialiser vos critères de recherches ?", "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    this.LSTBoxWithAddRemoveEvents.Items.Clear();
                }

            }

        }

        private void BTNRemoveCriteria_Click(object sender, EventArgs e)
        {
            while (this.LSTBoxWithAddRemoveEvents.SelectedItems.Count > 0)
            {
                this.LSTBoxWithAddRemoveEvents.Items.Remove(this.LSTBoxWithAddRemoveEvents.SelectedItems[0]);
            }

        }
        #endregion

        #region Technical Method
        private void changeCriteriaManagerButtonsState()
        {
            foreach (var item in this.Controls)
            {
                if (item is CheckBox && ((CheckBox)item).Checked)
                {
                    CheckedCheckboxes++;
                }

            }

            if (this.CheckedCheckboxes > 0)
            {
                foreach (var item in this.Controls)
                {
                    if (item is Button)
                    {
                        ((Button)item).Enabled = true;
                    }
                
                }
            
            }

            ResetCheckedCheckboxesTotal();
        
        }

        private void ResetCheckedCheckboxesTotal()
        {
            this.CheckedCheckboxes = 0;
        }

        private void listBoxCriterias_addCriteria(string[] pCriterias, bool pDragDropEvent = false)
        {
            if (pCriterias.Length > 0)
            {
                foreach (string critera in pCriterias)
                {
                    listBoxCriterias_addCriteria(critera, pDragDropEvent);
                }

            }

        }

        private void listBoxCriterias_addCriteria(string pCriteria, bool pDragDropEvent = false)
        {
            if (pCriteria != String.Empty && !this.LSTBoxWithAddRemoveEvents.Items.Contains(pCriteria))
            {
                this.LSTBoxWithAddRemoveEvents.Items.Add(pCriteria);

                if (!pDragDropEvent && this.TXTBoxCriteria.Text != string.Empty)
                {
                    this.TXTBoxCriteria.Text = this.TXTBoxCriteria.Text.Remove(this.TXTBoxCriteria.Text.IndexOf(pCriteria[0]), pCriteria.Length);
                }

            }

        }

        private void cleanCriteriaTXTBox()
        {
            string[] criteriasInTextBox = Regex.Split(TXTBoxCriteria.Text, Environment.NewLine);
            this.TXTBoxCriteria.Clear();

            foreach (string criteria in criteriasInTextBox)
            {
                if (criteria != string.Empty && criteria != Environment.NewLine)
                {
                    this.TXTBoxCriteria.Text += criteria + Environment.NewLine;
                }

            }

        }
        #endregion

        private void UCJobSearchCriteriaManager_DragEnter(object sender, DragEventArgs e)
        {

        }

        private void UCJobSearchCriteriaManager_DragDrop(object sender, DragEventArgs e)
        {

        }

    }

}

I hope it is understandable and would thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.