When clicking on a button, 5 textboxes will be displayed. I have to add a button for reply to the last textbox - can anyone show me how?
This is my code:
protected void GenTextBox(object sender, EventArgs e)
{
for (i = 1; i <= TotalReplys; i++)
{
HtmlGenericControl lineBreak = new HtmlGenericControl("br");
Page.Controls.Add(lineBreak);
TextBox MyTextBox = new TextBox();
MyTextBox.ID = i.ToString();
MyTextBox.Width = 540;
MyTextBox.Height = 60;
MyTextBox.Text = "Get the value from the database";
MyTextBox.TextMode = TextBoxMode.MultiLine;
Panel1.Controls.Add(MyTextBox);
Panel1.Controls.Add(lineBreak);
}
}
In general, you are on the right track. Use the same code that you have for generating a textbox, but repurpose it to generate a button.
Here are some hints to get you on the right track
if ( i == TotalReplys ){
Button MySearchButton = new Button();
//Set Button Properties
Panel1.Controls.Add(MySearchButton);
}
I imagine you are either hung up on the if-statement logic or perhaps not aware of the Button object. Either way, this should set you on the right track.
Related
everyone
I have this assignment at school where I have to make one of those alphabet puzzle games where you find a word in an alphabet soup. I don't really know the name for it.
I have 70 buttons. For example button12, button13, button14 and button 14 form the word "HOLA". As soon as people click those four buttons I want them (those four buttons) to be disabled, except if people clicked on another button which wasn't part of the word. I know I could program each button assigning a value to a variable and upping that value when I click the button. The problem is that I'd prefer not to do this on all 70 buttons since there are other requirements for the assignment and the code would be too long.
I tried a bunch of stuff but I've since then erased the code out of frustration.
This is an image of my form: enter image description here
private void button_Click(object sender, EventArgs e)
{
Button myButton = (Button)sender;
myButton.ForeColor = System.Drawing.Color.Red;
}
I have that event in all my buttons because I have to change the text color on each button when I click on them.
Edit: I did put all the buttons manually
You can try to create buttons and assign event hadlers in a loop:
for (int line = 0; line < 7; ++line) {
for (int column = 0; column < 10; ++column) {
Button button = new Button() {
Parent = this,
Text = "?", // Here you to generate button's text, e.g. with Random
Location = new Point(50 + line * 40, 50 + column * 40),
Size = new Size(30, 30),
};
button.Click += (ss, ee) => {
Button myButton = ss as Button;
myButton.ForeColor = System.Drawing.Color.Red;
};
}
}
If you put all the buttons on the form manually, you can assign event hadlers in a loop as well:
foreach (var button in Controls.OfType<Button>()) {
button.Click += (ss, ee) => {
Button myButton = ss as Button;
myButton.ForeColor = System.Drawing.Color.Red;
};
}
Hoping you can help - I have programmatically created button & richtextbox.
// Button to Edit
Button butEditToDo = new Button();
butEditToDo.Location = new Point(285, 10);
butEditToDo.Size = new System.Drawing.Size(25, 25);
butEditToDo.BackColor = Color.Transparent;
butEditToDo.FlatStyle = FlatStyle.Flat;
butEditToDo.FlatAppearance.BorderSize = 0;
butEditToDo.FlatAppearance.MouseOverBackColor = Color.FromArgb(244, 244, 244);
butEditToDo.Cursor = Cursors.Hand;
butEditToDo.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Edit_25));
pnlPendingNote.Controls.Add(butEditToDo);
// Pending Nane + Tag
RichTextBox rxtNotes = new RichTextBox();
rxtNotes.Size = new System.Drawing.Size(317, 68);
rxtNotes.Location = new Point(3, 37);
rxtNotes.Text = (read["notNote"].ToString());
rxtNotes.ReadOnly = true;
rxtNotes.BorderStyle = BorderStyle.None;
rxtNotes.DetectUrls = true;
rxtNotes.BackColor = Color.FromArgb(244, 244, 244);
pnlPendingNote.Controls.Add(rxtNotes);
So when ever I click on ButEditToDo_Click - I can get the right button clicked.
So when I click on this button I would like to enable the RichTextbox - and when I click the button again - I would like to update the database.
Button Click:
private void ButEditToDo_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
for (int i = 1; i < pendingcounter; i++)
{
if (btn.Name == ("PenNote" + i))
{
break;
}
}
}
Hope you can help please with enabling the button, I'm all good with the database.
Thank you.
Edit One
#Ed - thank you.
Please See Image.
What I would like to achieve - When i click on the tools icon - the RichTextBox will be enabled.
So if I click on the tools on first panel - then the R_TextBox will be enabled for me to edit the text.
Then the Icon will change and I will be able to click on it again to save to the database.
Hope that makes more sense for you Ed.
Just give the button an event handler that does stuff. Use a lambda so you can reference the local reference to the RichTextBox.
Button butEditToDo = new Button();
// ...snip...
RichTextBox rxtNotes = new RichTextBox();
// ...snip...
butEditToDo.Click += (sender, args) =>
{
CycleNoteState(rxtNotes);
};
And here's the guts of the event handler. You could put this all in the event handler, but the code's more readable this way. CycleNoteState isn't a very good name, but I'm not clear about the semantics of your program.
I may have misunderstood the logic for what the button does on successive clicks. If it's more complicated than this, you can introduce a state enum or something. Let me know and we'll get it figured out.
private void CycleNoteState(RichTextBox rtb)
{
if (!rtb.Enabled)
{
rtb.Enabled = true;
}
else
{
// Do save stuff here
}
}
How to create buttons dynamically after user input in C# (Visual Studio).
There is a text-box to enter how many buttons user wants?
Then my target is to create buttons below the input field as the user wants
then how can I get id's of that buttons?
private void button1_Click(object sender, EventArgs e)
{
List<Button> buttons = new List<Button>();
for (int i = 0; i < n; i++)
{
this.Controls.Add(buttons[i]);
}
}
Here I first added an event handler to the textbox, which is called whenever the text value is changed. The value is converted to the int value and then is used in a for loop statement. You can set your button's potion to the desired value using location property. Using tag or name property you can assign a unique value to your buttons. I hope the code helps.
Look at the code below :
private void Form1_Load(object sender, EventArgs e)
{
textBox1.TextChanged += textBox1_TextChanged;
}
void textBox1_TextChanged(object sender, EventArgs e)
{
var txtBox = sender as TextBox;
if (txtBox == null) return;
var count = Convert.ToInt16(txtBox.Text);
//
var xPosition = 0;
for (var i = 1; i <= count; i++)
{
var button = new Button
{
Tag = string.Format("Btn{0}", i),
Text = string.Format("Button{0}",i),
Location = new Point(xPosition, 0)
};
xPosition = xPosition + 100;
Controls.Add(button);
}
When you are creating Control(in your case Buttons) you can give them Name property. It will be very good if that name will be unique.
var btn = new Button();
btn.Name = "MyBtn";
btn.Text = "Our Button";
this.Controls.Add(btn);
For creation of N buttons you just need to put this in a Loop with N iterations and set btn.Name to something like "Name"+SomeNumber.
To set the Position of the Buttons to below the input you should set btn.Left and btn.Top to the corresponding coordinates.
Then when you need to work with generated Control/Button you can do search by that Name in the following way:
var btn = (Button)this.Controls.Find("MyBtn", true).First();
and do whatever you want with that Control/Button.
But in this case there is some danger as I am not checking if there was found any control with that name. If you write incorrect Name this will throw exception on .First().
I have a bunch of dynamically added controls which add row by row when the user clicks the add user button. I want there to be a label when the page loads and i want the same label to move down every time the add user button is clicked (under each row of textboxes). Right now it is there on load and it moves down when the user clicks the button the first time but after that it just stays. Here is my code:
Global variables:
Label Savelbl = new Label();
int LabelX = 15;
int LabelY = 110;
int spacelbl = 15;
Page load:
Savelbl.Location = new Point(LabelX, LabelY);
Savelbl.Name = "Savelbl";
Savelbl.Text = "Please click 'save' to save your changes";
CaeUsersPanel.Controls.Add(Savelbl);
Add user button:
private void CAEAddUserbtn_Click(object sender, EventArgs e)
{
var i = UsernameTextBoxes.Count + 1; // this is a list of the added textboxes
ADDUserInfo(i); //method which adds the dynamically created textboxes
Savelbl.Location = new Point(LabelX, LabelY + spacelbl);
}
Remove user button (the label should move back up when this is clicked):
private void Remove_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure you want delete this user? \n Deleting users may break workflows", "Delete", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
int idx = RemoveButtons.IndexOf((Button)sender);
// Remove button
RemoveButtons[idx].Dispose();
RemoveButtons.RemoveAt(idx);
// Remove textbox
UsernameTextBoxes[idx + 1].Dispose();
UsernameTextBoxes.RemoveAt(idx + 1);
//Shift controls up
for (int i = idx; i < RemoveButtons.Count; i++)
{
UsernameTextBoxes[i + 1].Top -= SpaceDelta;
}
space -= SpaceDelta;
Savelbl.Location = new Point(LabelX, LabelY - spacelbl);
}
}
You never update LabelX and LabelY.
LabelX = Savelbl.Location.X
LabelY = Savelbl.Location.Y
Savelbl.Location = new Point(LabelX, LabelY - spacelbl);
You could also get rid of these variables probably...
I'm looking for some advice on how to add click event handlers to labels that have been created created dynamically within a loop.
I've searched for click event handlers on dynamically created controls but this always comes back with single controls that aren't within an array.
example of code:
//create an array of 16 labels
Label[] label = new Label[16];
//loop through the array of labels
for (int i = 0; i < label.Length; i++)
{
label[i] = new Label(); //create new label
label[i].Name = "lbl" + i.ToString(); //give the label a name
label[i].Text = "label " + i.ToString(); //give the label text
}
Any help and advice on this would be great, thanks!
Add a handler:
label[i].Click += HandleLabelClick;
void HandleLabelClick(object sender, EventArgs e)
{
// ...
}
Note that you can determine which label was clicked by using the sender argument:
void HandleLabelClick(object sender, EventArgs e)
{
var label = (Label) sender;
if (label.Text == "this or that") { /* ... */ }
}