如何在C#中使表格可见

时间:2018-12-18 09:48:02

标签: c# forms winforms keyboard panel

我正在尝试为C#中的作业设计钢琴。我创建了一个MusicKey类来存储音乐键(以及BlackMusicKey类)。我在面板“ panel1”中填充如下音乐键:

this.panel1.Controls.Add(bmk);

在音乐键构造器中,我为每个音乐键设置位置和大小,并确保将“可见性”设置为true。但是,当我运行该表单时,它是完全空白的。

有什么我想念的吗?我非常确定音乐键的可见性没有问题,因此在使面板可见方面确实缺少一些东西。

任何帮助将不胜感激,谢谢!

注意:我还尝试使用panel1.Show()仍然无法正常工作。

所有相关代码都可以在下面找到:

MusicKeyClass:

class MusKey : System.Windows.Forms.Button
{
    private int musicNote; //determines the pitch mapped to number

    public MusKey(int iNote, int x, int y) : base()
    {
        musicNote = iNote;
        this.Location = new System.Drawing.Point(x, y);
        this.Size = new System.Drawing.Size(20, 80);
        this.Visible = true;

    }

    public int getMusicNote()
    {
        return musicNote;
    }
}

Form1类:

public partial class Form1 : Form
{
    int count = 0;
    int xLoc = 50;
    int yLoc = 30;
    int[] whitePitch = { 1, 3, 5, 6, 8, 10, 12, 13, 15, 17, 18, 20, 22, 24 };
    Panel panel1 = new Panel();
    System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
    Button button1 = new Button();

    private void Form1_Load(object sender, EventArgs e)
    {
        this.Paint += new PaintEventHandler(function);
        MusKey mk;
        BlackMusKey bmk;
        for (int k = 0; k < 14; k++)
        {
            int pitch = whitePitch[k];
            int ixPos = k * 20;
            mk = new MusKey(pitch, ixPos, yLoc);
            mk.MouseDown += new MouseEventHandler(this.button1_MouseDown);
            mk.MouseUp += new MouseEventHandler(this.button1_MouseUp); 
            this.panel1.Controls.Add(mk);
        }


        int xOffs = 20;
        int[] blackPitch = { 2, 4, 7, 9, 11, 14, 16, 19, 21, 23 };
        int[] xPos = { 10, 30, 70, 110, 150, 170, 210, 230, 250 };
        const int yPosBlack = 50;

        for (int k = 0; k < 10; k++)
        {
            int pitch = blackPitch[k];
            int ixPos = xPos[k];
            bmk = new BlackMusKey(pitch, ixPos, yPosBlack);
            bmk.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown); //create event MouseDown
            bmk.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp);  //create event MouseUp
            this.panel1.Controls.Add(bmk);
            this.panel1.Controls[this.panel1.Controls.Count - 1].BringToFront();
        }


    }

    SoundPlayer sp = new SoundPlayer();
    int count1;

    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        foreach (MusKey mk in this.panel1.Controls)
        {
            if (sender == mk)
            { //true for the specific key pressed on the Music Keyboard
                if (e.Button == MouseButtons.Left)
                {
                    timer1.Enabled = true; //variable of the Timer component
                    count = 0; //incremented by the timer1_Tick event handler
                    timer1.Start();
                    sp.SoundLocation = (mk.getMusicNote() + ".wav"); //might need to convert ToString() ??
                    sp.Play();
                }
            }
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        count = count++;
    }

    private void button1_MouseUp(object sender, MouseEventArgs e)
    {
        foreach (MusKey mk in this.panel1.Controls)
        {
            if (sender == mk) //true for the specific key pressed on the Music Keyboard
            {
                if (e.Button == MouseButtons.Left)
                {
                    timer1.Enabled = false;
                    sp.Stop();
                    string bNoteShape = null;
                    int duration = 0;
                    if (count >= 16)
                    {
                        bNoteShape = "SemiBreve";
                        duration = 16;
                    }
                    if (count >= 8 && count <= 15)
                    {
                        bNoteShape = "DotMinim";
                        duration = (8 + 15) / 2;
                    }
                    if (count >= 4 && count <= 7)
                    {
                        bNoteShape = "Crotchet";
                        duration = (4 + 7) / 2;
                    }
                    if (count >= 2 && count <= 3)
                    {
                        bNoteShape = "Quaver";
                        duration = (2 + 3) / 2;
                    }
                    if (count >= 1)
                    {
                        bNoteShape = "Semi-Quaver";
                        duration = 1;
                    }
                    MusicNote mn = new MusicNote(mk.getMusicNote(), duration, bNoteShape); //music note construction
                                                                                           // mn.Location = new Point(xLoc, yLoc);
                                                                                           //this.panel2.Controls.Add(this.mn); //adding MusicNote component to MusicStaff (panel2) collection
                    xLoc = xLoc + 15;
                }

            }
        }
    }

    private void function(object sender, PaintEventArgs e)
    {
        panel1.Show();
        panel1.Visible = true;
        panel1.BackColor = Color.Blue;
        panel1.Size = new Size(5, 5);
        panel1.Location = new Point(3, 3);
        this.Controls.Add(panel1);
        this.Controls.Add(button1);

    }

    private void Form1_Load_1(object sender, EventArgs e)
    {

    }
}

Form 1 [designer]类:

partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    // <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.SuspendLayout();
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(800, 450);
        this.Name = "Form1";
        this.Text = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load_1);
        this.ResumeLayout(false);

    }

    #endregion
}

音符类

class MusicNote
{
    public int notepitch;
    public String noteshape;
    public int noteduration;
    enum accid { sharp, flat, sole };


    bool dragging = false;
    System.Timers.Timer timer1 = new System.Timers.Timer();

    public MusicNote(int iNotepitch, int iDuration, String iBnoteShape)
    {
        notepitch = iNotepitch;
        noteduration = iDuration;
        noteshape = iBnoteShape;
    }

    bool timeron = false;
    bool changenote = false;
    public static int start = 0;


    //public void click(object sender, MouseEventArgs e) { }

    //public void RightPress(object sender, MouseEventArgs e) { }

}

}

黑色音乐键类

 class BlackMusKey : MusKey
{
    public BlackMusKey(int iNote, int x, int y) : base(iNote, x, y)
    {
        this.BackColor = Color.Black;
        this.Size = new System.Drawing.Size(20, 60);
    }
}

程序类

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

1 个答案:

答案 0 :(得分:0)

根据Form1类代码,缺少构造函数。请使用InitializeComponent()方法调用添加适当的构造函数。可以将其添加到类正文中的任何位置。 代码段:

public partial class Form1 : Form
{
    [...] //your objects declarations

    public Form1() 
    { 
        InitializeComponent(); 
    }

    [...] //rest of your code
}

请接受正确的答案。