I am trying to build a windows application using WinForms and C#, in one of the forms i want the user to be able to assign keys for each movement (i.e. left , right , up ,down motion etc.). That is something similar to
On the left hand side column the moves will be listed and the user should be able to assign a key for every move. I am very new to windows forms and am unable to figure out what control to use for the left hand side things, i tried using buttons with KeyDown event but in this the event does not trigger for enter/return key, for rest of the keys it works fine. So what control along with what event should be used so that the user can assign any key of his choice for any motion/control.
EDIT: this was the initial code.
namespace ControllerWinServe
{
public partial class Form2 : Form
{
static string[] array = new string[6];
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button_d_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
}
private void button_u_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Form.KeyPress: '" + e.KeyCode.ToString() + "' pressed.");
}
private void button_d_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Form.KeyPress: b2 '" +e.KeyCode.ToString() + "'pressed.");
}
}
}
AFTER Trying to use user17753 's suggestion.
namespace ControllerWinServe
{
public class EnterTextBox : TextBox
{
protected override bool IsInputKey(Keys key)
{
if (key == Keys.Enter)
return true;
return base.IsInputKey(key);
}
}
public partial class Form2 : Form
{
static string[] array = new string[6];
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button_d_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
}
private void button_u_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Form.KeyPress: '" + e.KeyCode.ToString() + "' pressed.");
}
private void button_d_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Form.KeyPress: b2 '" +e.KeyCode.ToString() + "'pressed.");
}
}
}
If you're talking about hitting enter in a TextBox it isn't triggered by default. You can create a new one called for example EnterTextBox that is derived from TextBox that overrides IsInputKey to allow enter to trigger the event.
One such implementation could be:
public class EnterTextBox : TextBox
{
protected override bool IsInputKey(Keys key)
{
if (key == Keys.Enter)
return true;
return base.IsInputKey(key);
}
}
With this class in your project's namespace you'll be able to add EnterTextBox from the Toolbox under your project's namespace category.
Then you can add a method that is triggered by the KeyDown event on the EnterTextBox such as this:
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
//stuff to do after enter is pressed
}
}
Related
I designed a dashboard form that contains buttons. Some of these buttons have sub-menus. Since I'm planning to limit the access to some sub-menus, I want that every time a user clicks a restricted sub-menu, a form will appear that will prompt the user to enter the password before accessing the restricted page.
I already made a password form and programmed that when a sub-menu is clicked, the password form will appear. However, once the correct password is submitted, it will look like this:
I want that it will look like this when password is correct:
I want the overlapping form placed on the original position / panel. How to do that?
Here's the code for the dashboard:
public CFMenu()
{
InitializeComponent();
customizeDesign();
}
private void customizeDesign()
{
panelInventory.Visible = false;
panelSales.Visible = false;
}
private void hideSubMenu()
{
if(panelInventory.Visible == true)
panelInventory.Visible = false;
if(panelSales.Visible == true)
panelSales.Visible = false;
}
private void showSubMenu(Panel subMenu)
{
if(subMenu.Visible == false)
{
hideSubMenu();
subMenu.Visible = true;
}
else
subMenu.Visible = false;
}
private void CFMenu_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'casaFrancaDataSet.Sales' table. You can move, or remove it, as needed.
this.salesTableAdapter.Fill(this.casaFrancaDataSet.Sales);
timer1.Start();
label4.Text = DateTime.Now.ToLongTimeString();
label5.Text = DateTime.Now.ToLongDateString();
}
private void btnInventory_Click(object sender, EventArgs e)
{
showSubMenu(panelInventory);
}
private void btnSales_Click(object sender, EventArgs e)
{
showSubMenu(panelSales);
}
private void button1_Click(object sender, EventArgs e)
{
openPanelForm(new CFInventoryAdd());
hideSubMenu();
}
private void button2_Click(object sender, EventArgs e)
{
openPanelForm(new PasswordInventView());
hideSubMenu();
//string password = Interaction.InputBox("Enter Password: ", "Inventory View");
//if(password == "hello")
//{
// openPanelForm(new CFInventoryView());
// hideSubMenu();
//}
//else
//{
// MessageBox.Show("Incorrect Password!");
//}
}
private void button3_Click(object sender, EventArgs e)
{
openPanelForm(new CFInventoryUpdate());
hideSubMenu();
}
private void button7_Click(object sender, EventArgs e)
{
openPanelForm(new CFSalesAdd());
hideSubMenu();
}
private void button6_Click(object sender, EventArgs e)
{
openPanelForm(new CFSalesView());
hideSubMenu();
}
private void button8_Click(object sender, EventArgs e)
{
openPanelForm(new CFCalculate());
hideSubMenu();
}
private void button5_Click(object sender, EventArgs e)
{
openPanelForm(new CFSalesUpdate());
hideSubMenu();
}
private void button9_Click(object sender, EventArgs e)
{
openPanelForm(new CFReport());
hideSubMenu();
}
private void timer1_Tick(object sender, EventArgs e)
{
label4.Text = DateTime.Now.ToLongTimeString();
timer1.Start();
}
private Form activeForm = null;
private void openPanelForm(Form childForm)
{
if (activeForm != null)
activeForm.Close();
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
panelForm.Controls.Add(childForm);
panelForm.Tag = childForm;
childForm.BringToFront();
childForm.Show();
}
Here's a sub-menu that requires a password:
private void button2_Click(object sender, EventArgs e)
{
openPanelForm(new PasswordInventView());
hideSubMenu();
}
Here's the code for the Password Prompt form:
public partial class PasswordInventView : Form
{
public PasswordInventView()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var password = txtPassword.Text;
if (password == "1234")
{
CFInventoryView f2 = new CFInventoryView();
f2.Show();
this.Visible = false;
}
else
{
MessageBox.Show("Username or Password is incorrect!");
txtPassword.Clear();
}
}
Before making the password form, I tried using the InputDialog from VisualBasic by referencing it on my project. I liked it however I can't set the font of the input, and I wanted that my password will appear as ******** when typed. That's why I made my own password form.
public partial class PasswordInventView : Form
Change form to UserControl.
public partial class PasswordInventView : UserControl
And create user control in main form.
If this not works go create user control and copy PasswordInventView to UserControl.
Ctrl + Alt + L > Right click project > Add > New Item > Select User Control (Windows Forms) > Add. Copy chield form to user control. And create user control in main form.
Search about user control.
I'm trying to implement a customized exit prompt in my WinForms. (I should not be using DialogBox)
I have a User Control Object placed in my main form that is invisible and disabled by default. Clicking in a certain button I have placed on the form shows and enables the object, disabling everything in my form except the User Control.
private void btn_close_Click(object sender, EventArgs e) {
prompt1.Visible = true;
prompt1.Enabled = true;
disableControls();
//Wait for a button to be pressed in prompt1
//Make an action based on a button pressed.
//closeApp returns a boolean
if (!prompt1.closeApp)
{
prompt1.Visible = false;
prompt1.Enabled = false;
enableControls();
}
else
{
Application.Exit();
}
}
Here's my code at the prompt object:
public partial class Prompt : UserControl
{
bool exit;
public bool closeApp
{
get{return exit;}
}
public Prompt()
{
InitializeComponent();
}
private void btn_yes_Click(object sender, EventArgs e)
{
exit = true;
}
private void btn_no_Click(object sender, EventArgs e)
{
exit = false;
this.Hide();
}
}
What I want to do is wait for a button to be pressed in my prompt object before proceeding to the next line in the btn_close_Click().
What should I do? Is there a better way to implement this?
Add events to your usercontrol then handle those events on your main form.
In your usercontrol:
public event EventHandler<EventArgs> ExitCancelled;
public event EventHandler<EventArgs> ExitApplication;
private void btn_yes_Click(object sender, EventArgs e)
{
ExitApplication?.Invoke(this, EventArgs.Empty);
}
private void btn_no_Click(object sender, EventArgs e)
{
ExitCancelled?.Invoke(this, EventArgs.Empty);
}
Handle the events on your form:
public void prompt1_ExitApplication(object sender, EventArgs e)
{
Application.Exit();
}
public void prompt1_ExitCancelled(object sender, EventArgs e)
{
prompt1.Hide();
enablecontrols();
}
I have a project that uses various click events and looks like this
namespace Example
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_obj_Click(object sender, EventArgs e)
{
MyMethods.Method_1("text1");
}
private void btn_catg_Click(object sender, EventArgs e)
{
MyMethods.Method_1("text2");
}
private void btn_up_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text1");
}
private void btn_top_up_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text2");
}
private void btn_down_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text3");
}
private void btn_top_down_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text4");
}
public static class MyMethods
{
public static void Method_1(string text) {...}
public static void Method_2(string text) {...}
}
}
}
As you can see I have a quite a number of click events so i'm curious if I can group them all in another c# file or a class or something
In your code-behind, declare a common method you want to call when any of the above buttons fire the Click event.
private void CommonClick(object sender, EventArgs e)
{
}
Now in your Properties window for each button, you can assign this event handler for all buttons:
Now when any of the buttons are clicked this same event handler is called.
If you want to know which button is clicked, you can either use button Name or even the Tag property.
Let's say we assign a separate unique Tag for each button. Tag is a property you can see in the property window for each button (and most controls).
Then you can use a switch-case statement in your code to identify which button was clicked.
private void CommonClick(object sender, EventArgs e)
{
switch (((Button)sender).Tag)
{
case "B1":
break;
case "B2":
break;
}
}
Above, B1, B2 etc are the tags I've assigned to each button.
usually in the form designer you dblclick on the empty "click" event property to generate new method as btn_..._Click(object sender, EventArgs e).
instead you can select existed method, so multiple buttons can call the same method:
Then in the called Method you can check which control trigger this event:
private void button1_Click(object sender, EventArgs e)
{
if (sender == button2)
{
// ....
}
if (sender == button1)
{
// ....
}
}
First, pardon my new-ness, I just started coding class recently. Now, upon startup, I want parts of my form (c#) to not be shown, however when I put
NameDisplay.Visible = false;
(NameDisplay being the label I wish to hide) into my Form1.cs it gives me the error of that it is a 'field' being used as a 'type'. How do I correct this, and apply to other object types (buttons, textboxes, etc?)
EDIT 1-
Code- as it stands
namespace ATM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Label NameDisplay;
NameDisplay.Visible = false;
private void Form1_Load(object sender, EventArgs e)
{
}
private void StartButton_Click(object sender, EventArgs e)
{
}
private void NameDisplay_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Remove Label NameDisplay;, and place NameDisplay.Visible = false; into your FormLoad event.
The loading of a form is an event just like clicking a button, and will execute the code like so.
Also, when I hide labels, I use .Hide(), but I believe that only works on WinForms.
Hope this helps!
You need to drag and drop the Label on the form and object will be created and initialized automatically in InitializeComponent.
In the form constructor (after InitializeComponent function) or Form_Load event, you may set the visibility to false
For example:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
NameDisplay.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void StartButton_Click(object sender, EventArgs e)
{
}
private void NameDisplay_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
private void tabPage3_Click(object sender, EventArgs e)
{
((Control)this.tabPage3).Enabled = false;
}
private void tabPage4_Click(object sender, EventArgs e)
{
if (GloballsClass.Role == "student")
{
((Control)this.tabPage4).Visible = false;
this.tabPage4.Hide();
}
}
private void tabPage1_Click(object sender, EventArgs e)
{
if (GloballsClass.Role == "student")
{
tabPage1.Hide();
}
}
private void WelcomePage_Load(object sender, EventArgs e)
{
I've done this some time ago. The problem is that there is no property Visible and Enabled is doing not the things you would like to do.
So here is how i'm doing it:
// Put this over the constructor
private TabPage tabPage4ToShowForNotStudents = this.tabPage4;
private TabPage tabPage1ToShowForNotStudents = this.tabPage1;
Then you have to subscribe the Load-Method of your Form:
void WelcomePage_Load(object sender, EventArgs e)
{
if (GloballsClass.Role != "student")
{
yourTabControl.TabPages.Add(this.tabPage4ToShowForNotStudents);
yourTabControl.TabPages.Add(this.tabPage1ToShowForNotStudents);
}
}
Now it will add the TabPage to your TabControl if the Role is not student. If it is it will not be added.
Be sure to not have them added in the designer otherwise it will not work.
Hope this is useful :)
thanks guys.i have sorted out the problem.
private void WelcomePage_Load(object sender, EventArgs e)
{
if (GloballsClass.Role == "student")
{
tabControl1.TabPages.Remove(tabPage5);
}
else
{
tabControl1.TabPages.Remove(tabPage4);
}
}