我表单上的按钮没有显示出来

时间:2015-04-08 13:55:59

标签: c# winforms

我需要帮助才能使这段代码正常工作......我不知道我在这里做错了什么。我正在使用Windows窗体应用程序中的许多按钮制作程序。我有三个类,Form1.cs,easy.cs和Methods.cs。 Form1类只是为了让你可以点击一个按钮来获得另一个窗口,就像你点击easy一样,easy类会弹出。我在Methods类中创建了我的按钮(参见下面的代码)。现在,这是事情,我想从我的类方法中获取函数,它似乎工作但有一件事,按钮不显示。在Methods.cs中的函数fylla_takka应该创建按钮,我想它确实如此,但是没有显示它们。然后是butt_click函数,就是当你点击一个按钮时就会发生这种情况。我希望在课堂上有这些东西,所以我可以一次又一次地使用它。 很抱歉在我的代码中使用Icelandic进行评论,但我没有希望将代码显示给其他人:)

Form1.cs中:

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

    namespace Samstæðuleikur
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();

            }


            easy easy = new easy();
            public void button1_Click(object sender, EventArgs e)
            {
                easy.Show();   
            }

            private void button2_Click(object sender, EventArgs e)
            {

            }



        }
    }

easy.cs:

  using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Samstæðuleikur
{
    public partial class easy : Form
    {
        public easy()
        {
            InitializeComponent();
        }

        Methods methods = new Methods();
        private void easy_Load(object sender, EventArgs e)
        {
            methods.fylla_takka(3,3);//This one is working
        }
    }
}

Methods.cs:

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

namespace Samstæðuleikur
{
    public partial class Methods : Form
    {
        public Methods()
        {
            InitializeComponent();
        }

       int counter = 0;
       int teljari = 0;
       int counter2 = 0;
            Random rand = new Random();//Ný random tala
             int ragdomly_easy = 0;
             int ragdomly_easy1 = 0;
            int ragdomly_easy2 = 0;
            int snowy1 = 0;
            int snowy2 = 0;
            int snowy3 = 0;

        public void ragdmomly(int endir)//Bý til fall sem velur 3 random tölur frá 1 upp í enda
        {

            ragdomly_easy = rand.Next(1, endir);
            ragdomly_easy1 = rand.Next(1, endir);
            ragdomly_easy2 = rand.Next(1, endir);
            int snowy1 = ragdomly_easy;//Planta fyrsta snowy
            int snowy2 = (ragdomly_easy1);//Annað snowy
            int snowy3 = (ragdomly_easy2);//Þriðja snowy
        }




        List<Button> buttons = new List<Button>();//Skilgreini lista svo ég get haft eins marga takka og ég vil!
        public void fylla_takka(int lengd, int fjoldi)//Býr til takkana, eins marga og ég vil!
        {

            MessageBox.Show("Testing if my function works, you see this message so it's working.");
            int x = 50;
            int y = 35;


            for (int i = 0; i < fjoldi; i++)//Keyri hve_margir_takkar sinnum yfir listann
            {
                teljari++;
                buttons.Add(new Button());//Set takkann í listann svo að það sé hægt að velja ákveðinn takka
                buttons[i].Location = new Point(x, y);//Vel frá listanum takka nr i og gef honum staðsetningu x og y
                buttons[i].Size = new Size(50, 50);//Vel frá listanum takka nr i og breyti stærð hans

                x = x + 50;//Takkinn, næst þegar lúppan keyrir, fer 35 til hliðar.

                if (teljari == lengd)//Hversu margir takkar eiga að vera í hverri röð
                {
                    y = y + 50;//Ef að lengdinni er náð þá förum við í næstu línu
                    x = 50;
                    teljari = 0;
                }

                this.Controls.Add(buttons[i]);// <-- Something may be wrong here, the buttons are not showing up Set takkann á formið, lúppan keyrir x mörgum sinnum og þá koma x margir
                //takkar
                buttons[i].Click += new EventHandler(butt_Click);//Ef ýtt er á takkann þá fer forritið í
                //eventHandlerinn butt_Click
                buttons[i].Name = i.ToString();
                counter++;
                counter2++;
            }

        }


       public void butt_Click(object sender, EventArgs e)//Eventhandlerinn butt_Click
        {

            Button temp = (Button)sender;//Ef að eitthvað gerist í takkanum þá gerist eventið s.s. allt sem er inn í butt_Click
            //í þessu tilfelli.


            for (int i = 0; i < buttons.Count; i++)//Keyri í gegnum takka listann
            {

                if (temp.Name == Convert.ToString(snowy1))
                {

                    temp.BackColor = Color.Green;
                    counter = counter + 1;
                    temp.Text = "Snowy";
                }

                else if (temp.Name == Convert.ToString(snowy2))
                {
                    temp.BackColor = Color.Green;
                    counter = counter + 1;
                    temp.Text = "Snowy";
                }

                else if (temp.Name == Convert.ToString(snowy3))
                {
                    temp.BackColor = Color.Green;
                    counter = counter + 1;
                    temp.Text = "Snowy";
                }


                else
                {
                    temp.BackColor = Color.Red;//Breyti litnum í rauðann
                    counter2++;
                }
            }
            if (counter == 120)//Þegar ýtt er á takka þá bætist alltaf 30 við því takarnir eru 30 30*3=120
            {

                MessageBox.Show("SNOWY!");
            }

            if (counter2 == 840 || counter2 == 720)
            {
                MessageBox.Show("Sorry, No Snowy.");
            }
        }

    }
}

我希望有人可以提供帮助:)。

2 个答案:

答案 0 :(得分:0)

事情是,我在我的一个表格中打电话给错误的班级。 easy.cs甚至不必在那里!然后我可以一次又一次地使用它。那么正确的解决方案就是:

Methods methods = new Methods();//Take a "copy" of the class Methods
    public void button1_Click(object sender, EventArgs e)//When button is clicked:
    {
        methods.Show();
        methods.fylla_takka(3, 3);//This one is working  
    }

我们不必使用easy类,而且Methods类保持不变。

答案 1 :(得分:-2)

通常,当控件添加到布局中时,它们在InitializeComponent()函数中完成。你不必在那里做,但如果你看一下这个构建的int函数,你会看到对this.Controls.Add( )的调用被暂停和恢复布局所包围。这些都是昂贵的调用,所以尽量减少尝试将整个for循环包装在其中:

this.SuspendLayout();

for (int i = 0; i < fjoldi; i++)//Keyri hve_margir_takkar sinnum yfir listann
{
  . . .

this.Controls.Add(buttons[i]);

. . .

}

this.ResumeLayout(false);
this.PerformLayout();