C#: How to write a text on sticky note.
2015/12/29
※これは前回ブログ「付箋紙に文字を書き込む」をご要望により英語に起こしなおしたものです。
– This is rewrote previous blog in English.
The following is a programming points.
- Textbox is located over the Label
- Textbox is usually hidden (Visible=false).
- Textbox appears when Label is double-clicked.
- Label hides when Textbox is double-clicked.
- Texts is linked between Textbox and Label.
using System.Drawing;
using System.Windows.Forms;
class StickyNote{
static void Main(){
Application.Run(new MainWindow());
}
}
class MainWindow : Form{
private Label label;
private TextBox text;
private Point mousePoint; //Clicked position of the mouse
public MainWindow(){
this.Width = 200;
this.Height = 100;
this.FormBorderStyle = FormBorderStyle.None;
this.ShowInTaskbar = false;
//TextBox is located over the form.
text = new TextBox(){
Dock = DockStyle.Fill,
BackColor = Color.LightPink,
BorderStyle = BorderStyle.FixedSingle,
Multiline = true,
Visible = false,
Parent=this,
};
text.DoubleClick += new EventHandler(DoubleClicked);
//Label is located over the form.
label = new Label(){
Dock = DockStyle.Fill,
BackColor = Color.LightPink,
BorderStyle = BorderStyle.FixedSingle,
Parent=this,
};
label.DoubleClick += new EventHandler(DoubleClicked);
label.MouseDown += new MouseEventHandler(MouseDowned);
label.MouseMove += new MouseEventHandler(MouseMoved);
}
private void DoubleClicked(object sender, EventArgs e){
//When Textbox is double-clicked.
if(sender==text){
text.Visible = false;
this.FormBorderStyle = FormBorderStyle.None;
label.Text = text.Text;
}
//When Label is double-clicked.
if(sender==label){
text.Visible = true;
this.FormBorderStyle = FormBorderStyle.Sizable;
}
}
//When mouse is clicked.
private void MouseDowned(object sender, MouseEventArgs e){
if ((e.Button & MouseButtons.Left) == MouseButtons.Left){
//Memoried the position
mousePoint = new Point(e.X, e.Y);
}
}
//When mouse is dragged and moved.
private void MouseMoved(object sender, MouseEventArgs e){
if ((e.Button & MouseButtons.Left) == MouseButtons.Left){
this.Location = new Point(
this.Location.X + e.X – mousePoint.X,
this.Location.Y + e.Y – mousePoint.Y);
}
}
}
スポンサーリンク