我正在編寫一個程序,以便單擊一個按鈕,並根據您將鼠標放下的時間,打印鏈接的註釋。我的問題是,第一次點擊工作正常,但當我第二次堅持它不會更新,這是讓我瘋狂。任何幫助都感激不盡。下面找到我使用的以下代碼。由於控制和輸出音樂註釋
對於Form1中:
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 NoteShape
{
public partial class Form1 : Form
{
public int duration = 0;
MusicNote mn;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
duration++;
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
timer1.Enabled = true;
duration = 0;
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
timer1.Enabled = false;
}
duration = duration % 20;
string bNoteShape = "";
if (duration >= 12)
{
bNoteShape = "SemiBreve";
}
if ((duration >= 6) && (duration <= 11))
{
bNoteShape = "Minim";
}
if ((duration >= 3) && (duration <= 5))
{
bNoteShape = "Crotchet";
}
if ((duration >= 1) && (duration <= 2))
{
bNoteShape = "Quaver";
}
if (duration == 0)
{
bNoteShape = "SemiQuaver";
}
mn = new MusicNote(1, bNoteShape);
MessageBox.Show(bNoteShape);
this.Controls.Add(this.mn);
}
}
}
對於根據類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace NoteShape
{
class MusicNote: PictureBox
{
public string path = "ImagesName\\";
public int pitch;
public string noteShape;
public MusicNote(int iPitch, string iNoteShape) : base()
{
pitch = iPitch;
noteShape = iNoteShape;
Location = new Point(150, 50);
Size = new Size(40, 40);
Bitmap bmp1 = new Bitmap(path + noteShape + ".bmp");
Image = bmp1;
BackColor = Color.Transparent;
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
}
}
我強烈建議使用Environment.TickCount,而不是增加自己的持續時間。獲取mouseDown中的值,並在mouseUp中獲取一個新值,做出區別並持續以毫秒爲單位:請參閱此文檔:http://msdn.microsoft.com/zh-cn/library/system.environment。 tickcount.aspx –
我不能這樣做,因爲教授只想使用課堂上完成的課程,這就是爲什麼......但問題仍然存在:/ – kurtborg92
「你在課堂上完成的課程」意味着什麼。 Environnement.TickCount是.NET框架的一部分,並且可以像任何其他類一樣使用,例如Bitmap或者Point,您已經在您的項目中使用它。此外,如果您需要幫助,您應該在問題中編寫項目說明,因爲我們不知道它們。 –