Disable Enter Key in Textboxes [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 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;
}
}

Related

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;

How to perform an action while a key is pressed [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
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();
}
}

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"

How to prevent data duplication in a datagridview [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
private void moviesGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
MovieDetailsForm form = new MovieDetailsForm(MovieDetailsForm.MovieViewMode.Read);
if (e.ColumnIndex==5)
{
form.ShowDialog();
}
}
I am trying to view the details of a movie when I press the view details button in the datagridview but for some reason I can't get it to work.
The place of the buttons in the datagridview is 5.
I'd show a ss but unfortunately I cant, yet.
The place of the buttons in the datagridview is 5
It means that the column is the fifth column?
If yes, don't forget that index in .Net are generally zero-based index. So it would be:
if (e.ColumnIndex==4)
Also, good remark from KyleMit, don't create an instance of MovieDetailsForm if you don't use it:
if (e.ColumnIndex==4)
{
MovieDetailsForm form = new MovieDetailsForm(MovieDetailsForm.MovieViewMode.Read);
form.ShowDialog();
}
Just to summarize what others have said and to help out your coding style...
private void moviesGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns["colDetailButton"].DisplayIndex == e.ColumnIndex)
{
// my guess is you also need other data, like the movie's IMDB number
string imdbValue = dataGridView1.Rows[e.RowIndex].Cells["colImdbValue"].Value.ToString();
using (var form = new MovieDetailsForm(MovieDetailsForm.MovieViewMode.Read))
{
form.ImdbValue = imdbValue;
form.ShowDialog();
}
}
else
{
// Remove this debugging code once you get your code working
Console.WriteLine("ColumnIndex {0} was clicked." e.ColumnIndex);
}
}
See this answer as to how to How to handle click event in Button Column in Datagridview? for a good overview of what to do. So long as you only have a single button, you actually don't have to specify the column index at all, which makes your code less fragile to change. Although, Chris is right, that indexes are zero based so you'd need a ColumnIndex of 4 to get the 5th column. You also don't have to new up your form unless you actually want to show it, so I'd move the declaration into the if statement like this:
private void moviesGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//make sure click not on header and column is type of ButtonColumn
if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == _
typeof(DataGridViewButtonColumn))
{
MovieDetailsForm form = new MovieDetailsForm(MovieDetailsForm.MovieViewMode.Read);
form.ShowDialog();
}
}

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