There is a ComboBox that stores the months of the year in text format. How can I write the number of the month in DataTime?

enter image description here

Wherever the month is added to the list, I cannot save the month number I need right away, because I need a user's choice.

for (int i = 1; i <= 12; i++) { cmbMonth.Items.Add(now.ToString("MMMM")); now = now.AddMonths(1); cmbMonth.Text = DateTime.Now.ToString("MMMM"); } 

It is necessary to get the name of the month from cmbMonth.Text order to record the full date selected by the user. But since the month is stored as text, problems have arisen. How can you solve? (Write a cycle with months and their numbers is not an option, since the text of the month in the ComboBox depends on the language of the system)

    3 answers 3

    There are a couple of good Parse and ParseExact methods for translating from String to ParseExact . Here is the second and suitable for this task!

    The essence of the following - you give him 3 arguments:

    1. string , where there are values ​​associated with time (whether it is time, date, month or something else).
    2. A ParseExact mask on which the ParseExact method will understand what is in paragraph 1.
    3. User culture or specified in advance.

    I will write an example to your task:

     var m = "Декабрь"; DateTime dd = DateTime.ParseExact(m, "MMMM", System.Globalization.CultureInfo.CurrentCulture); Console.WriteLine(dd); 

    As you can see, I set 3 parameters - month (December), mask (full month format) and culture (current user culture).

    The result of the program will issue:

     01.12.2018 0:00:00 

    Hope this solves your problem. Good luck!

    • Thank you very much! Clearly, it is clear that it was necessary. Thank you very much for the help! - Vasilisa Ivanova

    A small example of working with a combo box, where the element is not a string, but our class.

     class MyForm : Form { public MyForm() { var cb = new ComboBox(); var currMonth = DateTime.Now.AddDays(-DateTime.Now.Day + 1); for (var i = 0; i < 12; i++) { var dt = currMonth.AddMonths(i); cb.Items.Add(new MonthItem { Id = dt.Month, Text = dt.ToString("MMMM") }); } cb.DisplayMember = "Text"; cb.SelectedIndexChanged += (sender, args) => { var m = (cb.SelectedItem as MonthItem); if (m != null) { MessageBox.Show($"Selected {m.Text} with ID = {m.Id}"); } }; this.Controls.Add(cb); } private class MonthItem { public int Id { get; set;} public string Text { get; set;} } } 

    And as a result

    • Thank you for the answer. Undoubtedly in the future it may come in handy when working with ComboBox. - Vasilisa Ivanova

    It is probably best to store DateTime values ​​directly in the combo box. At the same time, configure the combo box format to display in the desired form.

    The cmbKuu.Text value is set only once, taking it out of the loop. In addition, you should consider the almost unbelievable case when between the two operators there will be a month change on the computer, as a result of which next month may appear in cmbKuu . Therefore, do not call DateTime.Now twice.

     var now = DateTime.Now; cmbMonth.FormattingEnabled = true; cmbMonth.FormatString = "MMMM"; cmbKuu.Text = now.ToString("MMMM"); for (int i = 1; i <= 12; i++) { cmbMonth.Items.Add(now); now = now.AddMonths(1); } 

    Now in the event handler, you can get the selected DateTime value and use its properties.

     private void CmbMonth_SelectedIndexChanged(object sender, EventArgs e) { var dateTime = (DateTime)cmbMonth.SelectedItem; // используем dateTime.Month } 
    • Thank you for the answer. I'll keep it on mind! - Vasilisa Ivanova