- [C#] Di chuyển và thay đổi kích thước Control Winform khi ứng dụng đang chạy
- [VB.NET] Chia sẻ source tạo sắp xếp đội hình bóng đá Line-ups đội bóng
- [C#] Hướng dẫn chỉnh sửa Text của label trực tiếp trên winform
- [C#] Hướng dẫn custom TextBox giống Ultraviewer trên Winform
- [C#] Show Modal Winform like Bootstrap
- [DATABASE] Thứ tự thực hiện mệnh đề truy vấn SELECT trong Sqlserver
- [C#] Hướng dẫn viết addin Excel Lấy hình ảnh từ URL internet vào Excel
- [DATABASE] TSQL view max length all column data trên table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng MailMerge kèm Hình ảnh trên Winform
- [DATABASE] Hướng dẫn truy vấn xem kích thước lưu trữ của từng bảng ghi Table trên sqlserver
- [C#] Hướng dẫn Fake Date Time sử dụng thư viện Harmony
- [DATABASE] Phân biệt câu lệnh DDL và DML trong sqlserver
- [C#] Hướng dẫn convert file mã HTML sang file Pdf trên winform
- [DEVEXPRESS] Tạo các loại mã vạch Barcode trực tiếp trên Devexpress Barcode API
- [DEVEXPRESS] Hướng dẫn custom Simple button thành Progressbar
- [DATABASE] Tách số và chữ từ chuỗi - hàm tối ưu hóa tách số và chữ trong Sqlserver
- [C#] Tìm kiếm gần đúng Full Text Search sử dụng thư viện Lucene.NET
- [C#] Chia sẻ tài liệu, sdk và source code máy chấm công dòng máy ZKTeco
- [C#] Memory Cache là gì, và sử dụng trong ứng dụng Winform
- [DATABASE] Khóa chính Primary Key trong Sqlserver
[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!