CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2005
    Location
    Pune,India
    Posts
    10

    Question How to check if Up/Down Button is pressed on NumericUpDown Control...

    Hi Friends,

    I would like to know that how to check whether up/down button was clicked on the NumericUpDown Control in .NET 2005.

    Any help would be greatly appreciated.

    With warm regards,

    KEDAR

  2. #2
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: How to check if Up/Down Button is pressed on NumericUpDown Control...

    check this event NumericUpDown1_ValueChanged

  3. #3
    Join Date
    Sep 2005
    Location
    Pune,India
    Posts
    10

    Re: How to check if Up/Down Button is pressed on NumericUpDown Control...

    Thank you for your interest Mr. Anis Khan...

    But the ValueChanged event occurs irrespective of whether you click up button or down button.

    What i wanted to know is that if there is any way to determine specifically which button has been clicked.

    Actually i was expecting that there should have been some value in Event Arguments of the event ValueChanged indicating up/down button pressed.

    With warm regards,
    KEDAR

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: How to check if Up/Down Button is pressed on NumericUpDown Control...

    In my opinion, the ValueChanged is the only event you could use for this!

    There are methods
    NumericUpDown.UpButton
    NumericUpDown.DownButton

    but they cannot be used to determine which was clicked, only to "click" them through code - understand¿

    What you could perhaps do, is to have a variable indicating if the value of the numericupdown increases or decreases, because if the value increas, then automatically the up button was pressed, same with decrease.
    Based on that, you can do what you want to when someone clicks up or down

    I Hope I have been helpful.

  5. #5
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: How to check if Up/Down Button is pressed on NumericUpDown Control...

    You cannot determine that because you shouldn't really care. What matters in a NUD is the Value, so you should only care when that changes. If you want to know whether the Value has increased or decreased then store the old value in a variable and compare the two in the event handler. Note that the ValueChanged event is raised when the user manually types a value into the control and then moves focus, so it isn't just linked to the buttons.
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  6. #6
    Join Date
    Sep 2005
    Location
    Pune,India
    Posts
    10

    Smile Re: How to check if Up/Down Button is pressed on NumericUpDown Control...

    Hi Friends,

    I should thank you for all those valueable inputs.

    The reason i want to know which button is clicked is that i need to change the value of the interval in the pre-determined ranges like:-
    Value will increase by 1 in 1-20 and by 2 in 20-40 upto 60 and again when one clicks down button value should decrease by the same interval by which it has been increased in that range.

    I am thinking of using a variable to keep track of the previous value or simply abandon the idea of using the numeric control and use simple textbox and 2 buttons.

    With warm regards,
    KEDAR

  7. #7
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: How to check if Up/Down Button is pressed on NumericUpDown Control...

    Are you doing this so that the user cannot enter values like 21, 23, etc. or are you just trying to make the NUD increment faster at higher values? If it's the former then you're out of luck as the user can type any number they want directly into the NUD anyway. If you absolutely want to restrict the value then you'd need to use a DomainUpDown instead and use a loop to populate it in the first place. If it's the latter you can manipulate the Increment property so that pressing a button changes the value by whatever amount you want. You could set the Increment to 1 below 20, change it to 2 above 20 and then to 3 above 60 or whatever. The problem comes at the boundaries. When the Value is 20 do you make the Increment 1 or 2, because you don't whether the next change will be up or down? It probably doesn't matter because the user can just hold down a button and the value will change faster and faster anyway. I could be wrong but it seems like you're trying to solve a problem that doesn't exist, or else you're trying to solve the wrong problem.
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  8. #8
    Join Date
    Sep 2005
    Location
    Pune,India
    Posts
    10

    Thumbs up Re: How to check if Up/Down Button is pressed on NumericUpDown Control...

    Thanks all of you. jmcilhinney,HanneSThEGreaT,aniskhan.

    With warm regards,
    KEDAR

  9. #9
    Join Date
    Jun 2013
    Posts
    1

    Re: How to check if Up/Down Button is pressed on NumericUpDown Control...

    Not sure if you've found a satisfying solution but try this:

    if (Convert.ToInt32(<NameOfNumericUpDownObject>.Text) < <NameOfNumericUpDownObject>.Value){ /*The up arrow was pressed (Value Increased)*/
    //Code Goes Here
    }
    else{/*Down Arrow Pressed (Value Decreaseed)*/
    //Code Goes Here
    }

    The .Text parameter doesn't seem to have an intellisense entry but it is there, and it doesn't get updated right away so it hangs around at the previous value. By converting it to an int and comparing it to the .Value parameter, which DOES get updated immediately, you can discern whether the value has increased or decreased.

    Hope this helps.

  10. #10
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: How to check if Up/Down Button is pressed on NumericUpDown Control...

    Thanks for your eagerness to help. But I suppose after 7 yaers, it is safe to assume that this thread was resolved. Please do not revive old threads. There are plenty of active threads that also need help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured