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");
}
}
Related
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;
}
}
}
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.
I have some problems making the HangMan game to work. Since i got my buttons, i had to make a method that displays the alphabet letters to user. So i have this Guessing method what i want to add to a button click event. So i get this red squiqqly line when i try to add a method to a button. The error is in HangMan_OnLoaded method. Thanks!
public void DisplayTheWord()
{
WrongGuesses = 0;
BitmapImage Hangman2 = new BitmapImage();
Uri URL = new Uri(BaseUri, images[WrongGuesses]);
Hangman2.UriSource = URL;
picture.Source = Hangman2;
string[] ReadWords = File.ReadAllLines("EnglishWords.txt");
int NextNumber = (new Random().Next(words.Length));
copyCurrent = "";
current = words[NextNumber];
for (int i = 0; i < ReadWords[NextNumber].Length; i++)
{
copyCurrent += "_" + " ";
}
CopiedWord.Text = copyCurrent;
}
private void Hangman_OnLoaded()
{
const int btnSize = 35;
var c = 0;
for (var i = 65; i <= 90; i++)
{
var btn = new Button();
btn.Content = (char)i;
btn.Width = btn.Height = btnSize;
var margin = btn.Margin;
margin.Left = c += 37;
btn.Margin = margin;
GridMain.Children.Add(btn);
btn.Click += Guessing();
}
}
private void Guessing(object sender, EventArgs e)
{
for (var i = 65; i <= 90; i++)
{
var btn = new Button();
btn = sender as Button;
btn.Content = (char) i;
var choice = btn.ToString();
if (copyCurrent.Contains(choice))
{
char[] temp = copyCurrent.ToCharArray();
char[] find = current.ToCharArray();
char guessChar = choice.ElementAt(0);
for (int index = 0; index < find.Length; index++)
{
if (find[index]== guessChar)
{
temp[index] = guessChar;
}
}
copyCurrent = new string(temp);
}
else
{
WrongGuesses++;
}
if (WrongGuesses < 6)
{
}
}
}
private void DisplayCopy()
{
CopiedWord.Text = "";
for (int index = 0; index < copyCurrent.Length; index++)
{
CopiedWord.Text += copyCurrent.Substring(index, 1);
CopiedWord.Text += " ";
}
}
You need to remove the brackets from the line:
btn.Click += Guessing();
so that it becomes:
btn.Click += Guessing;
#swatsonpicken said right. you need to remove the brackets from the line:
btn.Click += Guessing();
and replace with:
btn.Click += Guessing;
And one more thing you will need to correct that is the
private void Guessing(object sender, EventArgs e)
Write above line as below:
private void Guessing(object sender, RoutedEventArgs e)
Hope this will help. :)
I think:
The error is because Guessing is void and dont return nothing but yor're using Guessing like a method that return a event:
btn.Click += Guessing();
For dix that return a value event :)
Make sure your handler fits to Event you want to use:
private void Guessing(object sender, RoutedEventArgs e) {
//your handler code
}
btn.Click += Guessing;
Reason for that is, each event is expecting signature of delegate and defines parameters.
This function dynamically creates nine buttons for use in a game I am making. You can see what attributes I give to the button.
private void createbuttons()
{
int tot = 0;
int x = 100;
int y = 100;
while(tot < 9)
{
string buttonsname = (tot + "button").ToString();
Button creating = new Button();
creating.Name = buttonsname;
creating.Size = new Size(100, 100);
creating.Click += delegate
{
MessageBox.Show("You clicked me!");
};
creating.Text = buttonsname;
if(x > 300)
{
y += 100;
x = 100;
}
creating.Location = new Point(x, y);
Controls.Add(creating);
tot += 1;
x += 100;
}
}
What I want to know is how to reference these buttons in different parts of the same form. Specifically when 'Start Game' is clicked I want to change the text for each button to something different.
private void button10_Click(object sender, EventArgs e)
{
//What would I write here to change the text?
}
You can access the buttons by enumerating the controls, or you could create a list of buttons for future reference, and use that list later.
Here is how you do it with a list:
private IList<Button> addedButtons = new List<Button>();
private void createbuttons() {
int tot = 0;
int x = 100;
int y = 100;
while(tot < 9) {
string buttonsname = (tot + "button").ToString();
Button creating = new Button();
creating.Name = buttonsname;
creating.Size = new Size(100, 100);
creating.Click += delegate {
MessageBox.Show("You clicked me!");
};
creating.Text = buttonsname;
if(x > 300) {
y += 100;
x = 100;
}
creating.Location = new Point(x, y);
addedButtons.Add(creating); // Save the button for future reference
Controls.Add(creating);
tot += 1;
x += 100;
}
}
Now you can do this:
foreach (var btn : addedButtons) {
btn.Text = "Changed "+btn.Text;
}
The form has a property Controls that holds all child controls. To find a child control by its Name property use method Find, which returns the array, because there may be several control with the same Name, but if you make sure that names exist, are unique, and you know their type (Button) you can just take the first item from the array and cast it:
private void button10_Click(object sender, EventArgs e)
{
Button buttonNamedFred = (Button)this.Controls.Find("Fred", false)[0];
buttonNamedFred.Text = "I'm Fred";
}
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