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.
Related
When I click on a button in my Floqlayoutpanel they should hide at the place where I clicked on them. But instead they disappear and all the other buttons move.
They should hide at their place
But this is what happens
How I create my Buttons:
private void CreateButton()
{
int buttonIndex = 0;
for (int i = 0; i < 16; i++)
{
Button button = new Button();
button.Name = $"Button_{buttonIndex}";
button.Width = 100;
button.Height = 100;
button.Click += OnButtonClick;
button.BackgroundImage = BackSideImage();
flowLayoutPanel1.Controls.Add(button);
buttonIndex++;
}
}
How I hide my Buttons:
private void CompareCards()
{
if (clickedCards.Count >= 3)
{
if (clickedCards[0].PairIndex == clickedCards[1].PairIndex)
{
clickedCards[0].Button.Hide();
clickedCards[1].Button.Hide();
}
else
{
clickedCards[0].Button.BackgroundImage = BackSideImage();
clickedCards[1].Button.BackgroundImage = BackSideImage();
}
clickedCards.Clear();
}
}
Instead of hiding your button, you can make it invisible like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
for (int x = 0; x < 9; x++)
{
var button = new Button
{
Name = "Test-" + x,
Text = "Test-" + x,
Width = 100,
Height = 100
};
button.Click += OnButtonClick;
flowLayoutPanel1.Controls.Add(button);
}
}
private void OnButtonClick(object sender, EventArgs e)
{
//Instead of this...
//((Button)sender).Hide();
//Do this...
var button = ((Button) sender);
button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.BorderColor = BackColor;
button.FlatAppearance.MouseOverBackColor = BackColor;
button.FlatAppearance.MouseDownBackColor = BackColor;
button.Text = string.Empty;
button.Enabled = false;
}
}
In the program, by clicking button2, is created a table and imlemented data insertion. Also created textbox and login button, which is used for reading data from this textbox. When the login button is pressed, next actions should happen:
MessageBox should display some text;
login.Text should be changed;
table, which was created by clicking button2 should be deleted.
I dont know actually how to get access from login.click event handler to table in button2. I guess it should be done somehow by using EventArgs, but i dont understand how. Also I thought about creating some variable outside button2 handler scope, and use it later, but I guess its a bad practise.
Please,tell me how to solve this, or maybe its just wrong decision to create windows forms components such a way? if it`s so, then how to?) here is my code:
private void button2_Click(object sender, EventArgs e)
{
label1.Hide();
label2.Hide();
textBox1.Hide();
textBox2.Hide();
button2.Hide();
int user_count = Int32.Parse(textBox2.Text);
int file_count = Int32.Parse(textBox1.Text);
DataGridView T = new DataGridView();
T.Dock = DockStyle.Top;
T.AutoResizeColumns();
T.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
T.ColumnCount = file_count + 1;
T.RowCount = user_count + 1;
Controls.Add(T);
Controller_cs c = new Controller_cs(user_count, file_count);
for(int i = 1; i < T.RowCount; i++)
{
T.Rows[i].Cells[0].Value = c.user_name_insertion(i-1);
}
for (int i = 1; i < T.ColumnCount; i++)
{
T.Rows[0].Cells[i].Value = c.file_name_insertion(i - 1);
}
for(int i = 1; i < T.RowCount;i++)
{
for(int j = 1; j < T.ColumnCount;j++)
{
T.Rows[i].Cells[j].Value = c.rigts_insertion(j-1,i-1);
}
}
Label l = new Label();
l.Text = "Name";
l.Left = 20;
l.Top = 180;
Controls.Add(l);
TextBox username = new TextBox();
username.Left = 20;
username.Top = 210;
Controls.Add(username);
Button login = new Button();
login.Text = "Enter";
login.Left = 130;
login.Top = 175;
login.Click += login_handler;
Controls.Add(login);
}
private void login_handler(object sender, EventArgs e)
{
Button b = (Button)sender;
if (b.Text == "Enter")
{
b.Text = "Exit";
MessageBox.Show("Enter is done");
}
else
{
b.Text = "Enter";
MessageBox.Show("Quit is done");
}
}
This question already has answers here:
How to add event handler for dynamically created controls at runtime?
(3 answers)
How to add an event to a UserControl in C#?
(6 answers)
Closed 3 years ago.
I am trying to write a C# program that plays checkers. So far I've completed all the "Shallow" things such as generating the board and all the labels etc.
My problem is that all the controls are dynamically added. So I can't just "double click" them and define what to do at Button_Click event.
This is my code for generating the form
public partial class formGameBoard : Form
{
private readonly int m_BoardSize;
private readonly string m_Player1Name;
private readonly string m_Player2Name;
private GameTile m_BlueTile;
private bool m_IsThereBlue;
public formGameBoard(int i_BoardSize, string i_Player1Name, string i_Player2Name)
{
m_BoardSize = i_BoardSize;
m_Player1Name = i_Player1Name;
m_Player2Name = i_Player2Name;
if (m_Player2Name == "(Computer)")
{
m_Player2Name = "Computer";
}
m_IsThereBlue = false;
InitializeComponent();
}
private void formGameBoard_Load(object sender, EventArgs e)
{
int SizeOfButton = 60;
int ButtonRowindex = 0;
int ButtonColindex = 0;
this.Size = new System.Drawing.Size(30 + m_BoardSize * SizeOfButton, 100 + m_BoardSize * SizeOfButton);
Button[,] PlayButtonArray = new Button[m_BoardSize, m_BoardSize];
for (ButtonRowindex = 0; ButtonRowindex < m_BoardSize; ButtonRowindex++)
{
for (ButtonColindex = 0; ButtonColindex < m_BoardSize; ButtonColindex++)
{
PlayButtonArray[ButtonRowindex, ButtonColindex] = new Button();
PlayButtonArray[ButtonRowindex, ButtonColindex].Size = new Size(SizeOfButton, SizeOfButton);
PlayButtonArray[ButtonRowindex, ButtonColindex].Left = 10 + ButtonRowindex * SizeOfButton;
PlayButtonArray[ButtonRowindex, ButtonColindex].Top = 50 + ButtonColindex * SizeOfButton;
if ((ButtonRowindex + ButtonColindex) % 2 == 0)
{
PlayButtonArray[ButtonRowindex, ButtonColindex].Enabled = false;
PlayButtonArray[ButtonRowindex, ButtonColindex].BackColor = Color.Gray;
}
this.Controls.Add(PlayButtonArray[ButtonRowindex, ButtonColindex]);
}
}
FillButtons(PlayButtonArray);
}
public void FillButtons(Button[,] ButtonMatrix)
{
int i, j;
for (i = 0; i < m_BoardSize; i++)
{
for (j = 0; j < m_BoardSize; j++)
{
if ((i + j) % 2 == 1)
{
if (j <= (m_BoardSize / 2) - 2)
{
ButtonMatrix[i, j].Text = "O";
}
if (j > (m_BoardSize / 2))
{
ButtonMatrix[i, j].Text = "X";
}
}
}
}
}
struct GameTile
{
int RowIndex;
int ColumnIndex;
}
}
}
I have no issues with it, it looks very nice in my opinion
My problem is that all those buttons are dynamically added. I didnt drag and drop them. I created them at form load. Now I want something to happen when I click a button. For example, I want the button I clicked to change its color to blue.
How can I do that?
Adding the click event for a button is very simple, like this:
buttonName.Click += (sender, e) => { buttonName.Foreground = Colors.Blue; };
Or if you want to make it a method that will handle many button clicks, you could do this:
buttonName.Click += YourButtonHandler;
private void YourButtonHandler(object sender, EventArgs e)
{
// do something, for example:
Button b = sender as Button;
b.Foreground = Colors.Blue;
}
You can add a handler to Click event of your buttons and use sender parameter.
Also probably the index of button in the array is important for you, so you can store array index of the button in Tag property and use it later.
In the for loop:
var button = PlayButtonArray[ButtonRowindex, ButtonColindex];
button.Tag= new Point(ButtonRowindex, ButtonColindex);
button.Click += Button_Click;
Code for Button_Click:
private void Button_Click(object sender, EventArgs e)
{
var button = sender as Button;
//You can manipulate button here
//Also to extract the button index in array:
var indexes = (Point)button.Tag;
MessageBox.Show(string.Format("This is the button at {0}, {1}", indexes.X, indexes.Y));
}
The program has a label, two radio buttons, and a set of generated buttons from A-Z. There are two URLs I would like to use that have a text list of names. When You click a lettered button, the program splits the text list and displays them in the label.
The two radio buttons are supposed to switch the URL being used. One URL has male first names, the other url has female first names. However, I am not sure how to get this to work...as I have it now, choosing the radio button does nothing... it will stay on whatever the first URL was.
using System;
using System.Windows.Forms;
using System.Net;
namespace IndexTable
{
public partial class frmIndex : Form
{
public frmIndex()
{
InitializeComponent();
}
private void Button_Click(object sender, EventArgs e)
{
Button btn = sender as Button; // Convert datatype
label1.Text = btn.Text + "\n"; ;
foreach (string c in name)
{
if (c != "" && c.Substring(0, 1) == btn.Text)
{
label1.Text += c + "\n";
//listBox1.Items.Add(c);
}
}
}
string[] name;
private void Form1_Load(object sender, EventArgs e)
{
int top = 10;
int left = 20;
int width = 30;
for (char i = 'A'; i <= 'M'; i++)
{
Button button = new Button();
button.Left = left;
button.Top = top;
button.Width = width;
button.Text = i.ToString();
this.Controls.Add(button);
button.Click += new System.EventHandler(this.Button_Click);
top += button.Height + 2;
}
left = 50;
top = 10;
for (char i = 'N'; i <= 'Z'; i++)
{
Button button = new Button();
button.Left = left;
button.Top = top;
button.Width = width;
button.Text = i.ToString();
this.Controls.Add(button);
button.Click += new System.EventHandler(this.Button_Click);
top += button.Height + 2;
}
WebClient wc = new WebClient();
string names = wc.DownloadString(url);
name = names.Split('\n');
}
private string url = "http://www.cs.cmu.edu/Groups/AI/areas/nlp/corpora/names/male.txt";
private void btnBoy_CheckedChanged(object sender, EventArgs e)
{
url = "http://scrapmaker.com/data/wordlists/names/male-names.txt";
}
private void btnGirl_CheckedChanged(object sender, EventArgs e)
{
url = "http://scrapmaker.com/data/wordlists/names/female-names.txt";
}
}
}
The CheckedChanged event fires whether the button becomes checked or becomes unchecked. Therefore you need to add a line of code to see what the current check state of the button is before responding:
private void btnBoy_CheckedChanged(object sender, EventArgs e)
{
if (btnBoy.Checked)
url = "http://scrapmaker.com/data/wordlists/names/male-names.txt";
}
EDIT: also you only use the url once, in the form load event, with the line
wc.DownloadString(url)
You'll need to add code to actually use the url again after you change it with the radiobuttons--either in the radiobutton event handler, or the other button event handler for instance.
I have an array of buttons, like this:
int x = 0, y = 0;
butt2 = new Button[100];
for (int i = 0; i < 100; i++)
{
butt2[i] = new Button();
int names = i;
butt2[i].Name = "b2" + names.ToString();
butt2[i].Location = new Point(525 + (x * 31), 70 + (y * 21));
butt2[i].Visible = true;
butt2[i].Size = new Size(30, 20);
butt2[i].Click += new EventHandler(butt2_2_Click); //problem lies here (1)
this.Controls.Add(butt2[i]);
}
private void butt2_2_Click(object sender, EventArgs e)
{
// want code here
}
I want to change the back color of the button when clicked. I was thinking of passing i to be able to do this:
butt2[i].BackColor = Color.Green;
This should do the trick:
private void butt2_2_Click(object sender, EventArgs e)
{
Button pushedBtn = sender as Button;
if(pushedBtn != null)
{
pushedBtn.BackColor = Color.Green;
}
}
And this holds for most UI events, the 'object sender' parameter refers to the control that 'sent'/'fired' the event.
To learn more about C# event handling, I would start here.
Also, here is a SO question about GUI event handling, answered nicely by Juliet (accepted answer).
Hope this helps.