am using c# vs-2005
am on project to create textbox one by one on form1 and am success on button click event my code is below.
// declare location point of textbox on Global Area.
private point txtboxstartpoint=new point(10,10);
private void button_click (Object sender,EventArgs)
{
Textbox tbx = new TextBox();
tbx.Location= txtboxstartpoint;
tbx.size=new size (200,20);
this.panel1.control.add(tbx);
txtboxstartpoint.y += 25;
}
this works fine on button click event but problem is on keypress event like on enter
i wants to create textbox on enter one by one. and for that i assume that any method have
to create and call enter keypress event on newly created control like textbox to create
another new textbox below the previous one.
Kindly help me. suggest proper code for the same.
It's very hard to understand your question, but let's try some guessing:
You have a form, and if a user presses some specific key, you'd like to create a new TextBox and show it on your form regardless which control has currently the focus in your form.
If this statement is true, you can set Form.KeyPreview to true. And add an event handler to Form.KeyDown.
Due to the fact, that you set the preview to true you'll get every keyboard hit before it will be give further to the control that has currently the focus. So here you can check if the key that was pressed is the one you're listening for. And if yes, just call your TextBoxFactory and set the e.Handled to true to prevent that this key stroke will additionally reach the currently focused control.
I use the KeyDown event, to intercept the 'F1' key to provide my small help in a very small programm. Here is the code:
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)
{
//Your Code here
}
}
Related
I am trying to finish my final school project. I am creating a c# winform game to be specific. We can not use anything else.
I will not be posting here code because I got it pretty messed up and I guess u can answer me just with this info.
Setup:
I got my program set up like this. The is main form and two user controls. I switch those controls within the main form during the game. The first one is MENU and the second one is GUI with picturebox acting as a gamescene.
Problem:
Setup quite not important I guess. But what I need to do is to do some action when I press key Down on the first Control (while it is active in the form). Sounds easy I thought at first but the onKeyDown event in the menu.cs(1st usercontrol) is doing nothing when i press the key(The event method is not blank). I tried this.previewKey = true; in the menu load method but it did not even recognize it.
So my question is: Is there any way to use onKeyDown in usercontrols code?
I did it this way becouse I use the same keys in the second controls and i didnt want it to get messy (which obviously did the oposite huh)
TLDR: Need to use onKeyDown event in userControls (keyPreview might be the key)
BONUS
I also need to somehow link variables from Controls 1 to Form and Controls 2.
I looked it up and found out it would be easy in situacion like "Form to Form" but since it is userControls I cant figure it out and I feel like I am just a tiny bit from finishing it.(feels terrible sitting here 9 hours xD please help)
On the keypress event make sure it is for the selected control an not the main form. If you are capturing for the form to determine which key was pressed then use the keypress event for that. You can use a messagebox to verify that you have the right control. Every key has an integer value and you can access and use those by using the properties of e.
Bonus. Depending on how you implemented your code you will have to use either global varibles to pass the data across the forms or use delegates to actively access and set controls on another form
You have to register the event in the Form.Designer.cs :
private void InitializeComponent()
{
// Your form properties here
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
}
You're KeyDown event can be used like this in the Form.cs code :
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// To know if your event is working and the value of the key who's pressed
MessageBox.Show("Key Pressed = "
+ e.KeyCode.ToString()
+ ", Value = "
+ e.KeyValue.ToString());
// Example - add some actions bellow
if (e.KeyValue == 13)
MessageBox.Show("Return Key Pressed");
}
Is there a "generic" event for a Windows Form that will trigger any time the user clicks anything on a form? I need to make sure that a certain text box has input focus at all times, so if the user clicks on a button (or anything else, for that matter), the input focus needs to be redirected to this text box.
Is there an event in the Form class that will handle this, or do I have to handle a Click event to all controls recursively on the form?
I did some digging but I can't find what I want so far.
I actually figured something out - every time the TextBox loses focus, just refocus it.
textBox.LostFocus += TextBox_LostFocus;
private void TextBox_LostFocus(object sender, EventArgs e)
{
textBox.Focus();
}
I'm creating form application on c# . I have dragged a textbox with some text in it.
private void textBox1_Click(object sender, EventArgs e)
{
}
Now what is the event for the onlick on textBox1 ?
I need to add this on that function textBox1.Clear();
P.S I searched everywhere. But all i can find is jquery and javascripts... No c#.
EDIT
I tried onfocus like below..but its not working
private void textBox1_OnFocus(object sender, EventArgs e)
{
MessageBox.Show("dsd");
}
If you want to do something when the control is clicked the handle the Click event, not the TextChanged event. Presumably you just double-clicked the control in the designer. That will only handle the default event. To handle other events, open the Properties window, click the Events button at the top and then double-click the appropriate event.
That said, is Click really appropriate? What if the user enters the TextBox using the Tab key? If what you actually want to do is act when the control gets focus then you should handle the Enter event.
You can handle OnFocus/GotGocus event in the TextBox, and clear the text in the textbox.
Hope this helps.
as you can understand from the title that how to when you press Enter key the button automatically work, how to write code in c# for pressing Enter key instead of clicking on button?
Thank you!!
Is this WinForms? If so, you can set the AcceptButton of the form to be the button in question. Doing so will make pressing Enter behave exactly like clicking the button with the mouse, but it will only have that effect if the currently focused element isn't something else that will also capture the keypress.
I had been trying to solve the same problem. When a button had focus, hitting the enter key did not result in the KeyDown or KeyPress event firing. However, the KeyUp event fired. Problem solved.
private void CmdNoP_KeyUp(object sender, KeyEventArgs e) {
// I do not understand why this works
if (e.KeyCode == Keys.Return) {
cmdNoP_MouseClick(sender, null);
}
}
I believe, [space] is default key, which would "click" active button (or check checkbox and so on). You can always write method for events like OnKeyDown, OnKeyUp or similar. In these events you can check pushed key and do, whats clicking button is doing if this key is [enter].
For WPF set IsDefault="True" in XAML or use the button properties to set IsDefault checkbox.
here's my problem .. i'm doing a calculator in C# and i don't want to click every single button to make a operation, i wanna handle it with my num pad .. like
if i press "1" , show me in the textbox "1".
i changed
private void cmd1_Click(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '1')
{
txtShow.Text='1';
}
}
and i'm having this error :
No overload for 'cmd1_Click' matches delegate "System.EventHandler"
this.cmd1.Click += new System.EventHandler(this.cmd1_Click);
what the hack is wrong with this?
Cheers.
change
this.cmd1.Click += new System.EventHandler(this.cmd1_Click);
to
this.cmd1.KeyPress += new System.EventHandler(this.cmd1_Click);
You'll probably want to rename cmd1_Click too.
And as mentioned in the answer above, this would be better on the Form itself, rather than each button.
You are trying to attach an event handler that corresponds to a KeyPress event to a Click event.
There is something wrong here (bad copy/paste?):
private void cmd1_Click(object sender, KeyPressEventArgs e)
It's named as an auto-generated event handler for the Click event on cmd1, but its definition is the definition for a KeyPress event handler.
Which event do you want to handle? KeyPress or Click or both?
Click is a mouse event, you need to attach to a keyboard event if you want to receive keyboard event args, you'd have to put all your calculator buttons in a common pannel and handle both the button click "and" the text being sent to the panel, that way you could react to both keypresses anywhere and to click for the same result.
An easy way to handling events for all the buttons without doing it one by one is to have a single button click handler and check the text property of the control to know how to act (cast the sender to a button and check the text, do a switch on that)
Not tested:
switch(((button)sender).Text)
{
case "1":
// react to user having pressed 1 etc etc
}