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;
}
}
Related
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;
}
}
}
I am trying to disable the thin and crispy checkbox when traditional checkbox is clicked. I have these in a group due to me enabling the whole group when the numericUpDown value is set to 1. When I click traditional checkbox, it doesn't disable the thin and crispy checkbox. I am using windows form application
Code
private void NudQuantity1_ValueChanged(object sender, EventArgs e)
{
if (NudQuantity1.Value == 0)
{
gbCheesePizza.Enabled = false;
}
else
{
gbCheesePizza.Enabled = true;
}
if (CBXTraditional1.Checked == true)
{
CBXthinandcrispy1.Enabled = false;
}
}
When I run this code outside of a groupbox, it works perfectly.
I don't think this block should be inside the event handler
if (CBXTraditional1.Checked == true)
{
CBXthinandcrispy1.Enabled = false;
}
It means that, provided you've got no other event handling for the checkboxes, this code will only be executed when you change the value of NudQuantity1 so it won't execute anything when you click the checkboxes afterwards.
Try use radio buttons as Steve mentioned. They do this for you.
I have a repositoryItemCheckEdit in a column of my grid. The task I want to do is :
Once the user pressed the CheckEdit , this cell become disable so that the user can not make click again.
To do this task I'm using the CheckedChanged event, in the following way :
private void repositoryItemCheckEdit1_CheckedChanged(object sender, EventArgs e)
{
var obj = sender as CheckEdit;
if (obj.Checked)
{
repositoryItemCheckEdit1.Enabled = false;
}
}
With the above event the only thing I get is that the cell becomes clearer , but not is disabled. Even if I make click again it allows me to do it.
Any help is appreciated.
You will probably have more luck/an easier time dealing with this using the brute force method... at least i find this a lot easier than dealing with the crazyness of DataGridView controls scheme.
Use the Tag attribute of your control to set a flag on it, and then when someone tries to un-check it/change it, force it back to checked. Like so:
private void repositoryItemCheckEdit1_CheckedChanged(object sender, EventArgs e)
{
var obj = sender as CheckEdit;
if(obj.Tag != null)
{
obj.Checked = true;
repositoryItemCheckEdit1.Enabled = false;
}
else
{
if (obj.Checked)
{
obj.Tag = true;
repositoryItemCheckEdit1.Enabled = false;
}
}
}
I have a Repeater Control with various buttons in it.
When the button gets clicked, it needs to disable itself so it cant be clicked again. Working.
However, when I click that button, it needs to enable any other button but it.
So, When I click on it, it needs to disable. When I click on another one, the previous button must enable, and that one must disable.
So for I've tried:
Button btnLoad = (Button)e.Item.FindControl("btnLoad");
foreach (Button b in e.Item.Controls.OfType<Button>().Select(c => c).Where(b => b != btnLoad))
{
b.Enabled = true;
}
btnLoad.Text = "Currently Viewing";
btnLoad.Enabled = false;
But it isnt working. Depending on where I put it, its either leaving all the buttons enabled (But still changing its text), or not doing anything at all.
What do I need to do to make this work?
Edit: The code is found here:
protected void rptPdfList_ItemCommand(object source, RepeaterCommandEventArgs e)
Which is why I use Button btnLoad = (Button)e.Item.FindControl("btnLoad");.
The method is found in :
switch (e.CommandName)
{
case "LoadDoc":
//Above code
break;
}
Assuming you want that code in the Button's Click-event handler:
protected void Button_Clicked(Object sender, EventArgs e)
{
Button thisButton = (Button) sender;
thisButton.Text = "Currently Viewing";
RepeaterItem item = (RepeaterItem) thisButton.NamingContainer;
IEnumerable<Button> buttons = item.Controls.OfType<Button>();
foreach(var btn in buttons)
{
btn.Enabled = btn != thisButton;
}
}
Try a simple if statement in a loop as below
Button btnLoad = (Button)e.Item.FindControl("btnLoad");
foreach (Control c in e.Item.Controls)
{
if (b is Button)
{
if(b == btnLoad)
{
b.Enabled = false;
}
else
{
b.Enabled = true;
}
}
}
Using TabControl.SelectTab("...") shows the tab but it also gives the tab focus. I would like to show a particular tab, but keep focus where it is.
I have data rows in a grid. Based on properties of the selected row, I show a different tab page to have a different UI layout. But when using arrow keys to scroll through rows, the focus switches to the selected tab -- which I don't want to happen.
Thanks.
You can try disabling the TabControl before setting the selected tab, then re-enabling it. This will prevent it from taking focus. I tested this on a tab control with a few controls on it, and didn't see any visual change, but you'll have to try it in your UI and see whether it's ok for you.
tabControl1.Enabled = false;
tabControl1.SelectTab("tabPage4");
tabControl1.Enabled = true;
To be safe, you could put the line to re-enable the TabControl in a finally block to make sure it doesn't get left disabled.
I don't think there's a built-in function, but you can do in this way:
private bool skipSelectionChanged = false;
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
if (skipSelectionChanged)
return;
// supposing we decide tab[0] has to be selected...
this.SelectTabWithoutFocus(this.tabControl1.TabPages[0]);
}
private void SelectTabWithoutFocus(TabPage tabPage)
{
this.skipSelectionChanged = true;
// "this" is the form in my case, so you get the current focused control
// (ActiveControl), backup it, and re-set it after Tab activation
var prevFocusedControl = this.ActiveControl;
if (this.ActiveControl != null)
{
this.tabControl1.SelectedTab = tabPage;
prevFocusedControl.Focus();
}
this.skipSelectionChanged = false;
}
Here, I backup the current focused control, select the desired tab, and finally set the focus to the original control.
Skipping boolean is necessary, because giving the focus to the grid you trigger SelectionChanged event again, causing infinite looping.
This selects the tab pages while keeping the focus on top, as asked here above:
tc.TabPages[0].Enabled = false;
tc.SelectTab(0);
tc.TabPages[0].Enabled = true;
tc is here my instance for the TabControl type (i. e. it IS my tab control, and it has a few "tab pages"). This works properly for me. My purpose is to loop through these tab pages with the Left and Right keys (arrows) i. e. when I go forwards (by Key.Right) and reach the last tabpage I want to have my focus on [0] without activating the DataGridView which I have in that page, and when I go backwards (by Key.Left) and reach [0] I want to have [tc.TabCount - 1] enabled, which is the last one. The code for this case is:
tc.TabPages[tc.TabCount - 1].Enabled = false;
tc.SelectTab(tc.TabCount - 1);
tc.TabPages[tc.TabCount - 1].Enabled = true;
The complete piece of code is:
private bool KeyTc(System.Windows.Forms.Keys keyData)
{
if (keyData == K.Left && tc.SelectedIndex == 0)
{
tc.TabPages[tc.TabCount - 1].Enabled = false;
tc.SelectTab(tc.TabCount - 1);
tc.TabPages[tc.TabCount - 1].Enabled = true;
return true;
}
else if (keyData == K.Right && tc.SelectedIndex == tc.TabCount - 1)
{
tc.TabPages[0].Enabled = false;
tc.SelectTab(0);
tc.TabPages[0].Enabled = true;
return true;
}
return false;
}
This bool KeyTc is returned to a case in a switch statement for key evaluation in:
protected override bool ProcessCmdKey(ref Message keyMsg, Keys keyData)
{ switch keyData { ... } }
Base on the solution proposed by "Jeff Ogata : You can try disabling the TabControl before setting the selected tab, then re-enabling it. This will prevent it from taking focus", here bellow my solution:
tabMain.SelectedPageChanging += (s, e) =>
{
tabMain.Enabled = false;
};
tabMain.SelectedPageChanged += (s, e) =>
{
tabMain.Enabled = true;
};
Note: this code is using DevExpress "DevExpress.XtraTab.XtraTabControl".