C#: Forms and if statements - c#

I have a newbie question that's been bugging me. I've been messing around with forms in visual studio. In the simple program I'm making, I want to disable/enable a button depending on whether a checkbox is ticked.
Can someone tell me why this code doesn't work? Specifically the if statement.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Do_Check();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Do_Check();
}
private void Do_Check()
{
// button1.Enabled = checkBox1.Checked;
if (checkBox1.Checked)
button1.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Blah blah");
}
}
I've commented out one way of achieving the desired result. I'm just not sure why the if statement doesn't also work. Any help would be appreciated.

Your button is only enabled if the checkBox1 is checked - but never disabled. Hence you should do something like this:
if (checkBox1.Checked)
button1.Enabled = true;
else
button.Enabled = false;
However, your commented solution with
button1.Enabled = checkBox1.Checked;
is way more elegant.

Related

Hiding panel with animation is not working

i'm working on a project with C# and i'm occurring a problem while hiding a panel with (BunifuTransition) framework.
so basically i have many buttons as(Menu Buttons) and a subpanel with buttons as (submenu) and i'm having this problem...
So in here you can see that the Show and Hide panel works fine with all the clicked buttons...
Working fine in here
But when i try to close the same panel with clicking on the same button that shows it, here is what happened...
The panel closes but without Bunifu Animator
Here is the codes you need to know
private void HideSubMenu() //Method to hide the subpanles (BunifuAnimator)
{
if (PanelOtherRulesSubMenu.Visible == true)
PanelSubMenuAnimation.HideSync(PanelOtherRulesSubMenu);
}
--------------
private void ShowSubMenu(Panel Submenu) //Method to show the subpanels
{
if (PanelOtherRulesSubMenu.Visible == false)
PanelSubMenuAnimation.ShowSync(PanelOtherRulesSubMenu);
if (Submenu.Visible == false)
{
HideSubMenu();
Submenu.Visible = true;
}
else
Submenu.Visible = false;
and in here you can see that all the buttons are invoked to hide the subpanel, with exception of the "Other Rules", it is invoked the show the subpanels with some coditions ...
private void btnGovermentRules_Click(object sender, EventArgs e)
{
HideSubMenu();
}
private void GangRules_Click(object sender, EventArgs e)
{
HideSubMenu();
}
private void btnBusinessRules_Click(object sender, EventArgs e)
{
HideSubMenu();
}
private void btnBuildingRules_Click(object sender, EventArgs e)
{
HideSubMenu();
}
private void btnSubMenuOR_Click(object sender, EventArgs e) //here is the button invoked to show the panel
{
ShowSubMenu(PanelOtherRulesSubMenu);
}
And now guys i need you to help make the closing animation work as well when clicking "btnSubMenuOR_Click" button to close the subpanel.
Thank you,
Problem solved!.I just tried to remove and replace some lines of code as showing below, from this:
private void ShowSubMenu(Panel Submenu) //Method to show the subpanels
{
if (PanelOtherRulesSubMenu.Visible == false)
PanelSubMenuAnimation.ShowSync(PanelOtherRulesSubMenu);
if (Submenu.Visible == false)
{
HideSubMenu();
Submenu.Visible = true;
}
else
Submenu.Visible = false;
to this:
private void ShowSubMenu(Panel Submenu)
{
if (PanelOtherRulesSubMenu.Visible == false)
PanelSubMenuAnimation.ShowSync(PanelOtherRulesSubMenu);
else
PanelSubMenuAnimation.HideSync(PanelOtherRulesSubMenu);
}

Enabling the SHOP Button from form2 closing event

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;

Enable button if textbox populated

I have tried to code a button such that it is disabled upon form loading however enabled once a textbox has had text entered. My code is below, which is probably familiar:
Public Form()
{
InitializeComponent();
this.button1.enabled = false;
}
private void textbox_TextChanged (object sender, EventArgs e)
{
button1.Enabled = !string.IsNullOrWhiteSpace(textbox.Text);
}
The button indeed loads up disabled, the enabling function doesn't work upon text input and I'm not sure what the issue could be. It is a modal form is that matters. I was wondering if maybe I needed an event listener (although I'm not certain how exactly they work).
Check your Designer.cs file and make sure you have event handler registration there. Something like this:
this.textBox.TextChanged += new System.EventHandler(this.textBox_TextChanged);
Will this work? I can't really see a problem with your code though...
button1.Enabled = textbox.Text != "";
I hope this helps.
May not be the solution to your problem, but this would be the fastest check for one's computer to perform (at least if you let the JIT compiler optimize your code):
button1.Enabled = textbox.Text.Length > 0;
try this
private void Form1_Load(object sender, EventArgs e)
{
button1.Enabled = false;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
button1.Enabled = true;
}
}
You could try either of the following on the TextChanged property of the TextBox :
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textbox.Text.Length > 0)
{
button1.Enabled = true;
}
else
button1.Enabled = false;
}
or, using the string.IsNullOrEmpty method:
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textbox.Text))
{
button1.Enabled = true;
}
else
button1.Enabled = false;
}
The the below line:
button1.Enabled = !string.IsNullOrWhiteSpace(textbox.Text);
If it didn't work for you or if you are getting an error, then probably because the IsNullOrWhiteSpace method was introduced in .NET 4

