Quas Wex Exort
Quas Wex Exort

Reputation: 15

Display text on textbox based on month selected in datetimepiecker

Hello Guys I have a problem with datetimepicker I am using c# application I have 1 textbox name texbox1 and 1 datetimepicker name datetimepicker1

my question is 1. how to display text in my textbox if i select the month in datetimepicker for example i select the month of January there will be number 3 that will display in my textbox if i select February number 98 will display on my textbox and soon......

im working with my assignment

this is my code

if(datetimepicker1.Value.Month.ToString() == "January")

{

   textbox1.Text = "3";

}

else if(datetimepicker1.Value.Month.ToString() == "February")

{

   textbox1.Text = "98";

}

still no text will display on textbox

Upvotes: 0

Views: 630

Answers (2)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186688

Why don't you just compare Month with 1 for Jan ... 12 for Dec?

You can use ValueChanged event of DateTimePicker class to find out the moment when DateTimePicker.Value changes

// Whenever dateTimePicker1 Value changed
private void datetimepicker1_ValueChanged(object sender, EventArgs e) {
  if (datetimepicker1.Value.Month == 1) // <- If month is January
    textbox1.Text = "3";
  else if (datetimepicker1.Value.Month == 2) // <- If month is February
    textbox1.Text = "98";
}

Since datetimepicker1.Value.Month is in fact integer, so

  datetimepicker1.Value.Month.ToString(); // <- returns "1", not "January"

Upvotes: 3

Amr
Amr

Reputation: 50

Try this:

if (datetimepicker1.Value.Date.Month == 3)
   texbox1 .Text = "3";

N.B: 3 stands for March, 1 for Jan, etc.

Upvotes: 3

Related Questions