views:

85

answers:

2

I have a few multiline textboxes in a TabControl. The tabcontrol has a scrollbar, but as soon as a multiline textbox is focused scrolling will scroll the textbox instead of the tabcontrol. Is there any way to stop the textbox from taking the event and act more like a normal textbox, but still multiline?

A: 

Set the TextBox.ScrollBars property to Vertical. That gives the text box a scrollbar that scrolls the text. Make sure the TextBox fit the TabPage so you don't get a scrollbar in the tab page. You could set the TextBox.Dock property to Fill for example.


That wasn't it I guess, maybe you are talking about the mouse wheel. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing your existing text boxes.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class MyTextBox : TextBox {
    protected override void WndProc(ref Message m) {
        // Send WM_MOUSEWHEEL messages to the parent
        if (m.Msg == 0x20a) SendMessage(this.Parent.Handle, m.Msg, m.WParam, m.LParam);
        else base.WndProc(ref m);
    }
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}
Hans Passant
Maybe I wasn't clear enough. The tabcontrol has a scrollbar and it should be that way. The textbox doesn't have one, and it doesn't matter if it has one or not. When the (multiline) textbox is focused any mouse scrolling will affect scrolling in that textbox rather than the tabcontrol. I want that scrolling to happen in the tabcontrol even though the textbox is focused. Basically bind the textbox scroll event to scroll the tabcontrol instead, but I couldn't find anything that resembled a scroll event in the textbox.
Daniel
Hmm, no kidding. To be sure: you are talking about the mouse wheel?
Hans Passant
Sorry for the bad problem definition. Yes, I was talking about the mouse wheel. Also I was talking about winforms (the existence of WPF, ASP and similar slipped my mind when posting)).Thanks, it worked! Getting VS 2008 to load it into the toolbox didn't work quite as well though, so I added it programmatically.
Daniel
A: 

(Edit: assuming WPF here: the other method would work with WinForms)

You could add a PreviewMouseWheel handler on the TextBox, then set e.Handled = true; - in theory anyway!

http://msdn.microsoft.com/en-us/library/system.windows.uielement.previewmousewheel.aspx

Hope that helps!

Kieren Johnstone