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

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

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;
}
}
}

How to specify interaction beetween button click event handlers?

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");
}
}

How to change Visible panel?

I want create a Tab Panel with link buttons, my code is:
// create title panel
for (int i = 0; i < 5; i++)
{
Ctrl.Controls.Add(new LiteralControl(string.Concat("<li role=\"presentation\" class=\"rightcolomn\">")));
LinkButton lb = new LinkButton();
lb.ID = i.ToString();
lb.Text =i.ToString();
lb.Click += new EventHandler(this.lbnTitle_Click);
Ctrl.Controls.Add(lb);
Ctrl.Controls.Add(new LiteralControl("</li>"));
}
// this code we create panels
for (int i = 0; i < 5; i++)
{
Panel pn = new Panel();
pn.ID = "p" + i.ToString();
for (int j = 0; j < 3; j++)
{
//some code add to panel
}
pn.Visible = false;
Ctrl.Controls.Add(pn);
}
// code for link button click is:
protected void lbnTitle_Click(object sender, EventArgs e)
{
LinkButton ClickedLink = (LinkButton)sender;
}
When I click the link button, I want the corresponding panel to be set to visible, something like this: panel("p"+linkbutton.ID).Visible=true
how can access to panel and do it?
Ctrl is an asp:PlaceHolder.
Don't use control-IDs with numbers only, it's unlikely that they will be unique and it's also not very meaningful. However, use FindControl on the NamingContainer.
protected void lbnTitle_Click(object sender, EventArgs e)
{
LinkButton ClickedLink = (LinkButton) sender;
Control container = ClickedLink.NamingContainer;
Panel panel = (Panel) container.FindControl("p" + ClickedLink.ID);
panel.Visible = true;
}
string placeHolderId = "...";
string panelId = "p" + linkbutton.ID;
(this.Form.FindControl("placeHolderId").FindControl("panelId") as Panel).Visible=true;

I am having trouble getting a URL to switch in this windows forms program

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.

Populate form with controls based on int value

I was wondering how to go about doing something such as this:
I need to create a Form with a specific number of buttons based on an integer value representing the number of buttons needed, then give them their own specific names so that each can have their own unique event handlers.
A real example I can think of doing this would be the Windows log-in screen, where the number of controls created is based on the number of users and whether there is a Guest account or not. How do you think that they programmed that?
Thank you.
for (int i = 0; i < 5; i++)
{
Button newButton = new Button();
newButton.Name = "button" + i.ToString();
newButton.Text = "Button #" + i.ToString();
newButton.Location = new Point(32, i * 32);
newButton.Click += new EventHandler(button1_Click);
this.Controls.Add(newButton);
}
private void button1_Click(object sender, EventArgs e)
{
if (((Button)sender).Name == "button0")
MessageBox.Show("Button 0");
else if (((Button)sender).Name == "button1")
MessageBox.Show("Button 1");
}
Somehow you have to define the names of all the buttons. I would suggest you to create a new string array and write the button names inside, and then use them in buttons creation loop:
//do the same length as the for loop below:
string[] buttonNames = { "button1", "button2", "button3", "button4", "button5" };
for (int i = 0; i < buttonNames.Lenght; i++)
{
Button newButton = new Button();
newButton.Name = "button" + i.ToString();
newButton.Text = buttonNames[i]; //each button will now get its own name from array
newButton.Location = new Point(32, i * 32);
newbutton.Size = new Size(25,100); //maybe you can set different sizes too (especially for X axes)
newButton.Click += new EventHandler(buttons_Click);
this.Controls.Add(newButton);
}
private void buttons_Click(object sender, EventArgs e)
{
Button btn = sender as Button
MessageBox.Show("You clicked button: " + btn.Text + ".");
}

Categories

Resources