I have a form that a user uses to select the level when a Game starts. I want to disable the close button such that a user cannot close the form (The user will click some buttons to select the level).
I have been able to stop the user from closing the form if a button is not clicked using
bool _Next = false;
public Form1()
{
InitializeComponent();
button1.Click += new EventHandler(button_Click);
button2.Click += new EventHandler(button_Click);
button3.Click += new EventHandler(button_Click);
}
void button_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (btn == button1)
{
Level(1);
}
else if (btn == button2)
{
Level(2);
}
else if (btn == button3)
{
Level(3);
}
_Next = true;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (_Next == true)
{
}
else
{
e.Cancel = true;
}
}
This is quite long. I want to know if there is any way i can just disable or hide the form close button
You can hide the Controls of the form with:
this.ControlBox = false;
The above will remove the maximize, minimize and close button from the right upper corner.
The above is also accessible on the Visual Studio when you select the form itself at the properties tab.
You can override the OnClosing method:
protected override void OnClosing(CancelEventArgs e)
{
if (!_Next)
{
e.Cancel = true;
}
}
You're already disabling the close button. In your FormClosing event you cancel the event:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
This code works perfectly; I just tested it to make sure.
Related
I have this below form and I want to detect key-strokes and any mouse clicks on the button.
For example, if I press S on my keyboard, message will show START and keypress will display keyboard press. The same thing happen when mouse-click on START button, except that keypress will display START BTN.
Here is my button code. If I only do for button or only IsKeyDown it works fine, but when I combine both in one form, they go haywire.
private void btnStart_Click(object sender, EventArgs e)
{
lblKeypress.Text = "START BTN";
lblmessage.Text = "START";
}
Here is my Keyboard.IsKeyDown code:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (Keyboard.IsKeyDown(Key.S))
{
lblKeypress.Text = "Keyboard Press";
lblmessage.Text = "START";
}
}
Please help, thanks.
try this code
private void Form1_KeyDown(object sender,
EventArgs e){
if (e.KeyData == Keys.S){
lblKeypress.Text = "Keyboard Press";
lblmessage.Text = "START";
}
}
Make sure that in your Form you set KeyPreviewProperty to TRUE
In Form1.Designer.cs
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.form_keydown);
private void form_keydown(object sender, KeyEventArgs e)
{
int keyVal = (int)e.KeyValue;
keyValue = -1;
if ((keyVal >= (int)Keys.S))
{
keyValue = (int)e.KeyValue - (int)Keys.S;
lblKeypress.Text = "Keyboard Press";
lblmessage.Text = "START";
}
}
Note:
Ensure you do not have any unrelated WPF namespaces`
(Keyboard.IsKeyDown(Key.S)) is a WPF approach, in WinForms you are better off using combination of e.KeyValue & Keys
I have a little problem that I can't enable the shop button when the form2 closes.
So, I was making a shop for my clicker game. When you press the SHOP button your given a form2 (which was supposed to be the shop, WIP) and the shop button (button2) should be disabled because you can spam the shop button when its not disabled. After closing the shop the button was supposed to be enabled again, but I can't get it to work.
Note: the Form 2 Close button for the shop is sCloseBtn.
Form 1 Code:
private void button1_Click(object sender, EventArgs e)
{
i += 1;
if (i == 1)
{
label4.Text = "Cake";
}
else
{
label4.Text = "Cakes";
}
label3.Text = Convert.ToString(i);
}
public void button2_Click(object sender, EventArgs e)
{
WindowsFormsApplications2.Form2 secondForm;
secondForm = new WindowsFormsApplications2.Form2();
if (isInGui == false) {
secondForm.Show();
button2.Enabled = false;
isInGui = true;
}
else if (isInGui == true)
{
button2.Enabled = true;
isInGui = false;
}
}
Form 2 Code:
private void sCloseBtn_Click(object sender, EventArgs e)
{
Close();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
}
All help is appreciated :)
One way to solve that would be to move the event handler "Form2_FormClosing" to Form1,
public partial class Form1 : Form
{
// ...whatever is in your form already...
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
button2.Enabled = true;
isInGui = false;
}
// ...
}
and to assign it to secondForm's event after its creation, ie.
secondForm = new WindowsFormsApplications2.Form2();
secondForm.FormClosing += Form2_FormClosing;
I want to disable a button (button3) on a primary form when a second, modeless form (Form2) is loaded, and then re-enable the button when the modeless form is closed.
Here is what I've tried:
private void button3_Click(object sender, EventArgs e)
{
Form2 p = new Form2(label1.Text);
p.Show();
if (p.Shown)
this.button3.Click += new System.EventHandler(this.button3_Click);
else
this.button3.Click -= new System.EventHandler(this.button3_Click);
}
The best method for achieving this would be to disable button3 prior to showing Form2, and using the FormClosed event on Form2 to re-enable button3 once the form is closed:
public partial class Form1 : Form
{
...
private void button3_Click(object sender, EventArgs e)
{
// Instantiate the form and assign the FormClosed event
var form = new Form2(label1.Text);
form.FormClosed += Form2_FormClosed;
// Disable button3
button3.Enabled = false;
// Show the form
form.Show();
}
// Occurs when Form2 is closed
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
// Re-enable button3
button3.Enabled = true;
}
}
An alternative method, that assigns a lambda expression to the FormClosed event:
private void button3_Click(object sender, EventArgs e)
{
// Instantiate the form
var form = new Form2(label1.Text);
// Assign a lambda method to the FormClosed event to re-enable button3
form.FormClosed += (s, a) => button3.Enabled = true;
// Disable button3
button3.Enabled = false;
// Show the form
form.Show();
}
I did a similar thing, i m not sure to understand what you want to do but maybe it will help you.
public partial class Form2 : Form
{
private void button3_Click(object sender, EventArgs e)
{
Button3.Enabled = false;
Form2 p = new Form2(label1.Text);
p.ShowDialog();
//the code will stop here until you finish your work on form2
Button3.Enabled=true;
}
it works with me.
But if your form3 is just a short label use:
Button3.Enabled= false;
MessageBox.show ("blabla");
Button3.Enabled=true;
I have Form with textboxes and they have MouseEvent(MouseMove, MouseDown) and they are enabled on form load, but my question is how to call them just when i Click the Edit Button, so just then the textboxes can move?
My code:
private void textBox_MouseMove(object sender, MouseEventArgs e)
{
TextBox txt = sender as TextBox;
foreach (TextBox text in textBoxs)
{
if (e.Button == MouseButtons.Left)
{
if (txt.Name == text.Name)
{
txt.Left = e.X + txt.Left - MouseDownLocation.X;
txt.Top = e.Y + txt.Top - MouseDownLocation.Y;
}
}
}
}
private void textBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
private void btnEdit_Click(object sender, EventArgs e)
{
btnEdit.Visible = false;
btnPrint.Visible = false;
btnSave.Visible = true;
//Want to call mouse function here.
}
any suggestions?
Instead of hooking the events from the Visual Studio designer, you should manually hook the events in the btnEdit_Click handler method by adding this line:
textboxname.MouseMove += new MouseEventHandler(textBox_MouseMove);
Then unhook the event when your save button is clicked (I'm assuming you have some method btnSave_Click) by doing the following:
textboxname.MouseMove -= new MouseEventHandler(textBox_MouseMove);
The same goes for your MouseDown event.
If I understand your post, you want the textbox functionality to become "active" after you click the btnEdit button?
You could set a flag in btnEdit_Click, and only process the functionality in the other functions if that flag is true
Or, maybe add the event in the btnEdit_Click function, e.g.
private void btnEdit_Click(object sender, EventArgs e)
{
btnEdit.Visible = false;
btnPrint.Visible = false;
btnSave.Visible = true;
//Want to call mouse function here.
textBox.MouseDown += new MouseEventHandler(textBox_MouseDown);
}
But remove that extra line from where it currently exists in your code.
on Mdi parent form i am calling my child forms using menu items. on Child form Load my Menu item should be disabled on child form closed it will again Enabled.., i try FormClosing event handler i get the answer..,
private void btnMn1_Click(object sender, EventArgs e)
{
Forms.Cnblfrm cnbfrm = new Cnsmblfrm();
cnsmbfrm.MdiParent = this;
cnsmbfrm.Text = btnMn1.Text;
cnsmbfrm.Show();
this.btnMn1.Enabled = false;
cnbfrm.FormClosed += new FormClosedEventHandler(cnsmbfrm_FormClosed);
}
void cnbfrm_FormClosed(object sender, FormClosedEventArgs e)
{
btnMn1.Enabled = true;
//throw new NotImplementedException();
}
by using the above code i am getting the answer but i have morethan 20 ChildForms. By using this method My coding is increasing...., there is any method instead of this....,
If I understand you right: you have 20 buttons where every button opens a specific form, right?
If so, you can set the tag-property of each button to the form it opens. then you have to iterate through all buttons and set the click-event. All buttons have the same click-event. (let's call it btn_click)
the code of btn_click can look like:
private void btn_click(object sender, EventArgs e)
{
Button button = sender as Button;
if(button == null)
return;
Form form = button.Tag as Form;
if(form == null)
return;
form.MdiParent = this;
form.Text = button.Text;
form.Show();
button.Enabled = false;
form.Tag = button;
form.FormClosed += FormClosed;
}
private void FormClosed(object sender, FormClosedEventArgs e)
{
Form form = sender as Form;
if(form == null)
return;
Button button = form.Tag as Button;
if(button == null)
return;
button.Enabled = true;
}