How to perform an action while a key is pressed [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How to make that when pressing a key do an action and when you press it again, stop doing it in C#.
What I want to do: I have a program which the user must press F1 to activate any action, well; so that the user does not press another key, but the same one, how would this be done in C #? Since I currently stop the action by doing the following:
if (GetAsyncKeyState(Keys.F1) == -32767)
{
timer1.Start();
}
if (GetAsyncKeyState(Keys.F2) == -32767)
{
timer1.Stop();
}

You can use System.Timers.Timer.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Key.A)
{
if(!timer1.Enabled)
timer1.Start();
else
timer1.Stop();
}
}

Related

Loop for click event in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have multiple text boxes on a form and I want to display the name of the textbox when I click on it in a specific textbox. As I have 300 textboxes I dont want to create 300 click Events. Do I need some kind of loop?
Yes, exactly, iterate through all your Controls on your Form and if Control is a TextBox, subscribe to an event like this:
private void subScribeAllTextBoxClickEvents()
{
foreach(var ctrl in this.Controls)
{
var textBox = ctrl as TextBox;
if(textBox != null)
{
textBox.Click += textBox_Click;
}
}
}
private void textBox_Click(object sender, EventArgs e)
{
}
You could call subScribeAllTextBoxClickEvents method for example in your constructor.

Do we have to repeat our code in a WinForms application? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
So I'm trying to switch from C# console to form, but I always read that never repeat our code etc.
My first project would be a calculator, and I found a site just to take a look how it looks like in win form, but in this code there are a lot of repeating. Is this normal in form, in let's say a calculator?
Here is the link that I am talking about.
That is a lot of repetition, to improve it add one click event handler for all of the buttons, eg:
btn1.Click += btnClick;
btn2.Click += btnClick;
Then cast the sender to a Button to get which one was clicked, a rough example:
private void btnClick(object sender, EventArgs e)
{
var btnName = ((Button)sender).Name;
var btnValue = btnName.Replace("btn",string.Empty);
if (textBox1.Text == "0" && textBox1.Text != null)
{
textBox1.Text = btnValue;
}
else
{
textBox1.Text = textBox1.Text + btnValue;
}
}
Don't forget to unhook the event subscriptions in the form unload event:
btn1.Click -= btnClick;
btn2.Click -= btnClick;

Add additional space when fullstop were press C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a richtextbox and I want to add a space in between every time I press "."(fullstop).
It should automatically add/insert a space(without pressing the spacebar) after I press fullstop.
This will add a space after a Fullstop(.) has been pressed. You need to use the KeyUp Event.
private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.OemPeriod)
richTextBox1.Text += " ";
richTextBox1.SelectionStart = richTextBox1.Text.Length;
}
You can create a method to handle richTextBox.OnKeyUp event so that if the key that is pressed is the fullstop then append your text with the space.
private void RichtextBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
// Determine whether the key entered is the period key. Append a space to the textbox if it is.
if(e.KeyCode == Keys.OemPeriod)
{
RichTextBox1.Text += " ";
}
}
Obviously you will have to create this event for your own richTextBox rather than my example of "RichTextBox1"

Disable Enter Key in Textboxes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am developing a chatting system. And I have a Button to send the text in the text box to the chat log. How am I going to stop the user from press Enter key and to disable the Enter key. I know there are many posts out there like this, but the solutions haven't worked for me.
You can try something like this:-
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
}
I think you do not need to stop the user from pressing enter but instead send the chat to the other person on press of enter.
Also if you have any other shortcuts to be allowed then you can have a look at this C#: How to make pressing enter in a text box trigger a button, yet still allow shortcuts such as "Ctrl+A" to get through?
Using the same you can also block
private void textBoxToSubmit_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress=true;
}
}
Your question is a little ambiguous to say the least; however, the textbox control has an event called KeyDown : http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx
This way, you can capture whenever Enter is pressed and modify and behavior as needed, here is an example
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (Keys.Enter == e.KeyCode)
{
MessageBox.Show("Enter Was Pressed");
textBox1.Text = new String(textBox1.Text.Where((ch, i) => i < textBox1.Text.Length - 2).ToArray());
textBox1.SelectionStart = textBox1.Text.Length;
}
}

C# ComboBox Usage [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Google has not helped me on this one..
Say I have a combobox, with the values "X" and "Y".
What is the syntax to say..
"If the user selected X, do this, else do that."
I've tried several ways.. none work.
Thanks in advance.
I'll assume you're using WinForms, the property you're looking to use is ComboBox.Text.
Something like:
if (xyCombo.Text == "X")
// Do something
else (xyCombo.Text == "Y")
// Do something else
You have to subscribe to ComboBox's SelectedIndex changed event. Please refer to the below link.
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged.aspx
Try combining the above answers, like this.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.Text == "X")
//Action
else
//Other Action
}
or use a switch statement
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox1.Text)
{
case "X":
//Action
break;
case "Y":
//Another Action
break;
default:
break;
}
}

Categories