I have two textbox to let user input start date and end date. Then I have a checkbox that allow user to check to see some calculations. My question is, how can I disable the checkbox if the duration between start date and end date is shorter than a specific length. I mean, right after user input the start date and end date, he/she would see the checkbox is disabled because the time period length is not long enough.
if (productWealth.Count < 3)
checkBox7.Enabled = false;
This is what I have now, if count < 3, then checkbox 7 is disabled. Seems like the application only run the count when run click the RUN button, but I want them to see immediate effect.
You can use the event TextChanged, so when the user change the value of the TextBox you check if its true and enable the CheckBox.
tboxEndDate.TextChanged += new TextChangedEventHandler(tboxEndDate_TextChanged);
void tboxEndDate_TextChanged(object sender, TextChangedEventArgs e)
{
// Calcule the productWealth
if (productWealth.Count < 3) checkBox7.Enabled = false;
}
Look at the Leave() event for your start and end date textboxes. Place your if() statement in there.
Uh that is, if this is WinForms...
Related
I have a problem in a winform app.
I have several masked textbox which use the short date mask (//____), the problem is that if I select all the text (either with Ctrl + a or from code) and I write a new date is like that the first character goes at an 11th position (the mask disappear and i see only the character I wrote) and if I press backspace the text becomes something like this _1/01/1979 and I have to select all again and press backspace or delete everything.
I handled in this way
private void maskedTxt_KeyPress(object sender, KeyPressEventArgs e)
{
MaskedTextBox msk = sender as MaskedTextBox;
if (msk != null)
{
if (!string.IsNullOrEmpty(msk.Text.Replace("/", string.Empty).Replace(":", string.Empty).Trim()) && _focused)
{
_focused = false;
SendKeys.SendWait("{BACKSPACE}");
}
}
}
_focused is a Boolean variable that I set to true if a validation error happens at the leave event of the masked textbox (invalid date, the date is too big or too small etc...)
so that when someone enters the textbox the text can be written correctly
Is there a better way to handle this "error" or this is good? I tried it and it worked but a lot of people will have to use this application and probably there will be some errors along the way.
Thanks
I am having trouble setting the current date and focus to a CalendatView in Uwp.
When I press enter on a button I want to make the CalendarView visible select a date and focus on that date so that I can use the arrow keys to select another date.
cal.SelectionMode = CalendarViewSelectionMode.Single;
cal.SetDisplayDate(DateTimeOffset.Now.Date);
selectedDayItem.Focus(FocusState.Programmatic);
The selectedDayItem is a field being set in the handler:
private void cal_CalendarViewDayItemChanging(CalendarView sender,
CalendarViewDayItemChangingEventArgs args)
{
selectedDayItem = args.Item;
}
The calendar show the current date being set but the arrow keys don't seems foucused to that cell.The current selected cell which is moved by the arrow keys appears to be set in some kind of random manner? I just want the current selected cell to be where the date was set.There isn't a current selected cell to be set?
CalendarviewDayItemChanging event is called whenever the display of calendar view changes and is called for each of the dates that are being displayed. This means it will be called for example 31 times when you go to October, and your current code will just end up with the day item that has been changed the last (which can be pretty random and depends on the implementation of the CalendarView). Instead you will need to check for the CalendarViewDayItem.Date to make sure you are getting the right one:
private void cal_CalendarViewDayItemChanging(CalendarView sender,
CalendarViewDayItemChangingEventArgs args)
{
if (args.Item.Date == mySelectedDate)
{
selectedDayItem = args.Item;
}
}
The selectedDayItem now will be the instance with the selected date. However, I expect there will be a problem in case the selected date is already displayed - in which case this event will probably not fire. Unfortunately I don't have a reliable solution for this problem so far.
I looked on here but I have not been able to find what I want to do.
I simply have a onclick event for a button, and I want to increase the date value of a label by any number of days each time I click the button.
So lets say that number would be 2 days. If the current value of the label is 5/1/2016 when I click the button it should be 5/3/2016, and if again 5/5/2016 and so on. I can get it to do update once on the first click but not on a second click. Here i my code
protected void NDateOn_Click(object sender, EventArgs e)
{
lblCurrentDate.Text = DateTime.Today.AddDays (2).ToString ("dd");
}
I know it has somthing to to with the "Today" but I am not sure what to do from here
I appreciate any help in advance.
The problem is that you are adding 2 days to TODAY's date every time. What you want to do is add 2 to the date already stored there. You would want to do something like this (warning: have not tried compiling this).
lblCurrentDate.Text = DateTime.Parse(lblCurrentDate.Text).AddDays(2).ToString("MM/DD/YYYY");
The parameter passed to ToString() formats the datetime. You can modify that to get different formats. If you want to only store the days and not the month/year then you might need to do some more work. Hope this helps.
You need maintain the data in viewstate, each button click increment by 2
protected void Button1_Click(object sender, EventArgs e)
{
if (ViewState["datacount"] == null)
{
ViewState["datacount"] = 0;
}
ViewState["datacount"] = ((int)ViewState["datacount"]) + 2;
Label1.Text = DateTime.Today.AddDays((int)ViewState["datacount"]).ToString("dd");
}
I hope this may helpful for you
Note : viewstate data can maintain in same page only, can maintain in multiple pages
So in my project I have a few textBoxes that hold the coordinates of two corners (Latitude & Longitude). The textBoxes are updated by a timer (which gets a value from a server and sets the textBoxes if the value received is different from the current value). Problem is, I want the textBoxes to be available for manual editing; However if I'm in the middle of typing the number and the timer checks the current value he sees that it's a different thing from what the server returned and changes it immediately. Is there a way to check if the textBox is being edited at the moment, or a better way to solve this solution?
code (samples, the code is the same for the two corners):
if (northEastLatitude != double.Parse(neLatTB.Text)) //neLatTB is the textBox
neLatTB.Text = northEastLatitude.ToString();
else //No answer returned from the server so we need to reset the textBoxes
{
northEastLatitude = 0;
northEastLongitude = 0;
if(neLatTB.Text != "0")
neLatTB.Text = northEastLatitude.ToString();
if(neLngTB.Text != "0")
neLngTB.Text = northEastLongitude.ToString();
}
in addition, I have functions for TextChanged events for all of the textBoxes (so that when I set the coordinates manually it uploads them to the server). Is there any way to prevent this function from being called whenever I press the dot key? apparently it calls the event too (marks the ending of the text entering).
It really depends on your design but if you want to use TextBox to show updatable values and also make it possible to edit you have to suppress code from your timer to execute. WinForms TextBox doesn't have an option to show you if text is changing programmatically or by user interaction. You have to somehow make it by yourself.
There is plenty of ways to do that of courde. One way is to use Enter/Leave events to detect when your TextBox gets or loses focus. But there will be a need to click somwhere out from the control after edit.
Another one, and probably desired by you would be using TextChanged event preventing your timer from updating field until text in TextBox will be typed in full. I would do it something like that:
Fisrtly I would declare two bool variables for blocking parts of code from execution:
private bool _isDirty; // used when user types text directly
private bool _suppresTextChanged; // used when timer updates value programmatically
After that I would write TextBox.TextChanged event listener:
private void neLatTBTextChanged(object sender, EventArgs args)
{
if(_suppressTextChanged)
return;
_isDirty = true; // toggle dirty state
if(/* text has good format */)
{
// Upload changes to server
_isDirty = false; // end manual edit mode
}
}
And inside timer method i would set:
_suppresTextChanged = true; // on the beginning
if (northEastLatitude != double.Parse(neLatTB.Text)) //neLatTB is the textBox
neLatTB.Text = northEastLatitude.ToString();
else //No answer returned from the server so we need to reset the textBoxes
{
northEastLatitude = 0;
northEastLongitude = 0;
if(neLatTB.Text != "0")
neLatTB.Text = northEastLatitude.ToString();
if(neLngTB.Text != "0")
neLngTB.Text = northEastLongitude.ToString();
}
_suppresTextChanged = false; // after edit was made
Personally i think this design can lead to a lot of problems (consider what to do when user stops typing and leave the TextBox in _isDirty state etc...). Instead of using just TextBox I would add a Label to store data from timer (and probably data the user will type) and left TextBox just for entering user specific values.
I have two textbox's. I have an event setup for the "onLostFocus" of the textbox's. If i finish typing a word into both boxes one after the other all is well. At the point where i click back on the first textbox i want to click halfway through the word (perfectly resonable thing for a user to do). When the onLostFocus event fires here is my code :
void tbox_LostFocus(object sender, RoutedEventArgs e)
{
IInputElement focusedelement = FocusManager.GetFocusedElement(this);
if (focusedelement is TextBox)
{
TextBox focusedTextBox = focusedelement as TextBox;
int focusIndex = m_commandsStacker.Children.IndexOf(focusedTextBox);
int caretIndex = focusedTextBox.CaretIndex;
The caret index returns as 0 when i call focusedTextBox.CaretIndex. At that point I need to refresh the whole form and set parameters and all other kinds of whizzery, storing the texbox index and caret position to put everything back the same. Why does it return 0 and not the position where the caret should be halfway between the word?
This just doesn't work in c# express edition 2008 (whatever version of WPF that is) so the best thing to do here is to switch to onTextChanged and run the exact same code there, which works a treat. Its obviously annoying that you have to run the code multiple more times than if it worked on lost focus. When I get the time I will check if it works in c# express 2010 as we are upgrading (eventually)