How to make buttons click(work) multiple times? - c#

I'm creating a c# windows form application, and I created a navigation bar with buttons. There are 5 main buttons that appear all the time on the form but I also made sub buttons that appear when you click on the main buttons. When I click once they appear and if I click again on the same button they disappear and it works only once. If I click on the button for the third time or multiple times afterward they don't appear anymore. Can someone please tell me how to make it work properly? This is the code I used in the event handlers:
public void Izlez_Click(object sender, EventArgs e)
{
if (!buttonPressed4)
{
buttonPressed4 = true;
logout.Show();
exit.Show();
}
else
{
logout.Hide();
exit.Hide();
}
}

As Steve said, you also need to change buttonPressed4 to false in else statement.
public Form1()
{
InitializeComponent();
button2.Visible = false;
}
bool buttonPressed4 = false;
private void button1_Click(object sender, EventArgs e)
{
if (!buttonPressed4)
{
buttonPressed4 = true;
logout.Show();
}
else
{
// change buttonPressed4 to false
buttonPressed4 = false;
logout.Hide();
}
}

Related

How to make a button invisible even when I refresh a page depending on a label content in xamarin forms

When I click on the button it becomes hidden, however when I refresh the page it is still there. How to make it stay hidden depending on a content in a label?
private void Button3_Clicked(object sender, EventArgs e)
{
App.products[Index].Quantity++;
Button btn = (Button)sender;
if (btn.IsVisible)
{ btn.IsVisible = false; }
else
{
btn.IsVisible = false;
}
}
I want this Button to stay hidden when the page is refreshed depending on this value App.products[Index].Quantity. When I click on the Button it becomes from 0 to 1 and I want if it is not 0 the Button to be hidden.
In YourPage.xaml:
<Button IsVisible={Binding ButtonIsVisible} ... />
In YourPage.xaml.cs:
public YourPage()
{
InitializeComponent();
...
BindingContext = this;
}
// NOTE: `=>` not `=`. To be a property; expression evaluated every time it is needed.
public bool ButtonIsVisible => App.products[Index].Quantity == 0;
private void Button3_Clicked(object sender, EventArgs e)
{
App.products[Index].Quantity++;
// So xaml sees the change.
OnPropertyChanged(nameof(ButtonIsVisible));
}
For more information, google xamarin binding.

Where do I put custom code to handle a resize event C# windows form

I cannot work out where to put nor get the code to trigger when my main form windows are resized (ie minimize button clicked)
I am trying to trigger this code when ANY resize of the DigiDocketMain window is minimized etc, or also how I can specifically code the minimize button to do something - the ideal goal is to get the program - n minimize button click to hide the taskbar icon and show a tray icon.
I have tried placing this is the main code body and the designer code but nothing triggers it. any help would be appreciated.
private void DigiDocketMain_Resize(object sender, System.EventArgs e)
{
MessageBox.Show("You are in the Form.ResizeEnd event.");
if (this.WindowState == System.Windows.Forms.FormWindowState.Minimized)
{
this.Hide();
mainTrayIcon.Visible = true;
}
}
In your code behind add the following to the Form_Load Event
this.SizeChanged += Form1_SizeChanged;
Then implement the function, autocomplete may do this for you.
private void Form1_SizeChanged(object sender, EventArgs e)
{
// Add the code that will be called on resize events.
}
According to your description, when clicking the minimize button, you want to hide the
taskbar icon and display the tray icon.
I suggest that you set the Visible of notifyIcon1 to false in the property bar, and select a icon format image as the icon, then try the following code.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Deactivate(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.Hide();
this.ShowInTaskbar = false;
}
}
private void notifyIcon1_Click(object sender, EventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
this.ShowInTaskbar = true;
}
}

Winforms C# Disable/ Enable Button on Treenode Click

