简体   繁体   中英

How to assign a date variable from form1 to form2 in datetimepicker

In Section 28 of my source code I am assigning, the database field of date (car_completiondate) to a variable mcompdate and also to label4.Text. I want to use mcompdate or label4.Text in the second form, please advise how to?

// 28
// var mcompdate = (DateTime)reader1["car_completiondate"];
// label4.Text = (String.Format("{0:yyyy-mm-dd}", Convert.ToDateTime(reader1[28]).ToShortDateString()));
label4.Text = (Convert.ToDateTime(reader1[28]).ToString("yyyy-MM-dd"));
DateTime mcompdate = DateTime.Parse(label4.Text);
                
// DateTime mcompdate = DateTime.ParseExact(label4.Text, "yyyy-MM-dd", null);      

Well the easiest way is to make a field in one Form and access it in the other Form. For example

Field in second Form:

public DateTime date { private get; set; }

Now in first Form you just have to set the value of the date field form2_name.date = mcompdate and then in the second form you can use however you like. If you want to performs more operations than you can also use a public method just as explained.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM