How can I ensure that when you click on label, application expect to press any key, and when user press key, label text change to this key char?
Ok, now i have:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool isLabelClicked = false;
private void label1_Click(object sender, EventArgs e)
{
isLabelClicked = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (isLabelClicked)
{
label1.Text = ((char)e.KeyValue).ToString();
isLabelClicked = false;
}
}
}
And if I press Up, Down, Left, Right, Tab or Enter, application not responding and still expects press key. Only when I press any other key, application works well.
If I create new project and paste the same code, all works well, for Up, Down, Left and Right also, but I need this keys for my application which I do.
This example a textBox class : when press any key show it
using System;
using System.Windows.Forms;
using System.Drawing;
namespace KeyPressDisplayTextBox {
public partial class Form1 : Form {
private TextBox textBox1;
private Label label1;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
textBox1 = new TextBox();
textBox1.Location = new Point(10,10);
textBox1.KeyPress += textBox1_KeyPress;
Controls.Add(textBox1);
label1 = new Label();
label1.Location = new Point(10, 40);
label1.BorderStyle = BorderStyle.FixedSingle;
label1.Font = new Font("Arial", 14);
Controls.Add(label1);
}
void textBox1_KeyPress(object sender, KeyPressEventArgs e) {
label1.Text = e.KeyChar.ToString();
}
}
}
Best regards
You can handle KeyDown event on the Form and get the KeyValue as below
Try This:
You need to set the Form KeyPreview property to True to receive the key events from the Form
bool isLabelClicked = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (isLabelClicked)
{
label1.Text = ((char)e.KeyValue).ToString();
isLabelClicked = false;
}
}
private void label1_Click(object sender, EventArgs e)
{
isLabelClicked = true;
}
Related
I have 2 forms. Form1 and Form5. Form1 has 2 buttons which open Form5. I want to change the visibility of Form5's button after it is shown. I have set Form5 button's modifiers to public and I've also tried this code below but it's not working:
public void button1_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5();
fr5.button1.Visible = true;
fr5.ShowDialog();
}
public void button2_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5();
fr5.button1.Visible = false;
fr5.ShowDialog();
}
Edit: I've set button1.Visible = true; in Form5 Load event.
Thank you for your suggestions. :) I have solved the problem by changing the code like this:
public void button1_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5();
fr5.Show(this);
fr5.button1.Visible = true;
}
public void button1_Click(object sender, EventArgs e)
{
Form5 fr5 = new Form5();
fr5.Show(this);
fr5.button1.Visible = false;
}
//Hi,
I want to write a Software and am confused about "keeping the buttom selected (i mean the background color) when button is clicked, until i click on another one"
If someone could help me, i would appreciate that so much.
Thanks in advance//
namespace SoftwareUI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button1.ForeColor = Color.LightGray;
}
private void button1_Leave(object sender, EventArgs e)
{
button1.ForeColor = Color.GhostWhite;
}
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.ForeColor = Color.LightSlateGray;
}
}
}
Add this code for click event of all buttons:
Button b = (Button)sender;
b.BackColor = Color.LightGray;
foreach (Button bt in b.Parent.Controls.OfType<Button>())
{
if (bt != b)
bt.BackColor = Color.White;
}
I want to design a windows form using C sharp on Visual Studio 2013.
I go through the Source from here. but did not got it properly.
for that I have 3 combobox. I want to disable combobox2 when I click on combobox1 NSSCM element and enable when click on NSSFO element.
Below is my part of code snippet:
namespace NSE_First_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MaximizeBox = false;
MinimizeBox = false;
if (true)
{
comboBox1.Items.Add(Exchange.NSSCM.ToString());
comboBox1.Items.Add(Exchange.NSSFO.ToString());
comboBox1.Items.Add(Exchange.BSSCM.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
string selectedItem = string.Empty;
ProcessValue(selectedItem);
}
public enum Exchange
{
NSSCM = 1,
NSSFO = 2,
BSSCM = 3
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
Try this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
comboBox2.Enabled = false;
if (comboBox1.selectedIndex == 1)
comboBox2.Enabled = true;
}
Try this:
//This will disable combobox2 on the click of it
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox2.Enabled = false;
}
//This will enable combobox2 on the click of it
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox2.Enabled = true;
}
Because you want it on click, use the CLICK event, instead of SelectedIndexChange event.
Firstly I had been searching about my problem and can't find \help.
So my question is I've got 3 buttons and three userControl and when I click on one button it displays usercontrol 1 but after I click button 2. I cannot get back to usercontrol 1 im stuck in usercontrol2 and the button 1 does not do anything anymore.
Here's my code:
public partial class Form2 : Form
{
UserControl1 u1;
UserControl2 u2;
UserControl3 u3;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
u1 = new UserControl1();
u1.Dock = DockStyle.Fill;
this.Controls.Add(u1);
}
private void button2_Click(object sender, EventArgs e)
{
u1.Hide();
u2 = new UserControl2();
u2.Dock = DockStyle.Fill;
this.Controls.Add(u2);
}
private void button3_Click(object sender, EventArgs e)
{
u1.Hide();
u2.Hide();
u3 = new UserControl3();
u3.Dock = DockStyle.Fill;
this.Controls.Add(u3);
}
private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
SOLVED CODE for other's who need :)
--->
enter code here
public partial class Form2 : Form
{
UserControl1 u1;
UserControl2 u2;
UserControl3 u3;
public Form2()
{
u1 = new UserControl1();
u2 = new UserControl2();
u3 = new UserControl3();
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
u2.Hide();
u3.Hide();
u1.Show();
u1.Dock = DockStyle.Fill;
this.Controls.Add(u1);
}
private void button2_Click(object sender, EventArgs e)
{
u1.Hide();
u3.Hide();
u2.Show();
u2.Dock = DockStyle.Fill;
this.Controls.Add(u2);
}
private void button3_Click(object sender, EventArgs e)
{
u1.Hide();
u2.Hide();
u3.Show();
u3.Dock = DockStyle.Fill;
this.Controls.Add(u3);
}
private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
It seems that you should have:
u1=new UserControl1();
u2=new UserControl2();
u3=new UserControl3();
in the constructor public Form2() rather than in the event handlers. This will allow you to add
u2.Hide();
u3.Hide();
in your button1_Click handler.
You should probably also add u3.Hide() to button2_Click.
Take a look at this. I think it's absolutely clear and didn't need any further explanation.
public partial class Form1 : Form
{
private UserControl1 uc1 = new UserControl1();
private UserControl2 uc2 = new UserControl2();
private UserControl3 uc3 = new UserControl3();
public Form1()
{
InitializeComponent();
AssignedButtonClickEvents();
}
private void Form1_Load(object sender, EventArgs e)
{
}
protected void ButtonClicked(object sender, EventArgs e)
{
Button button = sender as Button;
panel1.Controls.Clear();
if (button != null)
{
switch (button.Name)
{
case "button1":
uc1.Dock = DockStyle.Fill;
panel1.Controls.Add(uc1);
break;
case "button2":
uc2.Dock = DockStyle.Fill;
panel1.Controls.Add(uc2);
break;
case "button3":
uc3.Dock = DockStyle.Fill;
panel1.Controls.Add(uc3);
break;
default:
panel1.Controls.Clear();
break;
}
}
}
public void AssignedButtonClickEvents()
{
foreach (Control ctl in this.Controls)
{
if (ctl is Button)
{
Button button = (Button)ctl;
button.Click += new EventHandler(ButtonClicked);
}
}
}
edit
note that i create a panel to store the usercontrols, but i think it's the same if you show your user controls directly on the windows forms. you only need to hide your controls.
If you use BringToFront() property when clicking the button, the functionality would happen. This method will be effective for response button clicks other than Hide() property.
private void buttonProdutsList_Click(object sender, EventArgs e)
{
productsListView.BringToFront();
}
My question is:
How can I disable the hold a key down in a textbox control using C#?
e.g Boxxxxxxxxxxxxxxxxxxxxx
I don't want to allow user to repeat any character from the keyboard.
Any suggestion?
thanks
Just an extra note for everyone using KeyDown and KeyUp events to suppress key repeat. If you do this you need to exclude catching meta keys like Alt/Shift etc otherwise when those keys are held down the actual key you want will not be sent since this is a two key combination and KeyDown and KeyUp events catch all keys.
This applies to all two-key combinations.
I did not add all of the meta keys to the following example, just the most common ones.
You can easily add your own keys by adding them to the collection.
Extending on the post from BFree:
public partial class Form1 : Form
{
private static readonly System.Collections.Generic.ICollection<System.Windows.Forms.Keys) ExcludeKeys = new System.Collections.Generic.HashSet<System.Windows.Forms.Keys)()
{
System.Windows.Forms.Keys.None,
System.Windows.Forms.Keys.Shift,
System.Windows.Forms.Keys.ShiftKey,
System.Windows.Forms.Keys.LShiftKey,
System.Windows.Forms.Keys.RShiftKey,
System.Windows.Forms.Keys.Alt,
System.Windows.Forms.Keys.Control,
System.Windows.Forms.Keys.ControlKey,
System.Windows.Forms.Keys.LControlKey,
System.Windows.Forms.Keys.RControlKey,
System.Windows.Forms.Keys.CapsLock,
System.Windows.Forms.Keys.NumLock,
System.Windows.Forms.Keys.LWin,
System.Windows.Forms.Keys.RWin
}
private bool isKeyPressed = false;
public Form1()
{
InitializeComponent();
this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
this.textBox1.KeyUp += new KeyEventHandler(textBox1_KeyUp);
}
void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (!ExcludeKeys.Contains(e.KeyCode))
{
isKeyPressed = false;
}
}
void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (!ExcludeKeys.Contains(e.KeyCode))
{
e.SuppressKeyPress = isKeyPressed;
isKeyPressed = true;
}
}
}
You can use the SupressKeyPress property on KeyEventArgs:
public partial class Form1 : Form
{
private bool isKeyPressed = false;
public Form1()
{
InitializeComponent();
this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
this.textBox1.KeyUp += new KeyEventHandler(textBox1_KeyUp);
}
void textBox1_KeyUp(object sender, KeyEventArgs e)
{
isKeyPressed = false;
}
void textBox1_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = isKeyPressed;
isKeyPressed = true;
}
}
bool prevent = false;
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = prevent;
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
prevent = false;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
prevent = true;
}
I'm not familiar with the specifics in c# but I imagine something like:
while (keyIsDown){
//do nothing
}
would work. I would be surprised if there is no way to keep track of a key being pressed.