Создание простой формы и кнопки - C#
Формулировка задачи:
Не получается создать кнопку, не пойму как это можно сделать.. Помогите пожалуйста
главный класс класс Cell
using System;
using System.Windows.Forms;
namespace Project3
{
class MainForm : Form
{
public MainForm()
{
this.Width = 500;
this.Height = 500;
this.Text = "Пятнашки";
}
static int Main(string[] args)
{
Application.Run(new MainForm());
Cell cell1 = new Cell(new Vector2(50, 50), 1);
return 0;
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Project3
{
class Cell
{
private const int cellSize = 30;
private int number;
Vector2 location;
public Vector2 Location
{
get { return location; }
set { location = value; }
}
public int Number
{
get { return number; }
set { number = value; }
}
public int CellSize
{
get { return cellSize; }
}
public Cell(Vector2 location, int number)
{
Button btn = new Button();
btn.Location = new System.Drawing.Point(location.X,location.Y);
btn.Name = number.ToString();
btn.Size = new System.Drawing.Size(CellSize, CellSize);
btn.Text = number.ToString();
btn.Enabled = true;
btn.Controls.Add(btn);
}
}
}Решение задачи: «Создание простой формы и кнопки»
textual
Листинг программы
public partial class Form1 : Form
{
Point move;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CreateButton(this);
}
private void CreateButton(Form Owner)
{
Button btn = new Button();
btn.MouseDown += Button_MouseDown;
btn.MouseMove += Button_MouseMove;
Owner.Controls.Add(btn);
}
private void Button_MouseDown(object sender, MouseEventArgs e)
{
move = e.Location;
}
private void Button_MouseMove(object sender, MouseEventArgs e)
{
Button button = (Button)sender;
if (e.Button == MouseButtons.Left)
{
button.Left += e.X - move.X;
button.Top += e.Y - move.Y;
}
}
}