[C#] Tự động show Popup các control như: Combobox, DateTimePicker
Ví dụ: Khi các bạn thiết kế form nhập thông tin.Nếu người dùng chưa chọn dữ liệu, thì mình sẽ thông báo lỗi kèm show luôn popup lên cho người dùng bắt buộc phải chọn dữ liệu.
Giao diện demo ứng dụng Auto show popup C#:
FULL CODE CSHARP:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AutoShowPopupCombobox
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
private static extern bool PostMessage(
IntPtr hWnd, // handle to destination window
Int32 msg, // message
Int32 wParam, // first message parameter
Int32 lParam // second message parameter
);
const Int32 WM_LBUTTONDOWN = 0x0201;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ShowPopupControl(comboBox1);
}
public void ShowPopupControl(Control control) {
var x = control.Width - 10;
var y = control.Height / 2;
var lParam = x + y * 0x00010000;
PostMessage(control.Handle, WM_LBUTTONDOWN, 1, lParam);
}
private void button2_Click(object sender, EventArgs e)
{
ShowPopupControl(dateTimePicker1);
}
private void btnclick_Click(object sender, EventArgs e)
{
ShowPopupControl(button4);
}
private void button4_Click(object sender, EventArgs e)
{
MessageBox.Show("Button 4 clicked!");
}
}
}
Chúc các bạn thành công với thủ thuật trên.