UWP CalenderView Set Date And Focus - c#

I am having trouble setting the current date and focus to a CalendatView in Uwp.
When I press enter on a button I want to make the CalendarView visible select a date and focus on that date so that I can use the arrow keys to select another date.
cal.SelectionMode = CalendarViewSelectionMode.Single;
cal.SetDisplayDate(DateTimeOffset.Now.Date);
selectedDayItem.Focus(FocusState.Programmatic);
The selectedDayItem is a field being set in the handler:
private void cal_CalendarViewDayItemChanging(CalendarView sender,
CalendarViewDayItemChangingEventArgs args)
{
selectedDayItem = args.Item;
}
The calendar show the current date being set but the arrow keys don't seems foucused to that cell.The current selected cell which is moved by the arrow keys appears to be set in some kind of random manner? I just want the current selected cell to be where the date was set.There isn't a current selected cell to be set?

CalendarviewDayItemChanging event is called whenever the display of calendar view changes and is called for each of the dates that are being displayed. This means it will be called for example 31 times when you go to October, and your current code will just end up with the day item that has been changed the last (which can be pretty random and depends on the implementation of the CalendarView). Instead you will need to check for the CalendarViewDayItem.Date to make sure you are getting the right one:
private void cal_CalendarViewDayItemChanging(CalendarView sender,
CalendarViewDayItemChangingEventArgs args)
{
if (args.Item.Date == mySelectedDate)
{
selectedDayItem = args.Item;
}
}
The selectedDayItem now will be the instance with the selected date. However, I expect there will be a problem in case the selected date is already displayed - in which case this event will probably not fire. Unfortunately I don't have a reliable solution for this problem so far.

Related

ASP.NET c# increase date value by number days on each button click

I looked on here but I have not been able to find what I want to do.
I simply have a onclick event for a button, and I want to increase the date value of a label by any number of days each time I click the button.
So lets say that number would be 2 days. If the current value of the label is 5/1/2016 when I click the button it should be 5/3/2016, and if again 5/5/2016 and so on. I can get it to do update once on the first click but not on a second click. Here i my code
protected void NDateOn_Click(object sender, EventArgs e)
{
lblCurrentDate.Text = DateTime.Today.AddDays (2).ToString ("dd");
}
I know it has somthing to to with the "Today" but I am not sure what to do from here
I appreciate any help in advance.
The problem is that you are adding 2 days to TODAY's date every time. What you want to do is add 2 to the date already stored there. You would want to do something like this (warning: have not tried compiling this).
lblCurrentDate.Text = DateTime.Parse(lblCurrentDate.Text).AddDays(2).ToString("MM/DD/YYYY");
The parameter passed to ToString() formats the datetime. You can modify that to get different formats. If you want to only store the days and not the month/year then you might need to do some more work. Hope this helps.
You need maintain the data in viewstate, each button click increment by 2
protected void Button1_Click(object sender, EventArgs e)
{
if (ViewState["datacount"] == null)
{
ViewState["datacount"] = 0;
}
ViewState["datacount"] = ((int)ViewState["datacount"]) + 2;
Label1.Text = DateTime.Today.AddDays((int)ViewState["datacount"]).ToString("dd");
}
I hope this may helpful for you
Note : viewstate data can maintain in same page only, can maintain in multiple pages

How to automatic add the first line of text from a list box to a text box

