Click here to Skip to main content
15,922,696 members
Home / Discussions / C#
   

C#

 
QuestionA bug in VS 2003??? Pin
LongRange.Shooter17-Feb-04 9:43
LongRange.Shooter17-Feb-04 9:43 
AnswerRe: A bug in VS 2003??? Pin
Heath Stewart17-Feb-04 12:42
protectorHeath Stewart17-Feb-04 12:42 
GeneralRe: A bug in VS 2003??? Pin
LongRange.Shooter18-Feb-04 2:38
LongRange.Shooter18-Feb-04 2:38 
GeneralRe: A bug in VS 2003??? Pin
Heath Stewart18-Feb-04 4:16
protectorHeath Stewart18-Feb-04 4:16 
Generalchanging CPropertySheet's bk color Pin
clayman8717-Feb-04 9:14
clayman8717-Feb-04 9:14 
GeneralRe: changing CPropertySheet's bk color Pin
Heath Stewart17-Feb-04 12:33
protectorHeath Stewart17-Feb-04 12:33 
GeneralReading a JPEG file into String Pin
AliIMCIn17-Feb-04 6:40
AliIMCIn17-Feb-04 6:40 
GeneralRe: Reading a JPEG file into String Pin
Werdna17-Feb-04 7:20
Werdna17-Feb-04 7:20 
GeneralRe: Reading a JPEG file into String Pin
Werdna17-Feb-04 11:30
Werdna17-Feb-04 11:30 
GeneralRe: Reading a JPEG file into String Pin
Heath Stewart17-Feb-04 12:32
protectorHeath Stewart17-Feb-04 12:32 
GeneralRe: Reading a JPEG file into String Pin
Mazdak17-Feb-04 10:09
Mazdak17-Feb-04 10:09 
GeneralRe: Reading a JPEG file into String Pin
Heath Stewart17-Feb-04 12:26
protectorHeath Stewart17-Feb-04 12:26 
GeneralRe: Reading a JPEG file into String Pin
Mazdak17-Feb-04 18:13
Mazdak17-Feb-04 18:13 
Generalproblem with Textboxes Pin
raheela17-Feb-04 6:22
raheela17-Feb-04 6:22 
GeneralRe: problem with Textboxes Pin
Heath Stewart17-Feb-04 12:23
protectorHeath Stewart17-Feb-04 12:23 
Generalwriting a struct to a binary file Pin
DennisMetz17-Feb-04 4:19
DennisMetz17-Feb-04 4:19 
GeneralRe: writing a struct to a binary file Pin
Heath Stewart17-Feb-04 4:44
protectorHeath Stewart17-Feb-04 4:44 
GeneralRe: writing a struct to a binary file Pin
DennisMetz17-Feb-04 5:06
DennisMetz17-Feb-04 5:06 
GeneralRe: writing a struct to a binary file Pin
Heath Stewart17-Feb-04 6:19
protectorHeath Stewart17-Feb-04 6:19 
Generalcatch tabpage change Pin
troels_sorensen17-Feb-04 3:41
troels_sorensen17-Feb-04 3:41 
GeneralRe: catch tabpage change Pin
Heath Stewart17-Feb-04 4:07
protectorHeath Stewart17-Feb-04 4:07 
GeneralRe: catch tabpage change Pin
Anonymous17-Feb-04 8:44
Anonymous17-Feb-04 8:44 
GeneralRe: catch tabpage change Pin
Heath Stewart17-Feb-04 9:21
protectorHeath Stewart17-Feb-04 9:21 
GeneralRe: catch tabpage change Pin
Anonymous17-Feb-04 10:56
Anonymous17-Feb-04 10:56 
GeneralRe: catch tabpage change Pin
Heath Stewart17-Feb-04 12:14
protectorHeath Stewart17-Feb-04 12:14 
It wouldn't be pointless to define a uint because you can always cast it to an int upon comparing, but I wouldn't recommend it since this method should be fast (it has to process potentially a lot of messages, after all) and the extra instruction to cast (and possibly two more to store and retrieve depending on the code) is too expensive.

Fortunately, I re-read the documentation for TCN_SELCHANGING and noticed that this message is sent in the form of a WM_NOTIFY message to the parent of the TabControl. This little example works:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class Test : Form
{
  public static void Main()
  {
    Application.Run(new Test());
  }
  public Test()
  {
    TabControl tab = new TabControl();
    
    this.SuspendLayout();
    this.Text = "Test";
    this.Controls.Add(tab);
	tab.TabPages.Add(new TabPage("Page 1"));
	tab.TabPages.Add(new TabPage("Page 2"));
    tab.Dock = DockStyle.Fill;
    this.ResumeLayout();
  }
  private const int WM_NOTIFY = 0x004e;
  private const int TCN_SELCHANGING = -552;
  private struct NMHDR
  {
    public IntPtr hwndFrom;
    public int idFrom;
    public int code;
  }
  protected override void WndProc(ref Message m)
  {
    base.WndProc(ref m);
    if (m.Msg == WM_NOTIFY)
    {
      NMHDR hdr = (NMHDR)Marshal.PtrToStructure(m.LParam, typeof(NMHDR));
      Console.WriteLine(hdr.code);
      if (hdr.code == TCN_SELCHANGING)
        m.Result = new IntPtr(1);
    }
  }
}
Sorry for the mess. Most messages I deal with are sent directly and aren't WM_NOTIFY style messages. This gets a little quirky. You'll notice I marshal the LPARAM parameter to an NMHDR struct, which I then check the code. Getting to know the notification messages helps, I just feel dumb that I didn't fully read it before.

Also note that I'm calling base.WndProc first. If you don't, Windows is likely to change the Message.Result field to 0 (or reset it to 0). You could also just not call it and return in this case if you get the WM_NOTIFY message with the NMHDR.code set to TCN_SELCHANGING.

 

Microsoft MVP, Visual C#
My Articles

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.