Prevent tab page from changing

K

kosjanne

I need to prevent user from changing a tab page, if the current tab
page has incorrect data. I've been searching these forums for a
solution and have found a suggestion that goes something like this:

[StructLayout(LayoutKind.Sequential)]
public struct NMHDR
{
public int hwndFrom;
public int idFrom;
public int code;
}

protected override void WndProc( ref Message m)
{
if ( m.Msg == 0x4e ) //WM_NOTIFY
{
NMHDR hdr = (NMHDR)Marshal.PtrToStructure(m.LParam, typeof(NMHDR));
if (hdr.code == -552)
{
...
m.Result = new IntPtr(1);
}
}
base.WndProc(ref m);
}

However, this doesn't work for me. The tab page is still changed even
though the m.Result is 1. Any ideas how to get this working? Is my
WndProc in correct place, when it is in the MainForm (which owns the
tab control)? Should it be somewhere else?

Thank you.
 
M

Mick Doherty

Yes, it's in the wrong place. It should be in the WndProc of an Inherited
TabControl.

Note that this will only prevent selection via mouse though. A user can
still change selection by using the Arrow keys as well as Ctrl+Tab and
Ctrl+Shift+Tab key combinations.

See the example on my site for a complete solution:
http://www.dotnetrix.co.uk/tabcontrols.html --> Add SelectedIndexChanging
Event.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top