I have a usercontrol containing 3 empty minutes textboxes named txtMorningMinutes, txtAfternoonMinutes and txtCapacityMinutes. This UserControl is repeated 2 times as Saturday and Sunday in a Webform with Save button outside UserControl and inside Webform.
Now there is a condition that user should enter a value less than that of txtCapacityMinutes for which the value is coming from database. Lets say the value of txtCapacityMinutes is 60.
Now the user enters 10 in Saturday morning textbox called txtMorningMinutes and saves the data. It will be persisted to the database.
Now the user enters 70 in Saturday morning textbox called txtMorningMinutes and tries to save data. Before saving the data in OnTextChanged of txtMorningMinutes, we need to check whether newly entered data is less than that of 60 which is txtCapacityMinutes. Because newly entered data 70 is greater than 60, we need to revert it back to 10.
The TextChanged event is something like below
protected void txtMorningMinutes_TextChanged(object sender, EventArgs e)
{
}
Where should I preserve the initial value of 10 in UserControl. If it is stored in Page_Load event of UserControl, it will be repeated 2 times i.e., for Saturday UserControl and Sunday UserControl. So, I need know about where to store previous value of txtMorningMinutes.Text i.e., 10 and apply the condition whenever necessary in OnTextChanged event.
When the user focuses the textbox you can save a copy of the relevant variables before they are manipulated. When the user clicks the save button you can perform your checks and rollback to the saved variables values if needed.
static string previousValue = "";
protected void Page_Load()
{
if(!IsPostBack)
{
previousValue = "5";
}
}
protected void txtMorningMinutes_TextChanged(object sender, EventArgs e)
{
if(Convert.ToInt32(txtMorningMinutes.Text) > Convert.ToInt32(txtCapacityMinutes.Text))
{
txtMorningMinutes.Text = Convert.ToInt32(txtMorningMinutes.Text) - Convert.ToInt32(txtCapacityMinutes.Text)
}
}
Related
I am building af chess clock in c#. In this clock I increment with seconds for each draw. The draw comes from hitting a button. The seconds in the incrementing is chosen in a combobox 'comboboxSeconds'. I want to use this value 5, 10, 15 seconds in the button event method. How do I subscribe to the combobox event so I can use the value in the button click event.
The code is simple:
```
private void btnStartHvid_Click(object sender, RoutedEventArgs e)
{
if (Hvidantal > 1)//we do not increment in the first draw)
{
timeHvid += TimeSpan.FromSeconds(5);// Add five seconds. Must come from combobox. So it could be 5, 10 or 15
}
lblHvidAntal.Content = Hvidantal++.ToString(); //show number of draws in a label
DPtimerHvid.Stop();
DPtimerSort.Start();
}
private void comboboxSeconds_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBoxItem cbi = (ComboBoxItem)(sender as ComboBox).SelectedItem; //ok
String seconds = cbi.Content.ToString(); //seconds chosen 5, 10 eller 15
}
I assume the problem here is read the selected time from the combobox in the button-handler, so it can be used to increment the time.
Assuming wpf, you should be able to use the SelectionBoxItem property to check the selected time. I.e.
timeHvid += TimeSpan.FromSeconds((int)myCombobox.SelectedItem);
You might want to assign a name to your combobox, i.e. insert x:Name="myCombobox" in the xaml.
Also, you need to decide what type of objects you want to put into your combobox. Using strings will make it more difficult to convert the value to a time. Using int will work fine if you only want to show the numbers, and no additional text. Or you could use a custom class to store both the actual value, and override ToString to determine how the value should be displayed.
I am currently working on a Program that displays and handles data from a database in C# forms. I have a DataGridView that is empty upon Program Load, and it later filled with data from the database when the user selects items from a ToolStripMenu. I need to be able to alter the background color of a cell in each row, every second. I have instantiated a timer, and set it up to tick every second. I have set up a method event to execute every time the timer ticks (every second), shown below:
void _timer_0_Tick(object sender, EventArgs e)
{
}
In this method I would like to be able to color the "status" cell of each row in the DataGrid to either red or green, based upon if the Status is good or bad.
Here is some pseudocode in the method illustrating what I would like to do:
void _timer_0_Tick(object sender, EventArgs e)
{
bool isDataStable = //code to determine if stable on my end
if(isDataStable == true)
{
DataGrid.Row[Index].Column["Status"].BackColor = Green;
}
else
{
DataGrid.Row[Index].Column["Status"].BackColor = Red;
}
}
Every DataGridView row/column edit example I've seen has to use an event handler based upon the DataGridView, how can I implement the timer while also editing the GridView in real time?
Thanks
Your pseudo-code is close - just use the proper properties:
void _timer_0_Tick(object sender, EventArgs e)
{
bool isDataStable = //code to determine if stable on my end
if(isDataStable == true)
{
DataGrid.Rows[Index].Cells["Status"].Style.BackColor = Color.Green;
}
else
{
DataGrid.Rows[Index].Cells["Status"].Style.BackColor = Color.Red;
}
}
I have a text box with a query the the user can load and change or leave alone. I have a test database that the user sends this to with a Development Test button. The results are displayed in another forms text box in XML.
Is there a way to check if the user changed anything in the text box since the development button was pushed? I have another button called production where, if the results returned to the user are correct the user can save this query to a production database.
What I want to prevent is a user loading a correctly worded query, test it, the results are good, and then the user changes something, either on accident or on purpose where this query will no longer work, and submitting it to production. At which point there will be errors and things will have to be found and changed. I would like a little message box that comes up and says: "The text box has been changed since you tested it are you sure you want to submit this to production". Now they can still submit it but at least they are warned..
Presume at some point a button is pushed or some action happens.
1) When you load the box, you could hold the original value in a variable
2) When the action occurs, you could re-query the same query and make another round-trip to retrieve the original value
3) Then compare the original value with current
if (textBox.Text == myOriginalValue)
{
}
else
{
}
You can subscribe to the TextChanged event, and update your field then.
textbox.TextChanged += (sender, e) => { textValue = textbox.Text; };
set a dirty flag in a Tag property and check it.
//Set 1 in the Tag property after loading data
private void LoadData()
{
textBox1.Text = myValue;
textBox1.Tag = 0;
}
//Set 0 in the Tag property on TextChange
private void textBox1_TextChange(object sender, EventArgs)
{
((TextBox)sender).Tag = 1;
}
//Check if Tag != 0 then do your stuff.
private void button1_click(object sender, EventArgs e)
{
if (textBox1.Tag != null && Convert.ToInt32(textBox1.Tag) != 0)
{
MessageBox.Show("The text box has been changed since you tested it are you sure you want to submit this to production", "Value Has Changed");
}
}
I want to write a simple program in C# that performs conversion from kVA rating to kW rating and calculate Current ratings of a electricity generator at fixed voltage level and PowerFactor.
What the program would do is the user enter value in any of the three textboxes (let say kVA) and the program calculate other two parameters (kW and Current) in respective textbox without pressing any other button.
after that when the user changes the value of other two parameter (say kW) then program would recalculate kVA and Current.
Same with the current textbox.
You need something like this:
On Form Load:
Textbox1.KeyUp += ProcessChange;
Textbox2.KeyUp += ProcessChange;
Then the ProcessChange handler:
private void ProcessChange(object sender, KeyEventArgs ka)
{
if(sender == Textbox1)
{
//do work...
}
else //sender==Textbox2
{
//do work...
}
}
This should get you going in the right direction...
I have an unbounded DataGridView with several columns. I've created a CellValidating function, and it works well. Right now I'm trying to read data in from a text file and put it in the DataGridView. When I do this however, the CellValidating function never gets called. Is it possible to validate data entered like this?
Edit: Here's part of my CellValidate function:
private void Grid_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
string headerText = Grid.Columns[e.ColumnIndex].HeaderText;
switch (headerText)
{
case "Number":
if (!String.IsNullOrEmpty(e.FormattedValue.ToString()))
{
if (!Regex.IsMatch(e.FormattedValue.ToString(), #"(^\d{1,2}$)"))
{
MessageBox.Show("Number must be a 1 or 2 digit positive number");
e.Cancel = true;
}
}
}
}
CellValidating and RowValidating events fire when the current cell/row is being changed or when ending an edit operation. You should be able to force the validation trigger using BeginEdit and EndEdit.