Untitled

 avatar
unknown
csharp
11 days ago
14 kB
5
Indexable
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace RecoursiveConstructor_Image_Text
{
    public partial class Form1 : Form
    {
        private UserControl1[] arrUC;
        UserControl1 firstUC, secondUC;
        private int arrUser_size = 2;
        static private Form1 Cat_Form, Dog_Form;
        private static Random formRand = new Random();

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        int GetBrightness(Color c)
        {
            return (int)(0.299 * c.R + 0.587 * c.G + 0.114 * c.B);
        }


        public Form1(int counter)
        {
            InitializeComponent();
            this.AutoScaleMode = AutoScaleMode.Dpi;
            this.WindowState = FormWindowState.Normal;

            arrUC = new UserControl1[arrUser_size];
            bool firstRound = false;
            for (int i = 0; i < arrUser_size; i++)
            {
                if (!firstRound)
                {
                    arrUC[i] = new UserControl1(formRand);
                    arrUC[i].Location = new Point(0, 30 + 120 * i + 50);
                    arrUC[i].event_FromUC += new delegate_MyEventHadler(Form_event_FromUC);
                    this.Controls.Add(arrUC[i]);
                    firstRound = true;
                }
                else
                {
                    arrUC[i] = new UserControl1(formRand);
                    arrUC[i].Location = new Point(0, 30 + 120 * i + 70);
                    arrUC[i].event_FromUC += new delegate_MyEventHadler(Form_event_FromUC);
                    this.Controls.Add(arrUC[i]);

                }
            }

            if (counter == 2)
            {
                Cat_Form = this;

                Form1 temp = new Form1(1);
                temp.Show();
            }

            if (counter == 1)
            {
                this.Text = "Dog";
                Dog_Form = this;

                if (formRand.Next(2) == 0)
                {
                    Cat_Form.Min_Max_label.Text = "Min";
                    Dog_Form.Min_Max_label.Text = "Max";
                }
                else
                {
                    Cat_Form.Min_Max_label.Text = "Max";
                    Dog_Form.Min_Max_label.Text = "Min";
                }

                if (formRand.Next(2) == 0)
                {
                    Cat_Form.Image_Text_label.Text = "Image";
                    Cat_Form.bySize_byBrightness_label.Text = "bySize";
                    Dog_Form.Image_Text_label.Text = "Text";
                    Dog_Form.bySize_byBrightness_label.Text = "byBrightness";
                }
                else
                {
                    Dog_Form.Image_Text_label.Text = "Image";
                    Dog_Form.bySize_byBrightness_label.Text = "bySize";
                    Cat_Form.Image_Text_label.Text = "Text";
                    Cat_Form.bySize_byBrightness_label.Text = "byBrightness";
                }
            }
        }

        private Label cloneLbl(Label l)
        {
            Label cur = new Label();
            cur.Text = l.Text;
            cur.Image = l.Image;
            cur.Font = l.Font;
            cur.Tag = l.Tag;
            cur.Size = l.Size;
            cur.BackColor = l.BackColor;
            cur.ForeColor = l.ForeColor;
            cur.TextAlign = l.TextAlign;
            cur.Name = l.Name;

            return cur;
        }
        private void Form_event_FromUC(object sender, myEventArgs e)
        {
            //List<Label> catList = new List<Label>();
            //List<Label> dogList = new List<Label>();
            List<Label> filteredDogList = new List<Label>();
            List<Label> filteredCatList = new List<Label>();
            Label clickedLabel = e.clicked;

            //List<Label> SortedList = new List<Label>();

            //if (clickedLabel.Name == "Cat")
            //    catList.Add(clickedLabel);
            //else if (clickedLabel.Name == "Dog")
            //    dogList.Add(clickedLabel);

            string color;
            if (radioButton_Red.Checked)
            {
                color = "Red";
            }
            else if (radioButton_Green.Checked)
            {
                color = "Green";
            }
            else
            {
                color = "Red";
            }

            bool isText = clickedLabel.Image == null;
            bool isImage = clickedLabel.Image != null;

            if (isText)
            {
                if ((clickedLabel.BackColor.R > 0 && clickedLabel.BackColor.G == 0) && color == "Red")
                {
                    if (clickedLabel.Name == "Cat")
                    {
                        filteredCatList.Add(clickedLabel);
                    }
                    else
                    {
                        filteredDogList.Add(clickedLabel);
                    }
                }
                else if (color == "Green" && clickedLabel.BackColor.R == 0 && clickedLabel.BackColor.G > 0)
                {
                    if (clickedLabel.Name == "Cat")
                        filteredCatList.Add(clickedLabel);
                    else if (clickedLabel.Name == "Dog")
                        filteredDogList.Add(clickedLabel);
                }
            }

            if (isImage)
            {
                if (clickedLabel.Tag != null && clickedLabel.Tag.ToString() == color)
                {
                    if (clickedLabel.Name == "Cat")
                        filteredCatList.Add(clickedLabel);
                    else
                        filteredDogList.Add(clickedLabel);
                }
            }

            Label catsLabel = null;
            Label dogsLabel = null;

            //עכשיו צרך לעשות MIN MAX BYSIZE BY BIGHTNES            
            if (filteredCatList.Count > 0)
            {
                string cmode = Cat_Form.bySize_byBrightness_label.Text;

                if (Cat_Form.Min_Max_label.Text == "Min")
                {

                    if (cmode == "bySize")
                    {
                        catsLabel = filteredCatList.OrderBy(lbl => lbl.Width * lbl.Height).FirstOrDefault();
                    }
                    else if (cmode == "byBrightness")
                    {
                        catsLabel = filteredCatList.OrderBy(lbl => GetBrightness(lbl.BackColor)).First();
                    }
                    else
                    {
                        catsLabel = filteredCatList.OrderBy(lbl => lbl.Width * lbl.Height).FirstOrDefault();
                    }

                }
                else
                {
                    if (cmode == "bySize")
                    {

                        catsLabel = filteredCatList.OrderByDescending(lbl => lbl.Width * lbl.Height).FirstOrDefault();
                    }
                    else if (cmode == "byBrightness")
                    {
                        catsLabel = filteredCatList.OrderByDescending(lbl => GetBrightness(lbl.BackColor)).First();
                    }
                    else
                    {
                        catsLabel = filteredCatList.OrderByDescending(lbl => lbl.Width * lbl.Height).FirstOrDefault();
                    }
                }
            }

            if (filteredDogList.Count > 0)
            {
                string dmode = Dog_Form.bySize_byBrightness_label.Text;
                if (Dog_Form.Min_Max_label.Text == "Min")
                {
                    if (dmode == "bySize")
                    {
                        dogsLabel = filteredDogList.OrderBy(lbl => lbl.Width * lbl.Height).FirstOrDefault();
                    }
                    else if (dmode == "byBrightness")
                    {
                        dogsLabel = filteredDogList.OrderBy(lbl => GetBrightness(lbl.BackColor)).First();
                    }
                    else
                    {
                        dogsLabel = filteredDogList.OrderBy(lbl => lbl.Width * lbl.Height).FirstOrDefault();
                    }

                }
                else
                {
                    if (dmode == "bySize")
                    {
                        dogsLabel = filteredDogList.OrderByDescending(lbl => lbl.Width * lbl.Height).FirstOrDefault();
                    }
                    else if (dmode == "byBrightness")
                    {
                        dogsLabel = filteredDogList.OrderByDescending(lbl => GetBrightness(lbl.BackColor)).First();
                    }
                    else
                    {
                        dogsLabel = filteredDogList.OrderByDescending(lbl => lbl.Width * lbl.Height).FirstOrDefault();
                    }
                }
            }

            UserControl1 uc = sender as UserControl1;
            if (firstUC == null)//בדוק איזה יוזר קונטרול
            {
                firstUC = uc;
            }
            else if (uc != firstUC)
            {
                secondUC = uc;
            }

            List<Label> sortedCat = new List<Label>();
            List<Label> sortedDog = new List<Label>();

            String dogMode = Dog_Form.bySize_byBrightness_label.Text;
            String catMode = Cat_Form.bySize_byBrightness_label.Text;

            if (Cat_Form.Min_Max_label.Text == "Min")
            {
                if (catMode == "bySize")
                {
                    sortedCat = filteredCatList.OrderBy(lbl => lbl.Width * lbl.Height).ToList();
                }
                else if (catMode == "byBrightness")
                {
                    sortedCat = filteredCatList.OrderBy(lbl => GetBrightness(lbl.BackColor)).ToList();
                }
                else
                {
                    sortedCat = filteredCatList.OrderBy(lbl => lbl.Width * lbl.Height).ToList();
                }
            }
            else
            {
                if (catMode == "bySize")
                {
                    sortedCat = filteredCatList.OrderByDescending(lbl => lbl.Width * lbl.Height).ToList();
                }
                else if (catMode == "byBrightness")
                {
                    sortedCat = filteredCatList.OrderByDescending(lbl => GetBrightness(lbl.BackColor)).ToList();
                }
                else
                {
                    sortedCat = filteredCatList.OrderByDescending(lbl => lbl.Width * lbl.Height).ToList();
                }
            }

            if (Dog_Form.Min_Max_label.Text == "Min")
            {
                if (dogMode == "bySize")
                {
                    sortedDog = filteredDogList.OrderBy(lbl => lbl.Width * lbl.Height).ToList();
                }
                else if (dogMode == "byBrightness")
                {
                    sortedDog = filteredDogList.OrderBy(lbl => GetBrightness(lbl.BackColor)).ToList();
                }
                else
                {
                    sortedDog = filteredDogList.OrderBy(lbl => lbl.Width * lbl.Height).ToList();
                }
            }
            else
            {
                if (dogMode == "bySize")
                {
                    sortedDog = filteredDogList.OrderByDescending(lbl => lbl.Width * lbl.Height).ToList();
                }
                else if (dogMode == "byBrightness")
                {
                    sortedDog = filteredDogList.OrderByDescending(lbl => GetBrightness(lbl.BackColor)).ToList();
                }
                else
                {
                    sortedDog = filteredDogList.OrderByDescending(lbl => lbl.Width * lbl.Height).ToList();
                }
            }

            //SEC: B
            if (firstUC != null && secondUC != null)
            {
                firstUC.Controls.Clear();
                secondUC.Controls.Clear();
            }
            int x = 20, y = 5;//place

            foreach (Label l in sortedCat)
            {
                l.AutoSize = false;           // חשוב
                l.Location = new Point(x, y);
                firstUC.Controls.Add(l);

                x += l.Width + 20;
            }

            x = 20; y = 5;//place

            foreach (Label l in sortedDog)
            {
                l.AutoSize = false;           // חשוב
                l.Location = new Point(x, y);
                firstUC.Controls.Add(l);

                x += l.Width + 20;
            }

            x = 20; y = 5;
            foreach (Label n in sortedCat)
            {
                Label c = cloneLbl(n);
                c.AutoSize = false;

                if (secondUC != null)
                {
                    secondUC.Controls.Add(c);
                    c.Location = new Point(x, y);

                    int widthLabel = c.Width;
                    x += n.Width + 20;
                }
            }

            x = 20; y = 5;
            foreach (Label n in sortedDog)
            {
                Label c = cloneLbl(n);
                c.AutoSize = false;

                if (secondUC != null)
                {
                    secondUC.Controls.Add(c);
                    c.Location = new Point(x, y);

                    int widthLabel = c.Width;
                    x += n.Width + 20;
                }
            }

            if (catsLabel != null)
            {
                Cat_Form.MinMax_Result_label.Text = catsLabel.Text;
                Cat_Form.MinMax_Result_label.Image = catsLabel.Image;
                Cat_Form.MinMax_Result_label.Size = catsLabel.Size;
                Cat_Form.MinMax_Result_label.BackColor = catsLabel.BackColor;

            }
            if (dogsLabel != null)
            {
                Dog_Form.MinMax_Result_label.Text = dogsLabel.Text;
                Dog_Form.MinMax_Result_label.Image = dogsLabel.Image;
                Dog_Form.MinMax_Result_label.Size = dogsLabel.Size;
                Dog_Form.MinMax_Result_label.BackColor = dogsLabel.BackColor;
            }
        }
    }
}
Editor is loading...
Leave a Comment