- [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
- [DATABASE] Cách query cộng trừ dồn dần trong Sqlserver
- [C#] Thiết kế ứng dụng Console đẹp với thư viện Spectre.Console
- [C#] Thiết kế ứng dụng Single Instance và đưa ứng dụng lên trước nếu kiểm tra ứng dụng đang chạy
- [C#] Giới thiệu JSON Web Token và cách đọc chuỗi token
- [C#] Cách tăng giảm font chữ tất cả các control trên winform
- [DEVEXPRESS] Tích hợp chức năng Tìm kiếm Search vào CheckedComboboxEdit
- [C#] Gởi email Metting Calendar Reminder kèm nhắc thời gian lịch họp
- [C#] Tìm kiếm xem danh sách từ khóa có tồn tại trong đoạn văn bản hay không
- [C#] Thiết kế giao diện ứng dụng trên Console sử dụng thư viện Terminal.Gui
- [C#] Hướng dẫn tạo mã VietQR Payment API Winform
- [C#] Sử dụng thư viện BenchmarkDotNet đo hiệu năng của hảm Method
- [DEVEXPRESS] Tìm kiếm không dấu tô màu highlight có dấu trên C# Winform
[C#] Di chuyển và thay đổi kích thước Control Winform khi ứng dụng đang chạy
Xin chào các bạn, bài viết hôm nay mình tiếp tục hướng dẫn các bạn cách di chuyển và thay đổi kích thước của một control trực tiếp khi ứng dụng đang chạy trên C#, Winform.
[C# - VB.NET] How to Move and Resize Control Run time in Winform
Khi các bạn muốn thiết kế một ứng dụng mà mong muốn người dùng cuối có thể setup layout chủ động thì bài viết này sẻ giúp ích cho các bạn.
Như các bạn muốn hiển thị layout bàn ghế của một quán cafe chẳng hạn, và khi triển khai ứng dụng người dùng có thể tự động sắp xếp layout.
Và sau đó, chúng ta sẽ lưu trữ layout và hiển thị lại khi load file layout vào.
Dưới đây là video demo ứng dụng:
Đầu tiên, các bạn tạo cho mình một component BoxWrapper.cs
bằng C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MoveAndResizeRunTime
{
public class BoxWrapper : Component
{
#region Fields
IDesignerHost designerHost;
Control rootDesignControl;
DesignSurface surface;
Control parentControl;
Panel designPanel;
#endregion
#region Propeties
private Control _SelectedControl;
public Control SelectedControl
{
get { return _SelectedControl; }
set { IsCustomization = false; _SelectedControl = value; OnChanged(); }
}
private Control _DesignContainer;
public Control DesignContainer
{
get { return _DesignContainer; }
set { _DesignContainer = value; OnChanged(); }
}
private bool _IsCustomization;
public bool IsCustomization
{
get { return _IsCustomization; }
set
{
if (value && !CanCustomize(value)) return;
{ _IsCustomization = value; OnChanged(); }
}
}
#endregion
#region Methods
void CopyBounds(Control target, Control source)
{
Point p = source.PointToScreen(new Point(0, 0));
if (target.Parent != null)
{
p = target.Parent.PointToClient(p);
}
target.Location = p;
target.Width = source.Width;
target.Height = source.Height;
}
static bool IsNull(object obj)
{
return obj == null;
}
void AddToList(IList list, Control c)
{
if (c.Dock != DockStyle.None) return;
if (c.Parent == null) return;
if (c.Name == string.Empty) return;
list.Add(c);
}
void TraverseControls(IList list, Control c)
{
AddToList(list, c);
foreach (Control control in c.Controls) TraverseControls(list, control);
}
public List<Control> GetAvailableControls()
{
List<Control> list = new List<Control>();
TraverseControls(list, DesignContainer);
return list;
}
bool NeedDestroyCustomization()
{
return !IsNull(rootDesignControl) && !IsNull(designPanel.Parent);
}
bool CanCustomize(bool isCutomization)
{
return !(IsNull(DesignContainer) || DesignMode || IsNull(SelectedControl) || (!isCutomization) || IsNull(SelectedControl.Parent));
}
Panel CreateDesignPanel()
{
Panel designPanel = designerHost.CreateComponent(typeof(Panel)) as Panel;
designPanel.BackColor = Color.Transparent;
designPanel.Padding = new Padding(2, 2, 2, 2);
return designPanel;
}
void CreateDesignSurface()
{
surface = new DesignSurface();
designerHost = surface.GetService(typeof(IDesignerHost)) as IDesignerHost;
rootDesignControl = designerHost.CreateComponent(typeof(UserControl)) as Control;
rootDesignControl.Dock = DockStyle.Fill;
rootDesignControl.BackColor = ColorTranslator.FromHtml("#fff7e6");//Color.AliceBlue;
Control c = surface.View as Control;
c.Dock = DockStyle.Fill;
c.BackColor = Color.White;
c.Location = new Point(15, 25);
c.Parent = DesignContainer;
}
void StartControlCustomization()
{
designPanel = CreateDesignPanel();
rootDesignControl.Controls.Add(designPanel);
CopyBounds(designPanel, SelectedControl);
parentControl = SelectedControl.Parent;
SelectedControl.Parent = designPanel;
SelectedControl.Dock = DockStyle.Fill;
}
void StartCustomiztion()
{
if (!CanCustomize(IsCustomization)) return;
CreateDesignSurface();
StartControlCustomization();
}
void FinishControlCustomization()
{
SelectedControl.Parent = parentControl;
SelectedControl.Dock = DockStyle.None;
CopyBounds(SelectedControl, designPanel);
}
void EndCustomization()
{
if (!NeedDestroyCustomization()) return;
FinishControlCustomization();
DestroyDesignSurface();
}
void DestroyDesignSurface()
{
rootDesignControl.Dispose();
}
void OnChanged()
{
if (IsCustomization) StartCustomiztion();
else EndCustomization();
}
#endregion
}
}
Class BoxWrapper.vb
bằng VB.NET
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel.Design;
using System.Collections;
namespace WindowsApplication1
{
public class Customizator : Component
{
#region Fields
IDesignerHost designerHost;
Control rootDesignControl;
DesignSurface surface;
Control parentControl;
Panel designPanel;
#endregion
#region Propeties
private Control _SelectedControl;
public Control SelectedControl
{
get { return _SelectedControl; }
set { IsCustomization = false; _SelectedControl = value; OnChanged(); }
}
private Control _DesignContainer;
public Control DesignContainer
{
get { return _DesignContainer; }
set { _DesignContainer = value; OnChanged(); }
}
private bool _IsCustomization;
public bool IsCustomization
{
get { return _IsCustomization; }
set
{
if (value && !CanCustomize(value)) return;
{ _IsCustomization = value; OnChanged(); }
}
}
#endregion
#region Methods
void CopyBounds(Control target, Control source)
{
Point p = source.PointToScreen(new Point(0, 0));
if (target.Parent != null)
{
p = target.Parent.PointToClient(p);
}
target.Location = p;
target.Width = source.Width;
target.Height = source.Height;
}
static bool IsNull(object obj)
{
return obj == null;
}
void AddToList(IList list, Control c)
{
if (c.Dock != DockStyle.None) return;
if (c.Parent == null) return;
if (c.Name == string.Empty) return;
list.Add(c);
}
void TraverseControls(IList list, Control c)
{
AddToList(list, c);
foreach (Control control in c.Controls) TraverseControls(list, control);
}
public List<Control> GetAvailableControls()
{
List<Control> list = new List<Control>();
TraverseControls(list, DesignContainer);
return list;
}
bool NeedDestroyCustomization()
{
return !IsNull(rootDesignControl) && !IsNull(designPanel.Parent);
}
bool CanCustomize(bool isCutomization)
{
return !(IsNull(DesignContainer) || DesignMode || IsNull(SelectedControl) || (!isCutomization) || IsNull(SelectedControl.Parent));
}
Panel CreateDesignPanel()
{
Panel designPanel = designerHost.CreateComponent(typeof(Panel)) as Panel;
designPanel.BackColor =Color.Transparent;
designPanel.Padding = new Padding(2, 2, 2, 2);
return designPanel;
}
void CreateDesignSurface()
{
surface = new DesignSurface();
designerHost = surface.GetService(typeof(IDesignerHost)) as IDesignerHost;
rootDesignControl = designerHost.CreateComponent(typeof(UserControl)) as Control;
rootDesignControl.Dock = DockStyle.Fill;
rootDesignControl.BackColor = Color.AliceBlue;
Control c = surface.View as Control;
c.Dock = DockStyle.Fill;
c.BackColor = Color.White;
c.Location = new Point(15, 25);
c.Parent = DesignContainer;
}
void StartControlCustomization()
{
designPanel = CreateDesignPanel();
rootDesignControl.Controls.Add(designPanel);
CopyBounds(designPanel, SelectedControl);
parentControl = SelectedControl.Parent;
SelectedControl.Parent = designPanel;
SelectedControl.Dock = DockStyle.Fill;
}
void StartCustomiztion()
{
if (!CanCustomize(IsCustomization)) return;
CreateDesignSurface();
StartControlCustomization();
}
void FinishControlCustomization()
{
SelectedControl.Parent = parentControl;
SelectedControl.Dock = DockStyle.None;
CopyBounds(SelectedControl, designPanel);
}
void EndCustomization()
{
if (!NeedDestroyCustomization()) return;
FinishControlCustomization();
DestroyDesignSurface();
}
void DestroyDesignSurface()
{
rootDesignControl.Dispose();
}
void OnChanged()
{
if (IsCustomization) StartCustomiztion();
else EndCustomization();
}
#endregion
}
}
Và cách sử dụng chúng ta chỉ cần SelectedControl Target vào control nào muốn thay đổi và set IsCustomization = true
là ok
Code C# trên Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MoveAndResizeRunTime
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnEnable_Click(object sender, EventArgs e)
{
boxWrapper1.SelectedControl = dataGridView1;
boxWrapper1.IsCustomization = true;
}
private void btnDisable_Click(object sender, EventArgs e)
{
boxWrapper1.IsCustomization = false;
}
}
}
Thanks for watching!