How can I Write an Event that Happens when Mouse Scrolls in TextBox?
Asked Answered
N

4

0

I wanna change the number of the TextBox when The Mouse Scrolls.
I have a Scroll TextBox But I Don't wanna use that. Is There any Event related to this? Should I write an TextBox Event? If yes, How can I write a textBox event that Happens when Mouse Scrolls?

Neuropathy answered 31/10, 2014 at 9:53 Comment(4)
Are you using winform??Cense
Yes, I'm using Windows Form ApplicationNeuropathy
https://mcmap.net/q/935361/-catch-textbox-scroll-eventGravid
Actually I didn't Understand The code and I don't want to use API Functions. Is there Any Better Way?Neuropathy
N
2

The MouseWheel event is there alright:

public Form1()
{
    InitializeComponent();
    textBox1.MouseWheel += textBox1_MouseWheel;
}

void textBox1_MouseWheel(object sender, MouseEventArgs e)
{
    throw new NotImplementedException();
}

But it is not visible in the event editor. No idea why..

You can find the full list of events through Intellisense like this:

Enter the Control's name and a dot. Now watch the dropdownlist for the event you need. When you have it write += add press Tab twice. This hooks up the event and creates a stub for it.

Nystagmus answered 31/10, 2014 at 10:10 Comment(4)
Are you sure that the event arguments are correct, I never used this event but in the documentation they are using MouseWheelEventArgs.Areola
Well they are as created by VS. The MouseWheelEventArgs are not from Winforms but from WPF. They contain a Delta alright, which has at least a sign that can be used, even if the value is always the same for my mice..Nystagmus
@Nystagmus my bad sorry about that :)Areola
I know this is an old question but inside the "textBox1_MouseWheel()" method, you are looking for e.delta to be positive (scroll wheel went up) or negative (scroll wheel when down).Goalkeeper
G
1

Use ScrollViewer.ScrollChanged event in xaml. Here is an example to implement auto scroll log textbox. Scroll up stops the auto scroll action and scroll down resume action.

XAML:

<TextBox x:Name="LogTextBox" Background="Black" Foreground="LightGray" TextChanged="OnTextChanged"
    IsReadOnly="True" IsReadOnlyCaretVisible="True"
    VerticalScrollBarVisibility="Auto" ScrollViewer.ScrollChanged="OnScroll">
</TextBox>

C#:

public partial class LogTextBlock : UserControl
{
    public LogTextBlock() => InitializeComponent();

    bool m_autoScroll = true;

    void OnTextChanged(object sender, TextChangedEventArgs e)
    {
        if (m_autoScroll) LogTextBox.ScrollToEnd();
    }

    void OnScroll(object sender, ScrollChangedEventArgs e)
    {
        var verticalChange = e.VerticalChange;

        if (verticalChange > 0)
            m_autoScroll = true;
        else if (verticalChange < 0)
            m_autoScroll = false;
    }

}
Grangerize answered 22/3 at 2:41 Comment(0)
A
0

You need MouseWheel event. Check the documentation.

Areola answered 31/10, 2014 at 9:58 Comment(3)
TextBox Doesn't have MouseWheel Event. Could you Explain More?Neuropathy
@AmirPourmand msdn.microsoft.com/en-us/library/…. MSDN says that they have.Areola
.Yes They Say but I TextBox Events. I can't see It. It has Been disappeared. :DNeuropathy
P
-1

VS studio is not Intellisense becuase some properties and methods are

[Browsable(false)] [EditorBrowsable(EditorBrowsableState.Advanced)]

Predicament answered 2/7, 2019 at 17:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.