Change background of a button when clicking another button - c#

I am currently experimenting in WPF and just created a UniformGrid with 800 buttons which are created in a for loop. All buttons have their own names and share the same click event.
What I want to do now is the following: I want to click the first button (rect0) to change the color of this button and the next one (rect1).
I am totally stuck right now because everything I write into the click event refers to the button I clicked.
public MainWindow()
{
InitializeComponent();
for (int i = 0; i < 800; i++)
{
Button BTN_rect = new Button()
{
Name = "rect" + i,
Background = Brushes.White,
};
BTN_rect.Click += BTN_rect_Click;
Uniform.Children.Add(BTN_rect);
}
}
private void BTN_rect_Click(object sender, RoutedEventArgs e)
{
Button BTN_rect = sender as Button;
BTN_rect.Background = Brushes.Red;
MessageBox.Show(BTN_rect.Name);
}

There are a load of ways to do this.
I took a shortcut and put just 9 buttons in a stackpanel, otherwise the same.
public MainWindow()
{
InitializeComponent();
for (int i = 0; i < 8; i++)
{
Button BTN_rect = new Button()
{
Name = "rect" + i,
Content =Name,
Tag = i,
Background = Brushes.White,
};
BTN_rect.Click += BTN_rect_Click;
sp.Children.Add(BTN_rect);
}
}
private void BTN_rect_Click(object sender, RoutedEventArgs e)
{
Button current = sender as Button;
current.Background = Brushes.Red;
string targetName = $"rect{((int)current.Tag) + 1}";
Button nextButton = sp.Children.OfType<Button>().Where(x => x.Name == targetName).SingleOrDefault();
nextButton.Background = Brushes.Red;
}
Usually, you'd template data into repeated controls rather than add them in code, btw.

Related

How to deselect all controls in a form?

I have dynamically created a list of buttons and set it to the form. After clicking one of them, the very next button to it will appear as selected.
Is it possible to deselect all controls dynamically created in a form? Particularly, can I, somehow, deselect that button after clicking the one before it?
private void GenerateButton()
{
for (int i = 1; i <= 15; ++i)
{
for (int j = 1; j <= 25; ++j)
{
Button button = new Button();
button.Location = p;
button.Size = size;
button.BackColor = Color.RoyalBlue;
button.Padding = pad;
button.Click += new EventHandler(button_Click);
this.Controls.Add(button);
p.X += 23;
}
p.Y += 23;
p.X = 0;
}
}
protected void button_Click(object sender, EventArgs e)
{
Button but = sender as Button;
but.Enabled = false;
but.BackColor = Color.LightGray;
}
You might try this method, which will select the next button in the form, after clicking on a button. You need to attach this button event handler to all your buttons. The order of buttons is determined by their order in the form Controls list. If you want a specific order, you will need to use LINQ to sort Controls by their TabOrder property and ensure your TabOrder is set properly
private void button_Click(object sender, EventArgs e)
{
var btn = (Control)sender;
btn.Enabled = false;
btn.BackColor = Color.LightGray;
// Where is this button in the form?
var indexOfThisButton = this.Controls.IndexOf(btn);
// Find next button index
for (int i = indexOfThisButton+1; i < this.Controls.Count; i ++)
{
// If it's a button, select it
if (this.Controls[i] is Button)
{
this.Controls[i].Select();
return;
}
}
// If we got down here we have got to the end of the controls list, start again
for (int i = 0; i < this.Controls.Count; i++)
{
// If it's a button, select it
if (this.Controls[i] is Button)
{
this.Controls[i].Select();
return;
}
}
}

I want to make a my own button which has 2 other button inside and I want to make them transparent but transparency doesn't work

I have a button, I put 2 other buttons inside it. I want those 2 other buttons to only appear when I enter the main button with my mouse. When I enter it, I want the 2 other buttons to be half opaque and only be fully opaque when I enter one of those 2 buttons.
These buttons are inside a FlowLayoutPanel with a background image on it.
This is how they look like:
The Buttons have a picture inside them and a text.
Here is my code:
public class MyButton : Button
{
public MyButton()
{
SetStyle(ControlStyles.StandardClick |
ControlStyles.StandardDoubleClick, true);
Text = component.ProductsName;
TextAlign = ContentAlignment.TopCenter;
ImageAlign = ContentAlignment.TopLeft;
Size = new Size(178, 75);
foreach (Button item in CustomButtons())
{
Controls.Add(item);
}
}
static Button[] CustomButtons()
{
Button delete = new Button();
delete.Location = new Point(157, 1);
delete.Size = new Size(20, 20);
delete.MouseEnter += OnMouseEnter;
delete.MouseLeave += DeleteOnMouseLeave;
Button customize = new Button();
customize.Location = new Point(delete.Left - 20, 1);
customize.Size = new Size(20, 20);
Button[] buttons = {delete, customize};
return buttons;
}
private static void DeleteOnMouseLeave(object sender, EventArgs e)
{
Button btn = (Button) sender;
btn.UseVisualStyleBackColor = true;
btn.BackColor = Color.Transparent;
}
private static void OnMouseEnter(object sender, EventArgs e)
{
Button btn = (Button) sender;
btn.UseVisualStyleBackColor = false;
btn.FlatAppearance.MouseOverBackColor = Color.FromArgb(100,
Color.Black);
}
}
I think I tried everything that came to my mind, I tried events and everything and the buttons never worked as I intended them to work.
Any help would be appreciated! Thanks! :D
it seems I solved it! I only had to set Flatstyle = FlatStyle.Flat and backColor = Color.Transparent! :D
Here is the result: exsample of output

