Basically, what I am trying to achieve here is that if a 'button' that I have created is pressed, it prints a character on a 'text block' and soon as I release it, it prints another on it. This is a small part of an application that I am building. But, the problem is my events never get executed, it works fine when I use Click event for the button though, but, it doesn't solve the purpose. So, I need some help with this. I read online that I have to set up Control.IsInputKey method but, I am not able to set it up being a newbie. So, please help me through it and I'll be grateful to you for making my work easier.
This is the code that I am using in this context where 'command' is the name assigned to the TextBlock and 'test' is the name assigned to my Button.
private void test_KeyDown(object sender, KeyEventArgs e)
{
command.Text = "Y";
}
private void test_KeyUp(object sender, KeyEventArgs e)
{
command.Text = "N";
}
Well, first you need to deside on which key you want theese events to fire. Your code right now, does nothing since you don't check which key is pressed. To do so you need to do this:
private void test_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == System.Windows.Input.Key.SomeKey)
command.Text = "Y";
}
private void test_KeyUp(object sender, KeyEventArgs e)
{
if(e.Key == System.Windows.Inpput.Key.SomeKey)
command.Text = "N";
}
Related
private void MeretOK_Click(object sender, EventArgs e)
{
//code
}
private void MeretTB_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode==Keys.Enter)
{
MeretOK_Click();
}
}
How can I start an event with a hotkey?
(I know I can just copy the code there but that is ugly)
If I just copy the code and run it I hear a beep. Why?
First, you have to allow your form to handle key events globally (set the form KeyPreview property to true) and to mark your event as internally handled. Second, if the purpose of this code is to simulate the mouse click on a specific control (programmatic click), for example a button, there is an easier way to accomplish it:
private void MeretTB_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
myButton.PerformClick();
}
}
I'm a beginner and have an assignment in which I must program the game of NIM. I begin with 15 "tokens" and at each turn a maximum of three can be removed, or "hidden". So far I am hiding these tokens on click by doing the following.
private void button1_Click(object sender, EventArgs e)
{
button1.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Visible = false;
}
I simply copied and pasted that multiple times and changed the button numbers so that my buttons will close on click. This might be obvious, but is there a more efficient way to do this, instead of having 15 button close methods?
You can use the same click event for every single button, and make use of the sender object, casting it to Button:
private void buttonsToClose_Click(object sender, EventArgs e)
{
((Button)sender).Visible = false;
}
Then just add that handler to every single button you want to close itself on click.
Note, though, that this will throw an InvalidCastException if you or anyone else uses this handler on an object that is not a Button, so if you're actually going to use this code I would add some sort of conditional to check the real type of the sender.
Additionally, you could reuse this for any Control object by casting sender to Control instead, given that Button inherits from Control, and all Control objects have the Visible property. Here's an example, with a conditional to guard against an invalid cast:
private void controlToMakeInvisible_Click(object sender, EventArgs e)
{
if (sender.GetType() == typeof(Control))
{
((Control)sender).Visible = false;
}
}
A final note - it seems from your post like you may have a slight misunderstanding about the way events are created and wired in with objects in Windows Forms. If you go into the Designer, add a click event, and see it pop into your Form code as follows:
private void button1_Click(object sender, EventArgs e)
the name of this method has no bearing on its function. The button1 part of button1_Click doesn't actually have any logical linkage with the Button button1 - it's just the default name assigned by the Designer. The actual assignment of the method button1_Click to the Button.Click event is auto-generated into your Form's Designer.cs method.
The point of this is that if you copy and paste button1_Click and change every incidence of button1 with button2, like so:
private void button2_Click(object sender, EventArgs e)
{
button2.Visible = false;
}
it's not going to fire when button2 gets clicked. In actual fact, it's never going to fire at all, because the method hasn't actually been connected to any controls/events.
just call your event in a foreach loop.
private void Form1_Load(object sender, EventArgs e)
{
foreach (var button in Controls.OfType<Button>())
{
button.Click += button_Click;
}
}
void button_Click(object sender, EventArgs e)
{
((Control) sender).Visible = false;
}
if you change:
Controls.OfType<Button>()
to
Controls.OfType<Control>()
it will set visible to false for any Control. so you can control what item you want the event to be raised for easily.
OfType summary: Filters the elements of an IEnumerable based on a specified type.
I have created many groupbox staked one on top of the other and made them to be invisible however ,if i call the first group box it shows but if i call the other groupboxes they will not show
private void buttonFinish_Click(object sender, EventArgs e)
{
groupBox1.Show();// is showing
}
private void buttonFinish_Click(object sender, EventArgs e)
{
groupBox2.Show();// is not showing
}
The second related problem is that if i try an if statement it the selected if statement shows nothing at all
private void buttonFinish_Click(object sender, EventArgs e)
{
if (comboBoxType.Text == "Car" && comboBoxName.Text == "BMW"
" && radioButtonBlack.Checked){
if (checkBoxTwoseater.Checked || radioButtonLeather.Checked ||
radioButtonBooster.Checked ){
groupBox1.show}
I cannot insert an image because i am new
Well, you said they are all on top of each other, so what is likely happening is the second call works just fine, but it is actually appearing behind the first one.
What you need to write is:
private void buttonFinish_Click(object sender, EventArgs e)
{
groupBox1.Show();// is showing
}
private void buttonFinish2_Click(object sender, EventArgs e)
{
groupBox1.Hide();
groupBox2.Show();// is showing now!
}
Note that this code is odd, since both methods appear to be named the same. I changed that to make "compilable" code, but you should check yours to make sure that it isn't causing a problem.
Show just sets the Visible property to true, it does not affect the Z-Order (see MSDN)
The second problem will require you stepping through the code with a debugger and checking all your conditions, you haven't given enough information for us to help.
I have added a keyPress event on a ListView. With a breakpoint on my event, I can see that most of the keys trigger the event. However, a few among which, the one I'm interested in (delete), just won't trigger my event.
Is that weird ? And no, there's no broken keys on my keyboard :D
private void listView1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Delete)
{
ListView target = (ListView)sender;
if (target.SelectedIndices != null && target.SelectedIndices.Count > 0)
{
string ric = target.SelectedItems[0].SubItems[0].Text;
//target.Items.RemoveAt(target.SelectedIndices[0]);
ListModels.getInstance().getModel("Vols").removeRic(ric);
}
}
}
The reason for this is that the KeyPress event sends a character to the control based upon the character-key you press. However, as you'd expect, the delete key does not represent a character and is thus a non-character key.
Therefore using the KeyPress event will do nothing as you have noticed. You should use the KeyDown or KeyUp Events, either of which will work absolutely fine. The nuance being whether you want your event to fire upon pressing, or letting go of a key.
You'll want to use the KeyDown event for this.
In KeyDown use the condition as follows,
if (e.KeyCode == Keys.Delete)
{
// Your Logic....
}
Use keyDown instead; keyPress is something like a full keyDown + keyUp
The problem is that if you set EditMode property to EditOnEnter it won't fire. If you use EditOnKeyStrokeOfF2 it will fire the event
If you are looking for a solution where the user should only be able to choose from the defined items, then I believe you can do it with this:
private void DropDownRank_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
}
See this code:
private void Form1_Load(object sender, EventArgs e)
{
listView1.KeyUp += new KeyEventHandler(ListView_KeyUp);
}
/// <summary>鍵盤觸發 ListView 清單</summary>
private void ListView_KeyUp(object sender, KeyEventArgs e)
{
ListView ListViewControl = sender as ListView;
if (e.KeyCode == Keys.Delete)
{
foreach (ListViewItem eachItem in ListViewControl.SelectedItems)
{
ListViewControl.Items.Remove(eachItem);
}
}
}
I tried all the stuff mentioned above but nothing worked for me, so im posting what i actually did and worked, in the hopes of helping others with the same problem as me:
Add an event handler in the constructor:
public partial class Test
{
public Test()
{
this.RemoveHandler(KeyDownEvent, new KeyEventHandler(Test_KeyDown));
// im not sure if the above line is needed (or if the GC takes care of it
// anyway) , im adding it just to be safe
this.AddHandler(KeyDownEvent, new KeyEventHandler(Test_KeyDown), true);
InitializeComponent();
}
//....
private void Test_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
//your logic
}
}
}
I'm trying to make it so no matter what, when I push Space, a certain block of code is executed (cmd_play, to be exact).
However, it only seems to work once if you do it using Form Keypress:
private void frmmain_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToChar(Keys.Space))
cmdPlay_Click(null, null);
}
any ideas?
Try to set the Handled property of KeyPressEventArgs to true. Not sure if it'll fix your issue but it is good form. More info here
private void frmmain_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == Convert.ToChar(Keys.Space))
{
cmdPlay_Click(null, null);
e.Handled = true;
}
}
If that doesn't work then keyboard event hooks definitely will. Though hooking events is a much larger and more dangerous undertaking.
Are you sure that cmdPlay_Click() isn't the problem? I.E. the event handler IS getting called multiple times but cmdPlay_Click() is only playing once?