Создание простой формы и кнопки - C#

Узнай цену своей работы

Формулировка задачи:

Не получается создать кнопку, не пойму как это можно сделать.. Помогите пожалуйста главный класс
Листинг программы
  1. using System;
  2. using System.Windows.Forms;
  3. namespace Project3
  4. {
  5. class MainForm : Form
  6. {
  7. public MainForm()
  8. {
  9. this.Width = 500;
  10. this.Height = 500;
  11. this.Text = "Пятнашки";
  12. }
  13. static int Main(string[] args)
  14. {
  15. Application.Run(new MainForm());
  16. Cell cell1 = new Cell(new Vector2(50, 50), 1);
  17. return 0;
  18. }
  19. }
  20. }
класс Cell
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. namespace Project3
  7. {
  8. class Cell
  9. {
  10. private const int cellSize = 30;
  11. private int number;
  12. Vector2 location;
  13. public Vector2 Location
  14. {
  15. get { return location; }
  16. set { location = value; }
  17. }
  18. public int Number
  19. {
  20. get { return number; }
  21. set { number = value; }
  22. }
  23. public int CellSize
  24. {
  25. get { return cellSize; }
  26. }
  27. public Cell(Vector2 location, int number)
  28. {
  29. Button btn = new Button();
  30. btn.Location = new System.Drawing.Point(location.X,location.Y);
  31. btn.Name = number.ToString();
  32. btn.Size = new System.Drawing.Size(CellSize, CellSize);
  33. btn.Text = number.ToString();
  34. btn.Enabled = true;
  35. btn.Controls.Add(btn);
  36. }
  37. }
  38. }

Решение задачи: «Создание простой формы и кнопки»

textual
Листинг программы
  1.     public partial class Form1 : Form
  2.     {
  3.         Point move;
  4.  
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         private void Form1_Load(object sender, EventArgs e)
  11.         {
  12.             CreateButton(this);
  13.         }
  14.  
  15.         private void CreateButton(Form Owner)
  16.         {
  17.             Button btn = new Button();
  18.             btn.MouseDown += Button_MouseDown;
  19.             btn.MouseMove += Button_MouseMove;
  20.             Owner.Controls.Add(btn);
  21.         }
  22.  
  23.         private void Button_MouseDown(object sender, MouseEventArgs e)
  24.         {
  25.             move = e.Location;
  26.         }
  27.  
  28.         private void Button_MouseMove(object sender, MouseEventArgs e)
  29.         {
  30.             Button button = (Button)sender;
  31.             if (e.Button == MouseButtons.Left)
  32.             {
  33.                 button.Left += e.X - move.X;
  34.                 button.Top += e.Y - move.Y;
  35.             }
  36.         }
  37.     }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

12   голосов , оценка 4.167 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы