Can i add 2 Function on single button(Unity) - c#

Morning, I have a question can I add 2 functions on a single button Like On/Off button but just 1 button, when I click once to turn off then I click that button again turn on
public void DrawingOn()
{
onScreenDrawing.enabled = true;
}
public void DrawingOff()
{
onScreenDrawing.enabled = false;
}
I try to add all of them on onClick button but that always turn off even though I clicked once

Open the Design view from your IDE, add or change the Click handler of your button to:
void Click_Button()
{
onScreenDrawing.enabled = !onScreenDrawing.enabled;
}

You just need one event to do that.
Change your code to something like this
public void ChangeState() {
bool updated;
if(onScreenDrawing.enabled){
onScreenDrawing.enabled = false;
updated = true
}else{
if(updated == false){
onScreenDrawing.enabled = true;
}
}
}

Related

How to change default radio button checked in c#

How to check radio button default
For example, if I have 5 radio buttons in the group, I want to check the default 1st radio button.
I need a code for updating it, like when the project is running, and I click the 2nd radio button, and the update button default should change to the 2nd radio button.
Select all RadioButtons with Strg
Go to Events and double click CheckedChanged
Then just check whether the button is checked or not
private void radioButton3_CheckedChanged_1(object sender, EventArgs e)
{
if (radioButton1.Checked)
{
//Do something
}
else if (radioButton2.Checked)
{
//Do something
}
else if (radioButton3.Checked)
{
//Do something
}
}
If you are using Winforms
Add Settings to you project (Set int value)
Add the Code will save the current radio checked Add in all radios
Properties.Settings.Default.currentRadio = 1;
Properties.Settings.Default.Save();
In the form initializing set the current checked Radio
if (Properties.Settings.Default.currentRadio == 1)
{
radioButton1.Checked = true;
}
else if(Properties.Settings.Default.currentRadio == 2)
{
radioButton2.Checked = true;
}

Button click to open new form and don't open if already have active form running

I have a button click event which will pop up a form. How do I check if an existing form is already present before creating one and showing it?
here is my code
private void Button_Click(object sender, RoutedEventArgs e)
{
Wizard wizard = new Wizard();
if (wizard.IsVisible)
{
}
else
{
wizard.Show();
}
}
the code here does not work as a new pop up wizard (form) is created everytime i click on the button from another window.
Either use ShowDialog and make it modal
Or use the reference reference to wizard or a bool flag to make the check. Make sure if you use a reference to set it null afterwards
bool isOpen;
...
public void click()
{
if (!isOpen)
{
// do something
wizard = new Wizard();
wizard.Closing += (sender, args) =>
{
isOpen = false;
};
isOpen = true;
wizard.Show();
}
...
Or as noted in comments. Set the button Enabled property false to protect from further hits
Enabled = false;

Xamarin C#: ImageButton, deselecting a button upon selecting another one

I'm building a game, one of the 'mechanics' is selecting a tool by clicking on one of the three ImageButtons (representing a Drill, a Hammer and a Brush).
I want to mark the clicked button selected but only until I switch tools by selecting a second one. Is there any clean way of unselecting/selecting buttons?
Current code:
if (drillbutton.Selected)
{
brushbutton.Selected = false;
hammerbutton.Selected = false;
}
else if(hammerbutton.Selected)
{
drillbutton.Selected = false;
brushbutton.Selected = false;
}
else if(brushbutton.Selected)
{
drillbutton.Selected = false;
hammerbutton.Selected = false;
}
There must be an easier way... Right?
You could assign the same click handler to every button, like this:
private void SelectButton(object sender, EventArgs args) {
// you will have to have an IEnumerable that contains your set of buttons
foreach(var b in buttons) {
if (b == (Button)selected) b.Selected = true else b.Selected = false;
}
}

C# If Button IsEnabled, its triggering event on click

I have next issue, I am working on small c#-wpf application,
and on load I am disabling button which is ussualy used to do some action on click, and it looks like this:
public MainWindow()
{
InitializeComponent();
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
if (String.IsNullOrEmpty(password.Password))
{
btnActivate.IsEnabled=false;
}
}
and somewhere I am checking my password field, for example:
private void password_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.Enter))
{
if (password.Password == "drag0n")
{
btnActivate.IsEnabled = true;
}
else
{
btnActivate.IsEnabled = false;
}
}
}
And my problem is next, when user enter "drag0n" and press enter, button should be just enabled, but its not only enabled, its calling automatic his event _Click, I don't know why does that happening, because in that case, if my button is just enabled event _Click is also called, and if user clicks on that button, event is called again, so actually my event onclick is called twice.
My question is how can I stop calling my Click event if I set IsEnabled=true. When I set IsEnabled=true I just want it to be enabled for pressing and I don't want execute event _Click.
I want to execute event _Click only when my button is pressed as it should work and not on IsEnabled=true.
Thanks guys,
Cheers
On click event occurs when you press Enter key, because this button is the default control in a form.
If you don't want click event on Enter key, you should either make this button not default or not process Enter key pressing in your button click (e.Handled = true -> when Enter is pressed).
Or try to change your code:
private void password_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.Enter))
{
if (password.Password == "drag0n")
{
e.Handled = true; // add this line
btnActivate.IsEnabled = true;
}
else
{
btnActivate.IsEnabled = false;
}
}
}

Make error div only shown when set to visible?

I have some code to show an alert on condition:
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gvGroups.Rows)
{
CheckBox cbAdmin = (CheckBox)gvr.FindControl("cbAdmin");
CheckBox cbRemove = (CheckBox)gvr.FindControl("cbRemove");
Label lblID = (Label)gvr.FindControl("lblID");
int id;
bool idValid = int.TryParse(lblID.Text,out id);
bool isReadOnly = !cbAdmin.Checked;
if (idValid)
{
Group g = SecurityManager.GetGroup(id);
if (g.IsReadOnly != isReadOnly)
{
bool updateSuccess = SecurityManager.ChangeGroupPermissions(id, isReadOnly);
var master = Master as KezberProjectManager;
if (master != null)
{
master.ShowErrorAlert("Sample Error");
}
}
if (cbRemove.Checked)
{
bool removeEmpSuccess = SecurityManager.RemoveEmployeesFromGroup(id);
bool removeSuccess = SecurityManager.RemoveGroup(id);
}
}
}
BindGrid();
}
And this calls:
public void ShowErrorAlert(string message)
{
error_alert.Visible = true;
lblError.Text = message;
}
When the master page loads:
if (!IsPostBack)
{
success_alert.Visible = false;
error_alert.Visible = false;
}
The only issue is, once the alert is shown, even if I dont ask for it to be shown next time, it is still there. The way I need it is, 1 call to ShowErrorAlert should only show it once, after, if I click the save button but no changes are made, then the show method is not called and it should not show up.
Is there a way to do this?
Thanks
If you want to show the alert only on one postback, remove the if (!IsPostBack) before setting the visibility of the success_alert and error_alert to false.
Page_Load is called before btnSave_Click so you want to first set them to Visible = false; and then, if ShowErrorAlert() is called, it'll change it to true.
Once you show the alert, there is no option in your code to hide it since you hide it only on !IsPostback
So you can just hide the alert in the beginning of the button click event by default and show it only if the condition is true..

Categories