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);
}
Related
I'm new to C# and started making my own program. How ever, I want these (https://gyazo.com/8b4e0f4141b15e1ff204e1cfc8f41827) to disappear until my progress bar loads (https://gyazo.com/5064ebd9582593942c6e538e1ae516dc). I tried to use Visible but got nowhere.
ProgressBar code:
private void button1_Click(object sender, EventArgs e)
{
this.timer1.Start();
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
var before = this.progressBar1.Value;
this.progressBar1.Increment(1);
var after = this.progressBar1.Value;
if (after > before && after == this.progressBar1.Maximum)
{
MessageBox.Show("Successfully loaded...");
this.BackgroundImage = Properties.Resources.image1;
}
}
In "Form.Load" set Image.Visible = false and when you are showing the MessageBox set Image.Visible = true
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.
I have two checkboxes on my form; chkBuried and chkAboveGround. I want to set it up so if one is checked, the other is unchecked. How can I do this?
I have tried the CheckChanged property:
private void chkBuried_CheckedChanged(object sender, EventArgs e)
{
chkAboveGround.Checked = false;
}
private void chkAboveGround_CheckedChanged(object sender, EventArgs e)
{
chkBuried.Checked = false;
}
And it works, just not as well as I hoped. That is, when I check chkBuried, then check chkAboveGround, both boxes become unchecked before I can check another one again.
modify your code as below.
private void chkBuried_CheckedChanged(object sender, EventArgs e)
{
chkAboveGround.Checked = !chkBuried.Checked;
}
private void chkAboveGround_CheckedChanged(object sender, EventArgs e)
{
chkBuried.Checked = !chkAboveGround.Checked;
}
I suggest you use check_click instead of check_changed
private void checkBox1_Click(object sender, EventArgs e)
{
checkBox2.Checked = false;
checkBox3.Checked = false;
}
private void checkBox2_Click(object sender, EventArgs e)
{
checkBox1.Checked = false;
checkBox3.Checked = false;
}
private void checkBox3_Click(object sender, EventArgs e)
{
checkBox1.Checked = false;
checkBox2.Checked = false;
}
The reason for the behavior you have explained is that you are using CheckedChanged event, which means that when you are setting the Checked property of a CheckBox manually, the event is also fired, causing another box to react again.
Therefore, the following might help you:
private void chkBuried_CheckedChanged(object sender, EventArgs e)
{
if (chkBuried.Checked == true) {
chkAboveGround.Checked = false;
} else {
chkAboveGround.Checked = true;
}
}
private void chkAboveGround_CheckedChanged(object sender, EventArgs e)
{
if (chkAboveGround.Checked == true) {
chkBuried.Checked = false;
} else {
chkBuried.Checked = true;
}
}
UPDATE 29.03.2020: functionally the code in my answer is the same as the answer given by Riz. Nevertheless, I am leaving the code as I put it originally since it might make the whole situation easier to understand for the people who are new to coding. If you are to implement anything similar in production code, please use the answer by Riz as an example.
I would prefer radio buttons, but you can do something like this:
public void CheckACheckBox(Checkbox ck)
{
foreach (Control ckb in this.Controls)
{
if ((ckb is CheckBox) && (ckb == ck))
ck.Checked = true;
else
ck.Checked = false;
}
}
List<CheckBox> groupOfCheckBoxes = new List<CheckBox>();
void InitFunction() {
groupOfCheckBoxes.Add(checkbox1);
groupOfCheckBoxes.Add(checkbox2);
groupOfCheckBoxes.Add(checkbox3);
foreach (CheckBox cb in groupOfCheckBoxes)
cb.Click += checkbox_Click
}
void checkbox_Click(object sender, EventArgs e)
{
foreach (CheckBox cb in groupOfCheckBoxes) {
cb.IsChecked = cb == sender;
}
}
However I would suggest radio boxes as well.
The code above is untested and may have some typos
Better version, allows the user to uncheck boxes.
private void chkOne_CheckedChanged(object sender, EventArgs e)
{
if (chkTwo.Checked == true)
{
chkTwo.Checked = !chkOne.Checked;
}
}
private void chkTwo_CheckedChanged(object sender, EventArgs e)
{
if (chkOne.Checked == true)
{
chkOne.Checked = !chkTwo.Checked;
}
}
The best option and easiest way for me was to create a Click event instead of a CheckedChanged event.
This method works perfectly with two or more checkbox and allows to have them all unchecked.
private void chkOne_Click(object sender, EventArgs e)
{
chkTwo.Checked = false;
}
private void chkTwo_Click(object sender, EventArgs e)
{
chkOne.Checked = false;
}
This will work for two unchecked boxes, since they are already unchecked it is simpler.
I had to do this myself also.
private void customer_IsCheckedChanged(object sender, EventArgs e)
{
business.IsChecked = false;
}
private void business_IsCheckedChanged(object sender, EventArgs e)
{
customer.IsChecked = false;
}
I needed to show or not show a table when activating the CheckBox, how they were two, if I activated both, everything was fine, but if I tried to deactivate one later, the other was also deactivated. PD: The default value for the tables that I used was Visible=false. The solution I got was the following:
protected void YourNameOfMethodForBothCheckBox(object sender, EventArgs e)
{
if (CheckBox_1.Checked == true)
{
Table_1.Visible = true;
if (CheckBox_2.Checked == true)
{
Table_2.Visible = true;
}
else { Table_2.Visible = false; }
}
else
{
Table_1.Visible = false;
if (CheckBox_2.Checked == false)
{
Table_2.Visible = false;
}
else
{
Table_2.Visible = true;
}
}
}
I have a login screen and I want to activate it when I press enter in the password textbox.
the problem is that even though it works, when I close the form the app acts like enter is still pressed and the form opens in an endless loop.
here is my code:
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox2.KeyDown += new KeyEventHandler(textBox2_KeyDown);
}
public void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (user == Username[1] && pass == passwords[1])
{
MessageBox.Show("Login successfull", "Welcome, HR");
UpdateDBForm newEmployee = new UpdateDBForm();
this.Hide();
newEmployee.ShowDialog();
return;
}
}
How tdo I solve this problem?
Thanks.
You are assigning the KeyDown-EventHandler everytime your text changes:
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox2.KeyDown += new KeyEventHandler(textBox2_KeyDown); // EVIL!
}
This means, that the more often you input data into your textbox, the more the eventhandler will be assigned and when you eventually hit enter it will be called as many times.
Assign the eventhandler once, i.e. in your constructor and this should work.
As #bash.d written you are assigning event multiple times, do it once (either by designer or in constructor (after InitializeComponent call) or in Form_Load event)
private void Form_Load(object sender, EventArgs e)
{
textBox2.KeyDown += new KeyEventHandler(textBox2_KeyDown);
}
also you've written that you want login after user click enter, so you have to add this if:
public void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (user == Username[1] && pass == passwords[1])
{
MessageBox.Show("Login successfull", "Welcome, HR");
UpdateDBForm newEmployee = new UpdateDBForm();
this.Hide();
newEmployee.ShowDialog();
return;
}
}
}
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.