Show button only when focus is on textbox

Is this possible to display button on Windows Form only when focus is on specific textbox?
Tried that with this approach:
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
button3.Visible = false;
}
No luck, because button click does not work then, because button is hidden immediately after textbox lost focus, preventing it from firing button3_Click(/*...*/) { /*...*/ } event.
Now I'm doing it like that:
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
//button3.Visible = false;
DoAfter(() => button3.Visible = false);
}
private async void DoAfter(Action action, int seconds = 1)
{
await Task.Delay(seconds*1000);
action();
}
Form now waits for a second and only then hides button3.
Is there any better approach?
I think you want to display the button only when focus is on specific textbox or the focus is on the button.
To do this you can check the Focused property of button3 in the Leave event of textBox2 and only hide the button if the button doesn't have focus. Note that the button will get focus before the Leave event of textBox2 fires.
You will then need to hide the button in the scenario where button3 loses focus and the focus moves to somewhere other than textBox2. You can use exactly the same technique here by handling the Leave event of button3 and only hiding button3 if textBox2 does not have focus.
The following code should fit your requirements:
private void textBox2_Leave(object sender, EventArgs e)
{
if (!button3.Focused)
{
button3.Visible = false;
}
}
private void button3_Leave(object sender, EventArgs e)
{
if (!textBox2.Focused)
{
button3.Visible = false;
}
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("Button clicked");
}
Why not work with the GotFocus and LostFocus event of the TextBox?
private void textBox2_GotFocus(object sender, EventArgs e)
{
button3.Visible = true;
}
Then hide the button on the click event.
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
button3.Visible = false;
}
How about you add a Panel and place the button and text boxes in that panel and when user MouseHovers that Panel then display the button...
This way user would be able to click on the button...
This is the event you are looking for, I think...
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover(v=vs.110).aspx
UPDATE:
var textboxFocussed = false;
private void textBox2_Enter(object sender, EventArgs e)
{
textboxFocussed = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
textboxFocussed = false;
}
UPDATE 2
private void Panel_GotFocus(object sender, EventArgs e)
{
button3.Visible = textboxFocussed;
}
private void Panel_LostFocus(object sender, EventArgs e)
{
button3.Visible = false;
}
Here are the details of the Panel Events
you can add Enter event handler for all controls on form at Load. Just make sure to skip the controls on which you want to show the button.
List<string> strControlException = new List<string>();
public Form1()
{
InitializeComponent();
strControlException.Add("btnMain");
strControlException.Add("txtMain");
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < this.Controls.Count;i++ )
{
if (!strControlException.Contains(Controls[i].Name))
{
Controls[i].Enter += new EventHandler(hideButton);
}
}
}
private void txtMain_Enter(object sender, EventArgs e)
{
btnMain.Visible = true;
}
private void hideButton(object sender, EventArgs e)
{
btnMain.Visible = false;
}
btnMain (Button you want to Manipulate) and txtMain (Which controls the vibility of the button) are the controls in contention here
Add more controls on the form to test.
Explanation for the above code :
First initialize a list with the names of controls that should show the Button
On Form Load add an Event handler to all controls (except the one in our list)
In the handler function hide the button. (You might want to perform more logic here based on the control that called this function)
Button is hidden by default and only on textbox Enter event we show the button.

Display Ballon Tip on Button Click

I want to display a ballon tip when an error occures instead of showing MessageBox.
[NOTE] i did not want it to be shown on mouse Hover.
I tried both but they actually show the tip on mouse hover
toolTip1.SetToolTip();
toolTip1.Show();
You can use the ToolTip Popup event to check if there is a Tooltip present and cancel it if there isn't. You can then set the tooltip during your validation then show it. In this example I set a timer to reset the tooltip text after a 2 second timeout.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
toolTip1.IsBalloon = true;
toolTip1.Popup += new PopupEventHandler(toolTip1_Popup);
toolTip1.SetToolTip(textBox1, "");
}
void toolTip1_Popup(object sender, PopupEventArgs e)
{
if (toolTip1.GetToolTip(e.AssociatedControl) == "")
e.Cancel = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
toolTip1.RemoveAll();
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
int temp;
if (!int.TryParse(textBox1.Text, out temp))
showTip("Validation Error", (Control)sender);
}
private void showTip(string message, Control destination)
{
toolTip1.Show(message, destination);
timer1.Start();
}
}
Much to my surprise it appears that toolTip1.IsOpen = true will show a tooltip and allow it to stay open. Note that you will need to provide code to close it because it wouldn't go away on its own on my machine no matter what I did.

Categories