I have a treenode which displays a checklist from a SQL database. I have a method to get the selected workflows.
I want to enable the run button if a checkbox is checked and disable the button if nothing is checked and on load.
I'm not sure where to put this if statement. I have tried putting it under the run button on the click action but it is not working correctly.
Any help is appreciated.
List<WorkflowViewModel> workflowViewList = new List<WorkflowViewModel();
var workflowList = GetSelectedWrokflows();
if (workflowList.Count == 0)
{
button.enabled = false;
}
else
{
button.enabled = true;
}
One way to do this is to create a method that will do the work of determining the selected workflow items and enabling or disabling the button. By putting the code in a single method, it allows you to call it from multiple places, and if you need to change the behavior, you only have one place to make the modifications.
Then you can just call this method from the Form_Load event, and from the checked list box's ItemCheck event:
public partial class Form1 : Form
{
List<WorkflowViewModel> workflowViewList = new List<WorkflowViewModel>();
private void SetRunButtonState()
{
workflowViewList = GetSelectedWorkflows();
button.Enabled = workflowViewList.Count > 0;
}
private void Form1_Load(object sender, EventArgs e)
{
SetRunButtonState();
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
SetRunButtonState();
}
// Rest of class code omitted...
}

How to stop two datagridview from using Unison Scrolling to independently using toggle button

I have two datagridviews which have unison scrolling. now i want to use the button which will control unison scrolling and independently scrolling. My question is, how can i use toggle button to control unison and independently(which means it will be on and off).
I have created a button and named it Lock. let me put in a good scenario.
1.You will select one row from grid1 and perform unison scrolling.(Unison part is working)
2.And click Lock button which will allow unison scrolling.
3.Toggle the button off and it scrolls independently.
How can i do that ?
I have created this and it seems not working at all
Constructor of the form:
public frmMain_Page()
{
Grid1.Scroll += new System.Windows.Forms.ScrollEventHandler(Grid1_Scroll);
}
Button for unison:
private void Gridview_Input_Scroll(object sender, ScrollEventArgs e)
{
Gridview_Output.FirstDisplayedScrollingRowIndex = Gridview_Input.FirstDisplayedScrollingRowIndex;
}
Look button :
private bool toggle = true;
private void btnLock_Click(object sender, EventArgs args)
{
try
{
if (toggle == true)
{
toggle = false;
Grid2.FirstDisplayedScrollingRowIndex = Grid1.FirstDisplayedScrollingRowIndex;
}
else
{
toggle = true;
Grid1.Scroll += new System.Windows.Forms.ScrollEventHandler(Grid1_Scroll);
}
}
catch( Exception ex)
{
}
}

Toggle Group Box Visibility

I have two Group Boxes grpMeter and grpTag. I have to place grpMeter over grpTag.. both need same location and size..
On button click, I have to make them visible alternately. Is it possible? I tried many times but only 1 group box becomes visible. Maybe because of the overlapping problem. I tried with panel, but the same problem arises. Is there any solution?
public void ShowMeter()
{
grpMeter.Visible = true;
grpTags.Visible = false;
}
public void ShowTag()
{
grpTags.Visible = true;
grpMeter.Visible = false;
}
Place both group boxes next to each other so that they don't overlap and see if it works then. If you made it work, don't move the one group box with the mouse, but select it only and then set the coordinates manually in the Properties list.
That way you can prevent the one group box from accidentially becoming the child of the other group box.
Try this logic inside a button_click event:
private void btn_Click(object sender, EventArgs e)
{
if (grpTags.Visible)
ShowMeter();
else
ShowTag();
}
Try this:
private void button_Click(object sender, EventArgs e)
{
grpMeter.Visible = !grpMeter.Visible;
grpTags.Visible = !grpTags.Visible;
}
See the code below. The button Click will toggle visibilty. Also it's important that you set one of the groupboxes as visible and the other one as invisible in your constructor
using System;
using System.Windows.Forms;
namespace TestForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//This is important. Set one of them to be Visible and the other one to be invisible
grpMeter.Visible = false;
grpTags.Visible = true;
}
private void button1_Click(object sender, EventArgs e)
{
grpMeter.Visible = !grpMeter.Visible;
grpTags.Visible = !grpTags.Visible;
}
}
}
I am not sure but what you are looking for seems like FlowLayoutPanel. Then you can put group boxes next to each other and positioning will be handled automatically. This prevents accidentally putting one GroupBox into another or shifting locations. Also provides an easier working at design time.
One of the good way is to use RadioButton. Take two Radio buttons and place it inside a groupbox.
Something like this would work:
private void rdMeter_CheckedChanged(Object sender, EventArgs e)
{
grpMeter.Visible = rdMeter.Checked;
grpTag.Visible = !rdMeter.Checked;
}
private void rdTag_CheckedChanged(Object sender, EventArgs e)
{
grpTag.Visible = rdTag.Checked;
grpMeter.Visible = !rdTag.Checked;
}

Categories