Right I have got a list box which contains a list of tracks and when the track is pressed it moves to a second list box, what I now need to happen is have the first item that's in the second list box move automatic to a text box.
This is my current code for the first move
private void genreListBox_DoubleClick(object sender, EventArgs e)
{
playlistListBox.Items.Add(genreListBox.SelectedItem);
}
I am thinking it should be something like this
presentlyPlayingTextBox.AppendText(playlistListBox.);
But I'm not sure how to add the first line without clicking it.
I have tried this but I get an error for the value.
presentlyPlayingTextBox.Text = playlistListBox.SelectedItem.Value;
Normally you would use something like the 'SelectedIndexChanged' or 'SelectedValueChanged' action of a listbox, otherwise events like DoubleClick will fail if the keyboard is used to change the selection.
Also that event should be triggered on the second listbox when it is changed by the first.
EDIT:
On a standard Listbox, there is no .SelectedItem.Value, only .SelectedItem.
However as a listbox holds objects, not just text, you need to say you want the text value.
presentlyPlayingTextBox.Text = playlistListBox.SelectedItem.ToString();
I don't know if I fully understand your question, but if you're just attempting to move the first item from ListBox_2 to a textbox, would the following work?
presentlyPlayingTextBox.Text = playlistListBox.Items[0]?.Value; // C# >= 6
presentlyPLayingTextBox.Text = ((playlistListBox.Items == null) ? playlistListBox.Items[0].Value : "" ); // C# < 6
You could also create your own delegate/event that would fire when the user did some action.

C# disable a checkbox based on run-time textbox user input value

I have two textbox to let user input start date and end date. Then I have a checkbox that allow user to check to see some calculations. My question is, how can I disable the checkbox if the duration between start date and end date is shorter than a specific length. I mean, right after user input the start date and end date, he/she would see the checkbox is disabled because the time period length is not long enough.
if (productWealth.Count < 3)
checkBox7.Enabled = false;
This is what I have now, if count < 3, then checkbox 7 is disabled. Seems like the application only run the count when run click the RUN button, but I want them to see immediate effect.
You can use the event TextChanged, so when the user change the value of the TextBox you check if its true and enable the CheckBox.
tboxEndDate.TextChanged += new TextChangedEventHandler(tboxEndDate_TextChanged);
void tboxEndDate_TextChanged(object sender, TextChangedEventArgs e)
{
// Calcule the productWealth
if (productWealth.Count < 3) checkBox7.Enabled = false;
}
Look at the Leave() event for your start and end date textboxes. Place your if() statement in there.
Uh that is, if this is WinForms...

Update Label Without Button

Is there a way to make a label automatically update itself so that I don't have to use a button to send out the command. What I have setup is a subtotal textbox, discount textbox, tax label, shipping textbox, and total label. So, when people fill in the subtotal, discount, and shipping, I want the tax label to be calculated, but only if previously a certain state was selected in another part of the form. So then, with all those filled in, I want the total label to be filled in. All of these I know I can do with a button, but I was wondering if there is a way to automate it using C# in Visual Studio.
Thanks.
I use the TextChanged Event to update such values between pairs of textboxes. Here are some extracts of my code:
private void onLongitudeTextChanged(object sender, EventArgs e) {
updateDistanceAndBearing();
}
updateDistanceAndBearing does some error checking - this can be a good idea if the user can put invalid values in and then updates the Text property of the other TextBoxes
I have text boxes but update the label.Text property instead.
It gets more messy (at least I found it so) if you have numeric updowns to get values
You can call a method to update the label in the change events for the controls.
For more detail, please supply more detail.
this is off the top of my head but should get you pretty close...
private void taxChanged(object sender, EventArgs e)
{
updateTax();
}
private void updateTax()
{
// the rest of your logic, checking state, etc.
//
this.Tax.Text = aValueCalculatedInYourLogicAbove;
updateTotal()
}
private void updateTotal()
{
// sum up whatever fields need to be summed
//
this.Tax.Text = aTotalValueCalculatedAbove;
}

Set selected WPF DataGridCell to focused and editable upon event

