'How to stop buttons getting top of each other?

I wanna make a game which you can sort the buttons which the game create by their weights depends on their colors from minimum to maximum, I create a class and typed this codes below and create a new form to run this game and then I added PLAY GAME button on MenuStrip on my original form so when you click the PLAY GAME button it summons the new form which is providing to play game the codes inside my new class but somehow the buttons I create is getting overlap, top of each other but i can't figure out how it happened, I am a begginer and get stuck with that what can i do for it?

codes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;

namespace a
{
     class MedTier
     {
        private int color_number;
        private int button_number;
        private Form form_object;
        private Button[] B;
        private ArrayList color;
        private bool tik;
        private Point tik_koordinat;
        private int extend;
        private int weight;
        private int panel_weight;
        private ArrayList PanelButtonList;

        public MedTier(int color_number, int button_number, int extend, int weight, Form form_object, int panel_weight)
        {
            this.color_number = color_number;
            this.button_number = button_number;
            this.form_object = form_object;
            this.extend = extend;
            this.weight = weight;
            this.panel_weight = panel_weight;
            tik = false;
            color = new ArrayList();
            B = new Button[button_number];
            create_color();
            create_button(form_object);
            PanelButtonList = new ArrayList();
        }
        private void bcreate_button(Form form_object)
        {
            Random r = new Random();
            for (int i = 0; i < button_number; i++)
            {
                B[i] = new Button();
                B[i].Name = i.ToString();
                B[i].Text = i.ToString();
                B[i].Size = new Size(50, r.Next(30, panel_weight));
                B[i].Location = new Point(r.Next(0, 255), r.Next(0, 255));
                B[i].BackColor = (Color)color[r.Next(0, color.Count)];
                B[i].MouseDown += new MouseEventHandler(MDown);
                B[i].MouseUp += new MouseEventHandler(MUp);
                B[i].MouseMove += new MouseEventHandler(MMove);
                form_object.Controls.Add(B[i]);
            }
        }
        private void MMove(object sender, MouseEventArgs e)
        {
            Button G;
            G = (Button)sender;
            if (tik)
            {
                G.Location = new Point(e.X + G.Left - tik_koordinat.X, e.Y + G.Top - tik_koordinat.Y);
            }
        }
        private void MUp(object sender, MouseEventArgs e)
        {
            tik = false;
            Button G;
            G = (Button)sender;
            int a = (G.Location.Y + G.Height);
            int b = (form_object.Height - panel_weight);
            if (a > b)
            {
                int y = form_object.Height - G.Height;
                G.Location = new Point(buttonA(G.Location.X), y);
                if (PanelButtonList.IndexOf(G) < 0)
                {
                    PanelButtonList.Add(G);
                    if (button_number == PanelButtonList.Count)
                    {
                        int no = Check();
                        if (no >= 0)
                        {
                            MessageBox.Show(" You finished the game but the buttons you have sort wrong is marked with 'hatalı'.");
                            ((Button)PanelButtonList[no]).Text = ((Button)PanelButtonList[no]).Name;
                        }
                        else
                            MessageBox.Show("Conragulations, you complete the game succesfully!");

                    }
                }
            }
            else
            {
                for (int i = 0; i < PanelButtonList.Count; i++)
                {
                    if (((Button)PanelButtonList[i]).Name == G.Name)
                    {
                        PanelButtonList.RemoveAt(PanelButtonList.IndexOf(G));
                        break;
                    }
                }

            }
        }
        private void MDown(object sender, MouseEventArgs e)
        {
            tik = true;
            tik_koordinat = e.Location;
        }
        private void creake_color()
        {
            Random r = new Random();
            for (int i = 0; i < color_number; i++)
            {
                color.Add(Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255)));
            }
        }
        int buttonA(int x)
        {
            int k = 0;
            Button BK;
            for (int i = 0; i < PanelButtonList.Count; i++)
            {
                BK = ((Button)PanelButtonList[i]);
                if ((BK.Location.X + BK.Width) > k)
                {
                    k = BK.Location.X + BK.Width;
                }
            }
            return k;
        }
        int Check()
        {
            int results = -1, maxy = -1;
            ArrayList Renk = new ArrayList();
            Button Bt;
            for (int i = 0; i < PanelButtonList.Count; i++)
            {
                Bt = (Button)PanelButtonList[i];
                if (i == 0)
                {
                    maxy = Bt.Height;
                    color.Add(Bt.BackColor);
                }
                else
                {
                    if (((Color)color[color.Count - 1]) == Bt.BackColor && maxy > Bt.Height)
                    {

                        Bt.Text = "Hatalı";
                        return i;
                    }
                    else if (((Color)color[color.Count - 1]) != Bt.BackColor)
                    {
                        if (color_check(Bt.BackColor, color))
                        {
                            Bt.Text = "Hatalı";
                            return i;
                        }
                        else
                        {
                            maxy = Bt.Height;
                            color.Add(Bt.BackColor);
                        }
                    }
                    maxy = Bt.Height;
                }
            }
            return results;
        }
        bool color_check(Color R, ArrayList Renk)
        {
            for (int i = 0; i < color.Count; i++)
            {
                if (R.Equals((Color)Renk[i]))
                    return true;
            }
            return false;
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source