- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
- [C#] Hướng dẫn tạo ứng dụng dock windows giống Taskbar
- [C#] Chia sẻ source code sử dụng Object Listview trên Winform
- [VB.NET] Chia sẻ source code quản lý thu chi mô hình 3 lớp Winform
- [DATABASE] Xóa lịch sử danh sách đăng nhập tài khoản trên SMSS Sqlserver Management Studio
- [C#] Sử dụng FolderBrowserDialog Vista trên Winform
- [DEVEXPRESS] Chia sẻ tool Winform UI Templates Early Access Preview (EAP)
- [C#] Chia sẻ source code Spin Content (Trộn nội dung văn bản theo từ đồng nghĩa) trên Winform
- [VB.NET] Chia sẻ source code lịch âm dương và hẹn lịch nhắc việc
- [C#] Hướng dẫn đọc thông số thiết bị Thiết bị kiểm tra Pin (HIOKI BATTERY HiTESTER BT3562)
- [VB.NET] Hướng dẫn giải captcha sử dụng dịch vụ AZCaptcha API trên winform
- [C#] Hướng dẫn chứng thực đăng nhập ứng dụng bằng vân tay (Finger Print) trên máy tính
- [C#] Color Thief cách xuất màu sắc thiết kế từ hình ảnh
- [C#] Cách tạo bản quyền và cho phép dùng thử ứng dụng Winform
- [C#] Hướng dẫn sử dụng trình duyệt web Chrome convert HTML sang tập tin file PDF
- [C#] Kết nôi điện thoại Android, IOS với App Winform via Bluetooth
[C#] Di chuyển control Runtime và lưu layout trên winform
Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách di chuyển vị trí Location của các control runtime trên C# winform.
[C#] Mouse Move Control Runtime Winform
Khi các bạn thiết kế giao diện ứng dụng lên, mình sẽ cho phép người dùng di chuyển vị trí từng control theo mong muốn của người dùng.
Và lưu lại vị trí cấu hình của từng control trên Winform thành một file layout .json, và 1 file base layout dùng để Reset cấu hình vị trí của từng control trên winform c#.
Giao diện demo ứng dụng di chuyển control trực tiếp đang chạy C# Winform:
Cách thực hiện chúng ta sẽ quét tất cả các control trên Winform, và thêm vào cho chúng sự kiện event MouseMove, để di chuyển các control Winform.
Full source code Move Control Runtime C#:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MoveControlRunTime
{
public partial class Form1 : Form
{
List<Control> m_MovebleControls;
List<LayoutControl> layoutControls;
public Form1()
{
InitializeComponent();
}
public void SaveBaseLayout()
{
var layoutControls = new List<LayoutControl>();
foreach (Control control in this.Controls)
{
var layoutControl = new LayoutControl();
layoutControl.Name = control.Name;
layoutControl.Location = control.Location;
layoutControls.Add(layoutControl);
}
var jData = JsonConvert.SerializeObject(layoutControls, Formatting.Indented);
File.WriteAllText("reset.json", jData);
}
private void Form1_Load(object sender, EventArgs e)
{
SaveBaseLayout();
m_MovebleControls = new List<Control>();
foreach (Control control in this.Controls)
{
m_MovebleControls.Add(control);
}
foreach (Control control in m_MovebleControls)
{
control.MouseMove += new MouseEventHandler(Control_MouseMove);
}
if (File.Exists("layout.json"))
{
var listControlLayout = JsonConvert.DeserializeObject<List<LayoutControl>>(File.ReadAllText("layout.json"));
foreach (Control control in this.Controls)
{
LayoutControl item = (LayoutControl)listControlLayout.Where(x => x.Name == control.Name).FirstOrDefault();
control.Location = item.Location;
}
}
}
private void Control_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Control controlToMove = (Control)sender;
controlToMove.BringToFront();
controlToMove.Location = new Point(controlToMove.Location.X + e.Location.X - 10,
controlToMove.Location.Y + e.Location.Y - 10);
}
}
private void btnSavelayout_Click(object sender, EventArgs e)
{
layoutControls = new List<LayoutControl>();
foreach (Control control in this.Controls)
{
var layoutControl = new LayoutControl();
layoutControl.Name = control.Name;
layoutControl.Location = control.Location;
layoutControls.Add(layoutControl);
}
var jData = JsonConvert.SerializeObject(layoutControls, Formatting.Indented);
File.WriteAllText("layout.json", jData);
MessageBox.Show("Save Layout Success!");
}
private void button2_Click(object sender, EventArgs e)
{
if (File.Exists("reset.json"))
{
var listControlLayout = JsonConvert.DeserializeObject<List<LayoutControl>>(File.ReadAllText("reset.json"));
foreach (Control control in this.Controls)
{
LayoutControl item = (LayoutControl)listControlLayout.Where(x => x.Name == control.Name).FirstOrDefault();
control.Location = item.Location;
}
}
}
}
public class LayoutControl
{
public string Name { set; get; }
public Point Location { set; get; }
}
}
Thanks for watching!