I can not go to "EventHandler" with double click

When btnAsset is double-clicked, it should go to allButton_Click.
But it only goes in one click. how can I do that?
public void Add(MainForm frm)
{
this.form1 = frm;
for (int i = 0; i < 10; i++)
{
btnAsset[i] = new Button();
btnAsset[i].Tag = i;
btnAsset[i].Name = "Asset-" + i.ToString();
btnAsset[i].Width = 150;
btnAsset[i].Height = 120;
btnAsset[i].Visible = true;
btnAsset[i].BackColor = Color.GreenYellow;
form1.flowLayoutVideo.Controls.Add(btnAsset[i]);
btnAsset[i].DoubleClick += new EventHandler(allButton_Click);
}
}
should go here when double clicked
void allButton_Click(object sender, EventArgs e)
{
Button p = sender as Button;
if (p != null)
{
int i = (int)p.Tag;
MessageBox.Show((i + 1).ToString() + ". seçildi");
}
}
Look what the docs says about it:
By default, the ControlStyles.StandardClick and ControlStyles.StandardDoubleClick style bits are set to false for the Button control, and the DoubleClick event is not raised.
You can change this behaviour by creating your own button class deriving from Button and change the style bits.

How to access or modify dynamically created button

I've created 8*8 arrays of button to create a grid for Minesweeper Game. After creating the grid I need to access them(buttons). How can I access them like change the name or disable click-ability or change the colors.
private void gridDesign()
{
/***********************************Uniform grid*********************/
int firstLoop, secondLoop;
for (firstLoop = 0; firstLoop < 8; firstLoop++)
{
for (secondLoop = 0; secondLoop < 8; secondLoop++)
{
Button lbl = new Button()
{
Name = "_" + firstLoop.ToString() +"_"+ secondLoop.ToString(),
Content = firstLoop.ToString() + " " + secondLoop.ToString(),
};
lbl.Click += button_Click;
uniformgridMinesweeper.Children.Add(lbl);
}
}
/********************************************************************/
}
private void button_Click(object sender, RoutedEventArgs e)
{
Button bbb = e.Source as Button;
bbb.Background = Brushes.Indigo;
//how can i access a button named "_5_5"
}
It is straightforward to access a child of a Grid with a specific property (Name):
var button = uniformgridMinesweeper.Children.OfType<Button>()
.FirstOrDefault(button => button.Name == "_5_5");
That said, you might want to take thumbmunkey's advice and put the buttons in your own Dictionary or lookup for quick and easy access. Or better yet, use data binding with a view model ...

How to make submit button appear after all text boxes are full?

I'm making a game with 6 dynamic buttons as "btn" at the top row and other 6 "lamp buttons" on a buttom row.
Player clicks on a top row button and text displayed in a bottom row button after clicking on it.
As soon as all the lamp buttons are full with text, I want a submit buttom to appear.
I tried to make for and foreach for my lamp buttons and it didn't help. Plese help!
here is a code:
public partial class Game : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
for (int i = 0; i < 6; i++)
{
Button btnLamp = new Button();
btnLamp.ID = "btnLamp" + i.ToString();
btnLamp.Click += btnLamp_Click;
this.Panel1.Controls.Add(btnLamp);
}
LiteralControl ltBreak = new LiteralControl();
ltBreak.Text = "<br/><br/>";
Panel1.Controls.Add(ltBreak);
Panel1.DataBind();
for (int i = 0; i < 6; i++)
{
Button btn = new Button();
btn.ID = "btn" + i.ToString();
btn.Text = "btn" + i.ToString();
btn.Click += btn_Click;
this.Panel2.Controls.Add(btn);
}
Panel2.DataBind();
}
void btn_Click(object sender, EventArgs e)
{
Button clickedbutton = (Button)sender;
string btn_cliked = clickedbutton.ID;
for (int i = 0; i <5 ; i++)
{
((Button)FindControl(("btn" + i.ToString()))).BackColor = System.Drawing.Color.LightSteelBlue;
}
clickedbutton.BackColor = System.Drawing.Color.Beige;
Session["clickedbutton"] = clickedbutton;
}
void btnLamp_Click(object sender, EventArgs e)
{
Button clickedbutton = (Button)sender;
string btnLamp_cliked = clickedbutton.ID;
((Button)FindControl(((Button)Session["clickedbutton"]).ID)).Enabled = false;
for (int i = 0; i < 5; i++)
{
if (((Button)Session["clickedbutton"]).Text.ToString() == ((Button)FindControl("btnLamp" + i)).Text)
{
((Button)FindControl("btnLamp" + i)).Text = "";
}
}
clickedbutton.Text = ((Button)Session["clickedbutton"]).Text.ToString();
}
This sort of stuff is best handled client side in javascript.
Attach a handler to the onchange event of your inputs and figure out inside it if
all inputs have value. Then show your button on page (should be present but hidden - display:none).
If none of the above makes any sense i suggest to do some research on web programming. Figure out the purpose of server code (C# in your case) versus html and javascript. Then come back with questions if needed

Categories

Resources