I have a textbox, a button, and a progressbar. I would like the keydown event of the textbox therefore the entry, is found using a button in the progressbar, in fact I would like the number entered in the textbox to become the value of the progressbar and that in this case the progressbar value increases. If you could put me a sample code, that would be nice, thank you.
public void button1_Click(object
sender,EventArgs e)
{
//I don't know.
}
public void textbox2_KeyDown(object
sender,KeyEventArgs e)
{
if(e.KeyCode <= Keys.NumPad0 ||
e.KeyCode >= Keys.NumPad9)
{
// I don't know !
}
As I understood, you want to change the progress bar value based on the textBox input and detect that change.
Well, to get the input from the textBox on change, there is an event for that which called:
TextChanged
so with every change in the input, you'll be able to capture the input.
For the progressbar, you can change the value by the property: Value.
Read more:
ProgressBar.Value
so if your progressBar control named: progressBar1, change it by:
progressBar1.Value = 5; // here the value is 5.
Now, you can use textChanged event to set the progress bar value like:
public void textbox2_TextChanged(object sender,KeyEventArgs e)
{
progressBar1.Value = int.Parse(textBox2.Text); // parse the input to int value. Consider using int.TryParse
}
On KeyDown capture the keyValue
int keyValue { get; set; }
private void textbox2_KeyDown(object sender, KeyEventArgs e)
{
int keyVal = (int)e.KeyValue; keyValue = -1;
if ((keyVal >= (int)Keys.D0 && keyVal <= (int)Keys.D9))
{
keyValue = (int)e.KeyValue - (int)Keys.D0;
}
else if (keyVal >= (int)Keys.NumPad0 && keyVal <= (int)Keys.NumPad9)
{
keyValue = (int)e.KeyValue - (int)Keys.NumPad0;
}
}
On Button Click Increase the ProgessBar value
public void button1_Click(object sender,EventArgs e)
{
//Here Increment the progress bar by keyValue
progressBar1.Value += keyValue;
}
KeyValues and KeyCodes
Related
I want to make it work this way: when I write to NumericUpDown 1k the value should be 1000, when I write 4M the value should be 4000000. How can I make it?
I tried this:
private void NumericUpDown1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyValue == (char)Keys.K)
{
NumericUpDown1.Value = NumericUpDown1.Value * 1000;
}
}
But it works with the original value that I wrote.
I want to make it work as a macroses. For example, If I want to get NUD1.Value 1000, I write 1 and then, when I press K the NUD1.Value becomes 1000.
Let's assume we have a NumericUpDown named numericUpDown1. Whenever the user presses k, we want to multiply the current value of the NUP by 1,000, and if the user presses m, the current value should be multiplied by 1,000,000. We also don't want the original value to trigger the ValueChanged event. So, we need to have a bool variable to indicate that the value is being updated.
Here's a complete example:
private bool updatingValue;
private void numericUpDown1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData != Keys.K && e.KeyData != Keys.M) return;
int multiplier = (e.KeyData == Keys.K ? 1000 : 1000000);
decimal newValue = 0;
bool overflow = false;
try
{
updatingValue = true;
newValue = numericUpDown1.Value * multiplier;
}
catch (OverflowException)
{
overflow = true;
}
updatingValue = false;
if (overflow || newValue > numericUpDown1.Maximum)
{
// The new value is greater than the NUP maximum or decimal.MaxValue.
// So, we need to abort.
// TODO: you might want to warn the user (or just rely on the beep sound).
return;
}
numericUpDown1.Value = newValue;
numericUpDown1.Select(numericUpDown1.Value.ToString().Length, 0);
e.SuppressKeyPress = true;
}
And the ValueChanged event handler should be something like this:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
if (updatingValue) return;
// Simulating some work being done with the value.
Console.WriteLine(numericUpDown1.Value);
}
Below is some of my code that isn't working. I wanted to see it could count the number of spaces in the text box so I'm having it display the number in a message box and it currently just throws a blank one. The problem is either in the float to string conversion or in the counter.
private static string _globalVar = "n";
public static string GlobalVar
{
get { return _globalVar; }
set { _globalVar = value; }
}
public Form1()
{
InitializeComponent();
button1.Text = "Enter";
}
string LNum { get; set; }
public void button1_Click_1(object sender, EventArgs e)
{
MessageBox.Show(LNum);
}
public void richTextBox1_TextChanged(object sender, System.Windows.Forms.KeyEventArgs e)
{
float n = 0;
if (Control.ModifierKeys == Keys.Space)
{
n = n + 1; ;
}
string.Format("{0:N1}", n);
string LNum = Convert.ToString(n);
}
What you are doing is an excellent way to learn how and when the events are raised and how they can be leveraged to customize an applications behavior and is something similar to what many of us have done over the years.
Based on what you say you are wanting to do, there are several issues. If you take a look at this code, it will do what you want.
public void button1_Click(object sender, EventArgs e)
{
int n = 0;
for (int counter = 0; counter < richTextBox1.TextLength; counter++)
{
if (richTextBox1.Text[counter] == ' ')
{
n++;
}
}
MessageBox.Show(n.ToString("N1"));
}
The key difference is that I am only looking at the entered text when the button is clicked. (TextChanged is run every time there is a change in the text displayed). I chose not to use a float variable to store the count of spaces as the count will always be an integer.
In addition, the parameter to TextChanged System.Windows.Forms.KeyEventArgs e is incorrect and will never compile if correctly bound to the TextChanged event.
The KeyEventArgs parameter is used by the KeyUp and KeyDown events. If you use these events, you will be looking to count every time the space bar is pressed and not the number of spaces in the text box. And like their name suggests, the events are raised every time a Key on the keyboard goes Up (Pressed) and Down (Released).
I have a textbox in a Windows form, I have clear button which basically clears the text char by char(that is one by one not all at once), my problem is i want to add another textbox to this form and would like to control both the textboxes with clear button meaning the clear should only clear the textbox which i have selected or clicked on, i tried doing it but either i am able to clear both the textboxes simultaneously or clear only textbox my code for single textbox is
private void clearBtn_Click(object sender, EventArgs e)
{
string s = txtID.Text;
if (s.Length > 0) txtID.Text = s.Substring(0, s.Length - 1);
}
You can set the which control has focus upon focus and then use that to see which one needs to be removed.
private Textbox SelectedTextBox;
protected void Form_Load(object sender, EventArgs e)
{
TextBox1.GotFocus += TextBox_GotFocus;
TextBox2.GotFocus += TextBox_GotFocus;
}
private void clearBtn_Click(object sender, EventArgs e)
{
if(this.SelectedTextBox == null) return;
string s = this.SelectedTextBox.Text;
if (s.Length > 0) this.SelectedTextBox.Text = s.Substring(0, s.Length - 1);
}
private void TextBox_GotFocus(object sender, EventArgs e)
{
this.SelectedTextBox = (Textbox)sender;
}
private void clearBtn_Click(object sender, EventArgs e)
{
sting textbox1text = textbox1.tostring();
string textbox2text = textbox2.tostring();
if (textbox1text.length > 0){
textbox1text = textbox1text.Remove(textbox1text.length - 1)
}
if (textbox2text.length > 0){
textbox2text = textbox2text.Remove(textbox1text.length - 1)
}
}
that will "backspace" one character of each textbox.
if you want to do a clear on the one you last updated textbox, add this to each if statement
if (textbox2text.length > 0 && textbox2text == textbox2text)
that will check if the textbox was updated before it clears, it wont clear if it hasnt been updated
I'm new to C#. Using the code below, whenever I press a number key on my keyboard, it will display twice in the textbox. When I press "1" on the keyboard it will display
"11", and when I press "2" it will display "22". Why is this?
private void Window_TextInput(object sender, TextCompositionEventArgs e)
{
if(!isNumeric(e.Text))
{
string display = string.Empty;
display += e.Text;
displayNum(display);
}
else
{
String inputOperator = string.Empty;
inputOperator += e.Text;
if (inputOperator.Equals("+"))
{
ApplySign(sign.addition, "+");
}
}
}
private bool isNumeric(string str)
{
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("[^0-9]");
return reg.IsMatch(str);
}
private void window_keyUp(object sender, KeyEventArgs e)
{
if (e.Key >= Key.D0 && e.Key <= Key.D9)
{
int num = e.Key - Key.D0;
outputText2.Text += num;
}
}
private void BtnNum_Click(object sender, RoutedEventArgs e)
{
Button num = ((Button)sender);
displayNum(num.Content.ToString());
}
private void displayNum(String n)
{
if (operator1 == 0 && double.Parse(n) == 0)
{
}
else
{
if (operator1 == 0)
{
outputText2.Clear();
}
outputText2.Text += n;
operator1 = double.Parse(outputText2.Text);
outputText2.Text = Convert.ToString(operator1);
}
}
You have two events that are handeling the Keyboard events. Although not really sure what the displayNum() method is doing
I am assuming the Window_TextInput event is the event you wish to primarily handle the event.
Try adding
e.Handled = true;
In the Window_TextInput method. If that doesn't solve the problem can you post the displayNum() method?
EDIT:
After further review of the code and trying the same I do not see the relevance for the window_keyUp method as your Window_TextInput handles the input characters and has more applicable logic for handling the TextInput changes.
After I removed the window_keyUp event method the output appeared as expected (although commented out the ApplySign() method.
You've subscribed to two window-level text-related events - TextInput and KeyUp - and both of them end up appending input to the TextBox.
window_keyUp appends numbers to the TextBox
It looks like Window_TextInput is supposed to append non-numeric characters, but your RegEx is incorrect ([^0-9] matches anything that is not numeric, so IsNumeric returns True if the input is not a number)
The effect is that every numeric key press shows up twice.
I am new to C sharp programming. I need to make a change in our project. Basically we are using Xeed datagrid, which has 4 columns. Data is bound with the collection object and was updated dynamically with DB call. My question is out of 4 columns, 1 column is editable. when user make a change in this column and hit enter, the focus needs to change to below cell in the same column in the edit mode. Following is the KeyUp event I am writting. After I make change this columna nd hit enter the focus is going to next row, but the edit mode is not going to next cell, but instead stays on the same cell which was eddited.
private void _dataGrid_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
_dataGrid.EndEdit();
int currentRow = _dataGrid.SelectedIndex;
currentRow++;
_dataGrid.SelectedIndex = currentRow;
_dataGrid.Focus() ;
_dataGrid.BeginEdit();
}
}
I think you need to change CurrentItem property. Iam using different grid control so I not guaranty that it will work. But procedure should be something like this:
private void _dataGrid_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
_dataGrid.EndEdit();
int nextIndex = _dataGrid.SelectedIndex + 1;
//should crash when enter hit after editing last row, so need to check it
if(nextIndex < _dataGrid.items.Count)
{
_dataGrid.SelectedIndex = nextIndex;
_dataGrid.CurrentItem = _dataGrid.Items[nextIndex];
}
_dataGrid.BeginEdit();
}
}
Following the solution
private void _dataGrid_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
int rowCount = _dataGrid.Items.Count;
int currentRow = _dataGrid.SelectedIndex;
if (rowCount - 1 > currentRow)
currentRow++;
else
currentRow = 0;
_dataGrid.CurrentItem = _dataGrid.Items[currentRow];
_dataGrid.BringItemIntoView(_dataGrid.Items[currentRow]);
}
}