Woot, first Stack Overflow post! I've been asked to work on a desktop application to improve an inventory process for my company. I dabbled with WPF in school and I figured I'd start there. After researching some, I learned about MVVM, put a design together, and forged ahead. Finally, I'm stuck and looking for some help and also a sanity check to see if I'm on the right path.
I have single-column DataGrid bound to an observable collection. Users of the application use a scan gun to enter values in. One potential value that I catch in my "Cell" model object is a "MoveNextColumn" value. This raises a custom event in my model that is handled in the View Model. The handler is supposed to simulate blank entries for all remaining rows in that column, set focus on the last row, and wait for input before moving on. So here is what I have so far:
private void dummyCell_MoveToNextColumn(object sender, RoutedEventArgs e) {
e.Handled = true;
// Cell is the model object containing the parsing rules and raising events
var lSender = sender as Cell;
var gridItems = ViewGridReference.Items;
var lastItem = gridItems[gridItems.Count - 1];
if (lSender == lastItem) {
// We are at the bottom of the column
// Move the program on to the next column
CurrentColumn++;
OnPropertyChanged("ItemPositions");
} else {
// Simulate "empty position" input for this cell and all cells down the column
// Cells are validating themselves as the simulation progresses
foreach (Cell item in ViewGridReference.Items) {
item.ActualItemCode = string.Empty;
}
// ViewGridReference is a reference to my DataGrid set from the view
ViewGridReference.Focus();
ViewGridReference.SelectedIndex = gridItems.Count - 1;
ViewGridReference.CurrentCell = new DataGridCellInfo(lastItem, ViewGridReference.Columns[0]);
((DataGridCell)ViewGridReference.SelectedItem).Focus();
}
}
All of this seems to be working as expected: all rows receive blank input and are validated (I use color properties in the cell to which the view binds to signify the validity of the entry).
Unfortunately, though the focus is on the last row as desired, it is not editable and the user cannot submit another "MoveNextColumn" value which would move the program on. The goal here is to minimize any keyboard interaction. Everything should be done with scan guns and barcodes.
Any ideas on how to make the selected cell editable after this code executes?
Any "hey, your design sucks" feedback would be cool too. This is new to me and I'm open to constructive criticism.
I have made some progress with this. The entire grid was left at an uneditable state in the code above. This now leaves focus on the last cell in my column and allows me to submit input with the scan gun.
This seems to work, but I'd still appreciate some feedback on whether there is a better way.
private void dummyCell_MoveToNextColumn(object sender, RoutedEventArgs e) {
e.Handled = true;
// Cell is the model object containing the parsing rules and raising events
var lSender = sender as Cell;
var gridItems = ViewGridReference.Items;
var lastItem = gridItems[gridItems.Count - 1];
if (lSender == lastItem) {
// We are at the bottom of the column
// Move the program on to the next column
CurrentColumn++;
OnPropertyChanged("ItemPositions");
} else {
// Simulate "empty position" input for this cell and all cells down the column
// Cells are validating themselves as the simulation progresses
foreach (Cell item in ViewGridReference.Items) {
item.ActualItemCode = string.Empty;
}
ViewGridReference.SelectedIndex = gridItems.Count - 1;
ViewGridReference.CurrentCell = new DataGridCellInfo(lastItem, ViewGridReference.Columns[0]);
(ViewGridReference.ItemsSource as ListCollectionView).EditItem(ViewGridReference.SelectedItem);
((DataGridCell)ViewGridReference.SelectedItem).Focus();
}
}
Updated 12/2/2010
Hey, there is an important update to this. The first thing to note is that text entry is being done with a scan gun in my scenario, so 'Enter' keys are sent down with each pull of the trigger. It shoots down each character followed by the Enter key all at once.
WPF sees this enter and wants to set the focus to the DataGridCell directly beneath the cell in which the Enter key input was received. The code above sets the focus to the last cell, but then the Enter key event still fires and is handled by DataGrid after this code is run. The effect is that the focus is reset back to the subsequent cell, not the last cell like I want.
So I need to either figure out how to eat the Enter key for just that scan, or I need to break how WPF handles Enter keys. The last line up there actually throws an exception. We are trying to use a Model class (Class.cs) as a DataGridCell, and there is nothing to handle that cast. Because of that, the Focus() method tries to operate on a null object and we get a NullReferenceException. This was really confusing me because Visual Studio 2010 would sometimes break to alert me about this, but sometimes it wouldn't. However, if I run the executable outside of Visual Studio, it works just fine. That's because unhandled, non-fatal exceptions are ignored and the Enter key behavior fails to operate as normal.
So it works, but in a pretty gross way. I either need to figure out how to do one-time handling of the Enter key and override the default WPF handler, or just leave it like it is and grimace.

Categories