I was looking to prevent the user from changing tabs and found some great code at www.dotnetrix.com.
Attached is my code based on that.
public partial class MainForm : Form
{
[StructLayout(LayoutKind.Sequential)]
private struct NMHDR
{
public IntPtr HWND;
public uint idFrom;
public int code;
public override String ToString()
{
return String.Format("Hwnd: {0}, ControlID: {1}, Code: {2}", HWND, idFrom, code);
}
}
private const int TCN_FIRST = 0 - 550;
private const int TCN_SELCHANGING = (TCN_FIRST - 2);
private const int WM_NOTIFY = 0x4E;//Windows message
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void TabPage1Click(object sender, EventArgs e)
{
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NOTIFY:
try
{
NMHDR hdr = (NMHDR)(Marshal.PtrToStructure(m.LParam, typeof(NMHDR)));
switch (hdr.code)
{
// User is attempting to change the tab page
case TCN_SELCHANGING:
//ADD function here
//Set the result to -1 if validation fails
// and return out by the following code
m.Result = (System.IntPtr)(-1);
return;
break;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
break;
}
base.WndProc(ref m);
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment