-1

I have a question regarding the following question about NumericUpDown:

Good NumericUpDown equivalent in WPF?

How I can do this for 12 months of a year? I am planning to use a vertical ScrollBar with a textbox. I want to link the vertical ScrollBar up and down clicks to increment and decrement the months in the textbox using C#.

<ScrollBar x:Name="scbm" 
    HorizontalAlignment="Left" Height="26" Margin="230,195,0,0" 
    VerticalAlignment="Top" RenderTransformOrigin="0.542,0.83"/>

<TextBox x:Name="txtm" 
    HorizontalAlignment="Left" Height="23" Margin="139,195,0,0" 
    TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="66"/>

Can anyone tell me how I can accomplish this?

Community
  • 1
  • 1
getc he
  • 107
  • 4
  • 13
  • Since you have found an answer to your question here, please delete your other question - [http://stackoverflow.com/questions/19829031/good-stringupdown-equivalent-in-wpf](http://stackoverflow.com/questions/19829031/good-stringupdown-equivalent-in-wpf) – Suresh Dec 02 '13 at 08:05

1 Answers1

0

You can have a look This article that does something similar with a datepicker.

Basically you'll want to have a property for the month, and depending on your approach (code behind / mvvm), handle the click events on the up/down buttons, or the keyboard keydown event to handle them logic (so in your case, add the up/down buttons, and wire them to the events, naming them appropriately).

For example, having this on your xaml:

<DatePicker ... PreviewKeyDown="PreviewKeyDown_EventHandler" ...  /> 

And something like this in your code behind:

private void PreviewKeyDown_EventHandler(object sender, System.Windows.Input.KeyEventArgs e)
{
  // Avoid them nasty exceptions is the user hits "up" or "down" with no date selected:
  if (sender == null || ((DatePicker)sender).SelectedDate == null)
    return;

  // Do this on up 
  if (e.Key == Key.Up)
  {
     ((DatePicker)sender).SelectedDate =
    ((DatePicker)sender).SelectedDate.GetValueOrDefault().AddMonths(1);
  }

  // And this on down
  if (e.Key == Key.Down)
  {
    ((DatePicker)sender).SelectedDate =
    ((DatePicker)sender).SelectedDate.GetValueOrDefault().AddMonths(-1);
  }
}

That example uses a datetime as the property, but you can do similar things with an int if that's what you're after.

(again, have a look at the link for more options, examples, and code example if you'd like)


Edit:

This doesn't have theh benefits of the up/down keys like the article's example, but this works:

<ScrollBar x:Name="scbm" 
    SmallChange="1" Maximum="12" Minimum="1"

    Value="{Binding MonthScrollBar}"
    HorizontalAlignment="Left" Height="26" Margin="230,195,0,0" 
    VerticalAlignment="Top" RenderTransformOrigin="0.542,0.83" />

<TextBox x:Name="txtm" 
    Text="{Binding MonthScrollBar}"

    HorizontalAlignment="Left" Height="23" Margin="139,195,0,0" 
    TextWrapping="Wrap"  VerticalAlignment="Top" Width="66" />

On my ViewModel (or your codebehind, if you want, do something similar)

public int MonthScrollBar
{
    get { return _monthScrollBar; }
    set { _monthScrollBar = value; 
          RaisePropertyChanged("MonthScrollBar");}
}
private int _monthScrollBar;

And setting the property value to whatever you want on the constructor.

Note that I'm using MVVM, binding to a property with notification change (so changes are propagating to the View), and initializing it. Both the ScrollBar and TextBox bind to the same MonthScrollBar property) If you're doing code behind, you can access it directly from the code behind.

Noctis
  • 11,507
  • 3
  • 43
  • 82
  • I need only months just like this link: http://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html – getc he Nov 08 '13 at 04:46
  • If you're binding to a DateTime, add or subtract a month. If you're binding to a number, add or substract 1 from the property. You've got all the info you need right there ... – Noctis Nov 08 '13 at 04:53
  • Datepicker is showing complete date :mm/dd/yyyy ..But i want to display string of month to user. – getc he Nov 08 '13 at 05:06
  • Obviously, It's from the given example. You can either have a `TextBox` , or play with the date format. (I'll suggest you use a textbox, and have an int there, just make sure in your logic to go from 12 to 1 when you hit up) – Noctis Nov 08 '13 at 05:08
  • Do you mean use text box along with ScrollBar ????my question is how to link the ScrollBar up and down clicks to increment and decrement the months in the textbox? – getc he Nov 08 '13 at 05:24
  • note: where did I suggest to use any framework? I suggest you look up MVVM [here](http://en.wikipedia.org/wiki/Model_View_ViewModel) or [here](http://msdn.microsoft.com/en-us/magazine/dd419663.aspx), and in the meanwhile, work with your codebehind – Noctis Nov 08 '13 at 07:00