Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
private void opc(object sender, EventArgs e)
{
//NOTE: buttons 13-16 share same click event
// needs for each loop
button13.Enabled = false;
button14.Enabled = false;
button15.Enabled = false;
button16.Enabled = false;
}
You can try this.
private void opc(object sender, EventArgs e)
{
for (int i = 13; i <= 16; i++)
{
Control[] buttons = this.Controls.Find(String.Format("button{0}", i), false);
if (buttons.Length > 0)
{
Button btn = (buttons[0] as Button);
btn.Click += btn_Click;
btn.Enabled = true; // or false
}
}
}
void btn_Click(object sender, EventArgs e)
{
MessageBox.Show((sender as Button).Name);
}
See code below :
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button13.Click += new System.EventHandler(this.button_Click);
this.button14.Click += new System.EventHandler(this.button_Click);
this.button15.Click += new System.EventHandler(this.button_Click);
this.button16.Click += new System.EventHandler(this.button_Click);
}
private void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
string name = button.Text;
}
}
}
You want to put the assignmens into a loop? With statically defined names for the buttons that is not possible AFAIK. You should put the buttons into an array or collection in the first place:
Button[] button = new Button[numberOfButtons];
for (int i=0; i<numberOfButtons; i++)
{
button[i] = new Button();
button[i].Click += new System.EventHandler(WhateverYouLike);
// ... some other property assignments, probably also rule-based
}
// ... and at a later time:
for (int i=0; i<numberOfButtons; i++) button[i].Enabled = false;
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a pictureBox that I use as a button. When I start the form I load it as disabled and after pressing a button I activate it, it works to change the image from disabled to activated. Then, when I disable this pictureBox again, the image doesn't change anymore ... what could be wrong?
here's my code:
private void btnUpdate_EnabledChanged(object sender, EventArgs e)
{
if (btnUpdate.Enabled == true)
{
if (mt.ArquivoExiste(Metodos.pathImagens, botaoUpdateNormal))
{
Image bt = Image.FromFile(Metodos.pathImagens + botaoUpdateNormal);
btnUpdate.BackgroundImage = bt;
}
}
else
{
if (mt.ArquivoExiste(Metodos.pathImagens, botaoUpdateDisabled))
{
Image bt = Image.FromFile(Metodos.pathImagens + botaoUpdateDisabled);
btnUpdate.BackgroundImage = bt;
}
}
}
Edit:
I changed the string to make that easier and put the entire relationated code:
string botaoUpdateNormal = "btnUpdate_normal.png", botaoUpdateDisabled = "btnUpdate_disabled.png",
botaoUpdateFocus = "btnUpdate_focus.png", botaoSearchNormal = "btnSearch_normal.png",
botaoSearchFocus = "btnSearch_focus.png", botaoInsertNormal = "btnInsert_normal.png",
botaoInsertFocus = "btnInsert_focus.png";
then i load the form:
private void IEstoque_Load(object sender, EventArgs e)
{
if (mt.ArquivoExiste(Metodos.pathImagens, botaoUpdateDisabled))
{
Image bt = Image.FromFile(Metodos.pathImagens + botaoUpdateDisabled);
btnUpdate.BackgroundImage = bt;
}
}
After that I have an event that when I change the row of a grid, the btnUpdate activates, and when I click on it and update my dataBase it desactivates.
This code work for pictureBox "like button" and with separate button:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.pictureBox.Enabled = false;
this.pictureBox.EnabledChanged += new System.EventHandler(this.pictureBox_EnabledChanged);
this.pictureBox.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseClick);
this.button.Click += new System.EventHandler(this.button_Click);
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);
}
private void pictureBox_EnabledChanged(object sender, EventArgs e)
{
if (pictureBox.Enabled == true)
{
var botao = "1.png";
Image bt = Image.FromFile(botao);
pictureBox.BackgroundImage = bt;
}
else
{
var botao = "2.png";
Image bt = Image.FromFile(botao);
pictureBox.BackgroundImage = bt;
}
}
private void button_Click(object sender, EventArgs e)
{
pictureBox.Enabled = !pictureBox.Enabled;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (!MouseIsPicture(e.Location)) return;
TogglePicture();
}
private void pictureBox_MouseClick(object sender, MouseEventArgs e)
{
TogglePicture();
}
private bool MouseIsPicture(Point location)
{
// Make sure the location is over the image.
if (location.X < 0) return false;
if (location.Y < 0) return false;
if (location.X >= pictureBox.Width) return false;
if (location.Y >= pictureBox.Height) return false;
return true;
}
void TogglePicture()
{
pictureBox.Enabled = !pictureBox.Enabled;
}
}
More over: check if pictures you are loading are not the same
I solve it! before deactivating the button I called a form as follows: formname.Show ();
I just changed it to formname.ShowDialog (); and it worked normally.
so I created my own private void that can create buttons
private void addButtonsToForm()
{
for (int i = 0; i < 26; i++)
{
Button currentNewButton = new Button();
currentNewButton.Size = new Size(20, 30);
currentNewButton.Location = new Point(20 + 25 * i, 420);
currentNewButton.Text = ((char)(65 + i)).ToString();
currentNewButton.Click += LetterClicked;
letters[i] = currentNewButton;
this.Controls.Add(letters[i]);
}
}
The buttons are alphabets and will be accessed when the user wants to choose a letter ... but the problem is I'm trying to figure out how to go back when the user clicked or selected a button..
Originally I wanted to do was i could just hide all the buttons created and just make the previous button visible but for some reason it only hides the only button that is clicked
//this is under private void LetterClicked(object sender, EventArgs e)
Button selectedLetter = (Button)sender;
selectedLetter.Enabled = false;
i thought of stupid codes like
addbuttonstoform().visible = false; but of course that won't work.. but you might get an idea to where i want to go.... it's a bit confusing to explain... I'm new in c# and i'm creating a guess the word game so help could be great..
Here is a working solution for your problem. See below:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Alphabets
{
public partial class Form1 : Form
{
private Button[] _letters;
public Form1()
{
InitializeComponent();
AddButtonsToForm();
}
private void AddButtonsToForm()
{
_letters = new Button[26];
for (int i = 0; i < 26; i++)
{
Button currentNewButton = new Button
{
Name = "BtnLetter"+ ((char)(65 + i)),
Size = new Size(20, 30),
Location = new Point(20 + 25 * i, 420),
Text = ((char) (65 + i)).ToString()
};
currentNewButton.Click += LetterClicked;
_letters[i] = currentNewButton;
this.Controls.Add(_letters[i]);
}
}
private void LetterClicked(object sender, EventArgs e)
{
var selectedLetter = (Button) sender;
//hide all other buttons
foreach (var letter in _letters)
{
if (letter.Text != selectedLetter.Text)
{
var buttons = this.Controls.Find("BtnLetter" + letter.Text, true);
buttons[0].Enabled = false;
}
}
}
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am working on a mini clicker game, it is not anything big, but i am having a problem with enabling my button, but i can disable it. I am still learning and i think it is okay to ask stupid questions like this. :D
Here is my Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Diamond_Clicker
{
public partial class Form1 : Form
{
private int clicks = 0;
private int counter = 1;
public Form1()
{
InitializeComponent();
}
private void myDiamond_MouseUp(object sender, MouseEventArgs e)
{
myDiamond.Image = Image.FromFile("C:\\Matej Dodevski\\Semos\\C#\\Diamond Clicker\\diamond.png");
}
private void myDiamond_MouseDown(object sender, MouseEventArgs e)
{
myDiamond.Image = Image.FromFile("C:\\Matej Dodevski\\Semos\\C#\\Diamond Clicker\\diamondMouseUp.png");
clicks++;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
clicks++;
}
private void timer1_Tick_1(object sender, EventArgs e)
{
counter++;
clicks = clicks + 1;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
clicks = clicks - 50;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
timer1.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
if (clicks > 5)
{
button1.Enabled = true;
}
else
button1.Enabled = false;
}
}
}
The Load Event is intended to get executed one time, and that's just before the form is displayed on the screen. Usually this event is where you would do some kind of one time initialization.
What you need to do instead is put that code into a function:
private void UpdateButton()
{
if (clicks > 5)
button1.Enabled = true;
else button1.Enabled = false;
// This function can be reduced to one line of code:
// button1.Enabled = clicks > 5;
}
Then you need to call this function at the end of your button1_click function, timer1_tick function, mousedown function and your timer1_tick_1 functions. Basically, into any function where the clicks variable can change.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How do I write a code that allows me to count the number of numbers that i put in a text box.
For example I have a form , a button and a text box. I type in 1 in the text box; press the button. Type in 3; press the button. Type in 5; press the button. and when I close out my form, a message box shows up saying i have 3 numbers.
code so far for form 1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnReadings_Click(object sender, EventArgs e)
{
using (Form2 f2 = new Form2())
{
while (f2.ShowDialog() != DialogResult.OK)
{
this.Enabled = false;
}
this.Enabled = true;
}
}
}
form 2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
this.DialogResult = DialogResult.OK;
}
private void button1_Click(object sender, EventArgs e)
{
}
}
You need to handle the form's Closing event, like this:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
int countOfNumbers = 0;
foreach(char c in textBox1.Text)
{
if(Char.IsDigit(c))
{
countOfNumbers += 1;
}
}
// Display a MsgBox asking the user to save changes or abort.
MessageBox.Show("Number of numbers in text box is: " + countOfNumbers.ToString());
}
I'm trying to create 4 buttons on my form when I click on button1, but the buttons don’t show up. Why not?
private void button1_Click(object sender, EventArgs e)
{
Button[] b = new Button[4];
for (int i=0; i < 4; i++)
{
b[i] = new Button();
b[i].Name = "button" + i;
b[i].Location = new Point(43, 39 + 10 * i);
b[i].Size = new Size(158, 48);
}
}
You have only created them, but you also need to add them to your form with: this.Controls.Add(b[i]);
private void button1_Click(object sender, EventArgs e)
{
Button[] b = new Button[4];
for (int i=0; i < 4; i++)
{
b[i] = new Button();
b[i].Name = "button" + i;
b[i].Location = new Point(43, 39 + 10 * i);
b[i].Size = new Size(158, 48);
this.Controls.Add(b[i]);
}
}
All you do is create an array of buttons, and assign buttons at indices. Your form knows nothing of these buttons, they could be an array of integers or anything for all that matters at this point. You will need to put them in the form's container:
Controls.Add(b[i]);
Now your form will take ownership of them, managing disposal when the container is disposed.
Try this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace winFormButtons
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Button[] b = new Button[4];
for (int i = 0; i < 4; i++)
{
b[i] = new Button();
b[i].Name = "button" + i;
b[i].Location = new Point(43, 39 + 10 * i);
b[i].Size = new Size(158, 48);
b[i].Click += new EventHandler(OnClick);
this.Controls.Add(b[i]);
}
}
public void OnClick(object sender, EventArgs e)
{
MessageBox.Show("Hello Handler:" + ((Button)sender).Name);
}
}
}
Create a Panel on your form. and add this line in your code
panel1.Controls.Add(b[i])