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.
Related
I'm confused on what I'm supposed to use to get a users input.
I have 2 TextBoxes and a Button and I want to take what the user enters in those 2 TextBoxes and use it to calculate the total when the button is pushed.
I already have 3 variables declared: one for 1st input, one for the 2nd input, and one for the total of the two.
double length;
double width;
double area;
area =((length*length)+(width)(width));
I have this code inside of an event handler that will calculate the area when the Button is clicked. C# is new for me, I remember in Java you can use a scanner but I'm not sure what you can use here.
The TextBox control has a Text property. You would simply reference that to get the value out of the TextBox.
Note that in the following example, I'm making assumptions about the names of your controls since you didn't provide them.
private void CalculateBtn_Click(object sender, EventArgs e)
{
string lengthString = LengthTxt.Text;
string widthString = WidthTxt.Text;
}
You'll notice that the Text property returns a string. You'll need to parse that into a number to do the actual calculations. By using, for example, Double.Parse or Double.TryParse.
Since you're taking user input, you should use Double.TryParse. A TextBox will take any string, after all. Not just strings that parse to a double.
private void CalculateBtn_Click(object sender, EventArgs e)
{
if (!double.TryParse(LengthTxt.Text, out double length))
{
MessageBox.Show("Please enter a number for the length.");
return;
}
if (!double.TryParse(Width.Text, out double width))
{
MessageBox.Show("Please enter a number for the width.");
return;
}
}
Click on one of the box and you should see the properties tab on the bottom right. Search for the (Name) property under "Design" and set a name such as "length". In the EventHandler, get the string value with (the name of your box).Text and you can use a method such as Int32.Parse(string) to convert it to an int.
I have two textboxes, textboxB copies from textboxA every time the text is changed but textboxA doesn't keep scrolling to the end. They're both one line textboxes that must have the cursor at the end 100% of the time. pls help.
private void Question_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
autoComplete.Text = Question.Text;
autoComplete.Focus();
autoComplete.Select(autoComplete.Text.Length, 0);
Question.Focus();
}
Try this:
TextboxA.CaretIndex = TextboxA.Text.Length;
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)
}
}
I am working on a small program and, everything works fine. Instead of having a hard coded timer though, I'd like to change the timers interval from the form from a listbox or a numericupdownbox, combobox or something along those lines.
So instead of it being a hard coded 3000MS like it is I wanted to be able to change it on the form from a small menu with 1000-10000 milliseconds.
The thing is, I am not sure how to tell the timer to use an interval specified in a optional box.
Is it possible?
Thanks.
If you're going to use a control that allows for a non-numeric input such as a TextBox ensure you validate that the input is in fact a number. Better to trap errors in the first place than deal with exceptions later.
private void SetIntervalButton_Click(object sender, EventArgs e)
{
int interval = 0;
bool success = Int32.TryParse(intervalTextBox.Text, out interval);
if(success)
{
operationTimer.Interval = interval;
}
}
You can omit the checking above if you're using a NumericUpDown control as it only allows a numeric value.
private void SetIntervalButton_Click(object sender, EventArgs e)
{
operationTimer.Interval = (int) numericUpDown1.Value;
}
You can set the interval of a timer to a value you like in the change event your your combobox.
private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
ComboBox comboBox = (ComboBox) sender;
aTimer.Interval = double.Parse(ComboBox1.SelectedValue);
}
You should use double.TryParse if you can not ensure valid data for setting the interval. If the value is being taken from a combobox and its read-only then there is no need for TryParse.
You can try:
myTimer.Interval = (int) myTextbox.Text;
You can put this in textchanged event if you want it fired every time you change the textbox value.
I have a ComboBox in my C# Winform. Some of the Item texts are larger than the size of the ComboBox. Whenever I select these values, the end portion is visible. How can I ensure that, the beginning portion is shown.
For example,
Consider the items: {"small","big text selection"}
Now, the ComboBox is large enough to show 8 characters. When I select, "big text selection",
I can only see, "election", but I would like to view "big text" instead.
Is it significantly to you to use DropDownStyle equal to DropDown? In this style combobox have an editor, so when yoou select new value from the list it display in the editor and cursor position set at end of text. So in this case you should send HOME button code to the combobox editor this will move cursor at the start of line. You can do that as shown below:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SendKeys.Send("{HOME}");
}
But if DropDown style is not significant to you just change it to DropDownList and you will have desired behavour.
In the SelectedIndexChanged event create a Timer:
Timer timer = new Timer();
timer.Interval = 10;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
And in its Tick:
void timer_Tick(object sender, EventArgs e)
{
comboBox1.Select(0, 0);
(sender as Timer).Stop();
(sender as Timer).Dispose();
}
The Select call will achieve what you're after.
You can also look at expanding the value dynamically or use a tooltip for large items..
I explained it here for how to do it for Listbox:
http://blogs.msdn.com/b/sajoshi/archive/2010/06/15/asp-net-mvc-creating-a-single-select-list-box-and-showing-tooltip-for-lengthy-items.aspx