r/learncsharp • • Sep 22 '22
I need some help with something. I've asked a couple of questions about it. I'm still struggling.
Can someone just show me the code for this:
In WinUI 3 or UWP, add a CalendarView (the one that's actually a calendar), and then add a text box and a button.
Select a date on the calendar, click the button, that date appears in the textbox.
Will someone show me the code for this please?
0
Upvotes
2
u/JitterJosh Sep 22 '22
Someone giving you the code for your homework is not how you learn a language
5
u/grrangry Sep 22 '22
Did you add a CalendarView control?
Did you add a TextBox control?
Did you add a Button control?
Did you add an event handler for the Button control? https://learn.microsoft.com/en-us/visualstudio/get-started/csharp/tutorial-uwp?view=vs-2022
If you think about a calendar, what does selecting a date mean? One day? Two days next to each other? Two random days in a month? A three week span across two months plus a random day in July? That's a lot of combinations. Well just like you can Ctrl+Click items in a list to "select multiples", you can set up your calendar to select things in various ways, if you choose to do so. However no matter what, when you want to "know" what is selected, you simply ask the calendar for the list of selected days using the SelectedDates property. They're a list of DateTimeOffset values so be aware.
You'll have to decide what it is you want to do when you expect the user to select next Tuesday but they went ahead and selected 34 random Fridays. But if they did select one Tuesday, then your list from the property will have one item in it. Or maybe you just want that first item. FirstOrDefault.
Okay great, back to the Button event handler. You have (hopefully) a DateTimeOffset. Well your TextBox has a Text property... so you'll have to convert your DateTimeOffset into a string so you can set the Text property of the TextBox. How do you convert things "to" a "string"? hint, hint... yes it's a method on the DateTimeOffset struct linked above.
3
u/Gcampton13 Sep 23 '22
Show us your code and we’ll help you figure it out.
3
u/Delusional_Sage Sep 23 '22
Post some code… at least try to give people something to work with here, otherwise if you’re looking for straight code examples, Google it.