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"
Related
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 2 years ago.
Improve this question
Using a Windows Form I am trying to replicate the functionality of a "Tab Control" but instead of the panel selection being controlled by a tab selection it would be controlled by a list. Is there a built in way to produce this using neatly like the tab control or should I just check if a list value is equal to some value and if yes display panel else don't?
This is probably a dumb question so sorry for wasting anyone's time and thanks in advance!
Besides, you can also use tabpages in tabcontrol as "panel". Just hide the header via the code
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.Appearance = TabAppearance.FlatButtons;
tabControl1.ItemSize = new Size(0, 1);
tabControl1.SizeMode = TabSizeMode.Fixed;
foreach (TabPage tab in tabControl1.TabPages)
{
tab.Text = "";
}
}
Then select the tabpage like,
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
if(((ListBox)sender).SelectedItem.ToString() == "tabPage2")
{
tabControl1.SelectedTab = tabPage2;
}
//...
}
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.
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;
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();
}
}
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;
}
}