我正在编写一个程序,以便单击一个按钮,并根据您将鼠标按下的时间打印链接的便笺。我的问题是,第一次点击工作正常,但当我第二次坚持它不会更新,这让我发疯。任何帮助都感激不尽。下面找到我正在使用的以下代码。感谢
对于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);
}
}
}
答案 0 :(得分:0)
您正在创建一个新笔记,但每次都将它放在同一位置。
您应该编辑现有/点击的音符或定位新音符。
答案 1 :(得分:0)
我刚试过你的代码&#34;按原样#34;在Visual Studio 2010中没有任何问题。我唯一改变的是我对位图进行了评论,因为我没有这些位图,但我选择背景颜色黑色来看它。
//Bitmap bmp1 = new Bitmap(path + noteShape + ".bmp");
//Image = bmp1;
BackColor = Color.Black;
另外,我把timer1的间隔时间设为500毫秒,因为我不知道你的间隔是多少。
我可以一遍又一遍地做,消息框中弹出正确的内容! 您可以再试一次,也可以更新您的代码,因为现在没有任何问题。
修改
您需要在button1_MouseUp
中添加此内容this.Controls.Add(mn);
mn.BringToFront();
图片框是在前一张图片后面绘制的。