I Am using a DateTime Picker in WindowsForm Application of C#. I initalize My dateTImePicker with DateTime.Min Value. What I want is that when a user clicks that dropdown It Should Change its value to DateTime.Now Value and show the current value in calendar but I have to first open it then close it and then reopen it to get the required date in calendar. How can I Do it in a single Click? Any Help will be appreciated.
So far I have tried these events:
private void GroupEndingDate_MouseUp(object sender, MouseEventArgs e)
{
GroupEndingDate.Value = DateTime.Now;
}
private void GroupStartingDate_MouseDown(object sender, MouseEventArgs e)
{
GroupStartingDate.Value = DateTime.Now;
}
private void GroupStartingDate_DropDown(object sender, EventArgs e)
{
GroupStartingDate.Value = DateTime.Now;
}
Try this in your dropdown event. This worked for me.
private void Form1_Load(object sender, EventArgs e)
{
dateTimePicker1.Value = DateTime.Now.AddDays(-3);
}
private void dateTimePicker1_DropDown(object sender, EventArgs e)
{
dateTimePicker1.Value = DateTime.Now;
}
The following works just fine for me without having to click the control a second time.
private void dateTimePicker1_DropDown(object sender, EventArgs e)
{
dateTimePicker1.Value = DateTime.Now;
}
But I agree this approach seems a little odd to me. The date will be set every time the control opens up. Why not just initialize it to DateTime.Now in your form's Load event?
Related
I recently started to learn C# and right now I want to mess arouwnd with the Form[Design].
Right now I'm trying to BringToFront() custom controls each time I hover over a button (Ive got a few buttons close to each other, each time I hover over them I get a certain User Control).
This is what I've got so far:
private void button1_Hover(object sender, EventArgs e)
{
costumControl1.BringToFront();
}
private void button1_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
private void button2_Hover(object sender, EventArgs e)
{
costumControl2.BringToFront();
}
private void button2_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
private void button3_Hover(object sender, EventArgs e)
{
costumControl3.BringToFront();
}
private void button3_Leave(object sender, EventArgs e)
{
CostumControl0.BringToFront();
}
But bringing to front the user control costumControl0 when I hover from a button to another it's not what I want.
But I don't know how to go about this.
Is it possible to just add a if statement where I check if I'm not hovering the buttons close to my current one and then display the costumControl0.
Or a timer is necessary to delay the display of the costumControl0 and skip the command if I'm starting another event.
If the timer is needed, can I use one timer for all of the buttons or do I need to create one for each?
Or whats the best approach for this?
I am trying to add an event handler to a date picker control programmatically during the window load process, but it is giving me an error message:
Cannot implicitly convert type
'System.Windows.Controls.SelectionChangedEventHandler' to
'System.EventHandler'
The code:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
LoadCombo();
dpFrom.SelectedDateChanged += new SelectionChangedEventHandler(this.dp_SelectedDateChanged);
}
private void dp_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
// reset label;
}
Just try the short form (syntactical sugar):
dpFrom.SelectedDateChanged += this.dp_SelectedDateChanged;
(this is usually automatically generated when coding in Visual Studio)
I have tried to change my labels text in both the form_load and form_shown methods and nothing happens. I don't want to set the text in the properties as I want to use the text in another label later. Ive tried both these methods neither works.
private void History_Load(object sender, EventArgs e)
{
populateHistoryQuestionArray();
historyQ1.Text = historyQuestion[0];
}
and
private void History_Shown(object sender, EventArgs e)
{
populateHistoryQuestionArray();
historyQ1.Text = historyQuestion[0];
}
You must add ToString()
private void History_Load(object sender, EventArgs e)
{
populateHistoryQuestionArray();
historyQ1.Text = historyQuestion[0].ToString();
}
I Create login form and want to put "remember me" check box on it.
But every time i open program it doesn't change.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
Project.Properties.Settings.Default.rememberMe = true;
Project.Properties.Settings.Default.Save();
}
else
{
Project.Properties.Settings.Default.rememberMe = false;
Project.Properties.Settings.Default.Save();
}
}
Also i want to save user login information, should i save them in app setting just like remember me setting or there is better way?
You're saving the settings, but you need to retrieve those settings too.
Subscribe to the Form's load event and set the value of the CheckBox.
private void Form1_Load(object sender, EventArgs e)
{
checkBox1.Checked = Project.Properties.Settings.Default.rememberMe;
}
Also, and this is just common practice, but your code could be shorter:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Project.Properties.Settings.Default.rememberMe = checkBox1.Checked;
Project.Properties.Settings.Default.Save();
}
I navigated to a page where I want to be able to see the date i got from the Database and to edit that date if it is needed. When I change the date, it always go back to the date that was set by the database previously. How do I solve this. Below are my code:
private void dateData_Loaded(object sender, RoutedEventArgs e)
{
dateData.Value = DateTime.Parse(NavigationContext.QueryString["Date"]);
}
private void dateData_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
{
dateData.Value = (DateTime)dateData.Value;
}
I Think the loaded event is getting called every time you are changing the date and you are assigning the date you are getting from your querystring parameters. So try something like this And check if it works
private void dateData_Loaded(object sender, RoutedEventArgs e)
{
if(NavigationContext.QueryString.ContainsKey("Date"))
{
dateData.Value = DateTime.Parse(NavigationContext.QueryString["Date"]);
NavigationContext.QueryString.Remove("Date");
}
}