I'm trying to recall the buttons I created and position them on arrays that hold its location but FindControl gives Error.
'Form1' does not contain a definition for 'FindControl' and no extension method
'FindControl' accepting a first argument of type 'Form1' could be found (are you missing
a using directive or an assembly reference?)
My code is under a timer1.
private void timer1_Tick(object sender, EventArgs e)
{
...
if ...
{
...
Button btn = new Button();
{
btn.Name = "Btn-" + tail.ToString();
btn.Height = 10;
btn.Width = 10;
btn.Tag = tail+1;
btn.Location = new Point((stailX[1]-1)*10, (stailY[1] - 1) * 10);
}
this.Controls.Add(btn);
}
...
for (int i = 1; i <= tail; i++)
{
((Button)this.FindControl("Btn-" + tail.ToString())).Location = new Point((stailX[i] - 1) * 10, (stailY[i] - 1) * 10);
}
}
I've omitted parts of code not important. Please help.
My approach would include putting the controls in an array or a list for easy access later. Here is an example:
public partial class Form1 : Form
{
List<Button> buttonList = new List<Button>();
int i = 0;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
i++;
Button btn = new Button();
btn.Location = new Point(20 , i* 30);
buttonList.Add(btn); // add instance to list
this.Controls.Add(btn); // add the same instance to the form
for (int j = 0; j < buttonList.Count; ++j)
{
// do whatever you want here
buttonList[j].Text = j.ToString();
}
}
}
This way you don't need to find the control you want on every iteration.
I think you can achieve the same, by iterating through each controls in the form using this.Controls, and can be more specifically by filtering them using .OfType<Button>(). To make sure that those controls are dynamically created with specific Ids you can add the conditions as well. The code for this will looks like the following:
int i = 1;
foreach (Button childButton in this.Controls.OfType<Button>())
{
if (childButton.Name.StartsWith("Btn-"))
{
childButton.Location = new Point((stailX[i] - 1) * 10, (stailY[i] - 1) * 10);
}
i++;
}
Hope that it will help you
How about Controls.Find("controlname", true)
Related
I would like to add MaterialSingleLineTextField dynamically to a Form.
I have used MaterialSkin NuGet package:
I am trying to create multiple MaterialSkin TextBoxes dynamically on Form.Load. But no Controls are displaying in the hosing Panel.
private void Form1_Load(object sender, EventArgs e)
{
int n = 5;
int pointX = 30;
int pointY = 40;
//panel1.Controls.Clear();
for (int i = 0; i < n - 1; i++)
{
MaterialSingleLineTextField a = new MaterialSingleLineTextField();
a.Text = (i + 1).ToString();
a.Visible = true;
a.Location = new Point(pointX, pointY);
panel1.Controls.Add(a);
panel1.Show();
pointY += 20;
}
}
This code block works perfectly fine for normal TextBoxes.
Is there any way to add MaterialSingleLineTextField dynamically?
A sample Form initialization with default Theme values.
The MaterialSkinManager is initialized in the Form Constructor, setting the Theme to MaterialSkinManager.Themes.LIGHT and default color scheme. The Form base Type is set to MaterialForm.
A specified number of MaterialSingleLineTextField controls is added to a parent container (a Panel), starting from a defined Location downwards.
These Controls are anchored to the parent and the Height is set to Parent.Font.Height + 4.
It's important that you specify the Size of these Controls, otherwise you'll get a minimal size that prevents the Controls from showing their content.
As you can see in the AddTextFields() method, it's important that you dispose of the previous Controls added to the Parent container, if you want to replace the existing with new ones. Even more important using this Library.
Calling the Clear() method of a Control.Controls collection (as in the line you have commented out) doesn't dispose of anything, those controls are still alive.
public partial class Form1 : MaterialForm
{
private readonly MaterialSkinManager msManager = null;
public Form1()
{
InitializeComponent();
msManager = MaterialSkinManager.Instance;
msManager.AddFormToManage(this);
msManager.Theme = MaterialSkinManager.Themes.LIGHT;
}
private void Form1_Load(object sender, EventArgs e)
{
AddTextFields(panel1, 5, new Point(30, 10), false);
}
private void AddTextFields(Control parent, int controlsCount, Point startPosition, bool ClearExisting)
{
if (clearExisting && parent.Controls.Count > 0) {
for (int i = parent.Controls.Count - 1; i >= 0; i--) {
parent.Controls[i].Dispose();
}
}
int controlHeight = parent.Font.Height + 4;
int yIncrement = 0;
for (int i = 0; i < controlsCount; i++) {
var textField = new MaterialSingleLineTextField() {
Text = (i + 1).ToString(),
Size = new Size(parent.ClientSize.Width - startPosition.X - 4, controlHeight),
Location = new Point(startPosition.X, startPosition.Y + yIncrement),
Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right
};
parent.Controls.Add(textField);
yIncrement += (controlHeight + 10);
}
}
private void matBtnChangeTheme_Click(object sender, EventArgs e)
{
msManager.Theme = MaterialSkinManager.Themes.DARK;
msManager.ColorScheme = new ColorScheme(Primary.Blue600, Primary.Blue900, Primary.Blue500, Accent.LightBlue200, TextShade.WHITE);
}
private void matBtnAddControls_Click(object sender, EventArgs e)
{
AddTextFields(panel1, 7, new Point(30, 10), true);
}
}
Sample functionality:
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;
}
}
}
}
}
Here is my code so far where I have a numericupdown item named numericUpDown and button. Once the user selects a number when they press the button it dynamically created the fields.
private void createPerson_Click(object sender, EventArgs e)
{
Label[] person_Name = new Label[(int)this.numericUpDown.Value];
TextBox[] person_txtinput = new TextBox[(int)this.numericUpDown.Value];
for (int i = 0; i < this.numericUpDown.Value; i++)
{
//create person name label
Person_Name[i] = new Label();
Person_Name[i].Location = new System.Drawing.Point(20, 114 + i * 25);
Person_Name[i].Size = new System.Drawing.Size(120, 15);
Person_Name[i].Text = (i + 1).ToString() + #")" + "Person Name:";
this.Controls.Add(Person_Name[i]);
//create person name textbox
PersonNameTxtInput[i] = new TextBox();
PersonNameTxtInput[i].Location = new System.Drawing.Point(140, 114 + i * 25);
PersonNameTxtInput[i].Size = new System.Drawing.Size(125, 20);
this.Controls.Add(PersonNameTxtInput[i]);
}
}
private void save_Click(object sender, EventArgs e)
{
for (int i = 0; j < this.numericUpDown.Value; i++)
{
MessageBox.Show("" + PersonNameTxtInput[i].Text);
}
}
My question is, how can I get all the values from the textboxes depending on how many fields are created by the user when the save button is pressed?
I have tried using the code within the save button listener however how can i make Label[] person_Name = new Label[(int)this.numericUpDown.Value]; a global variable so i can access it within the save button for loop.
Well, I don't know exactly why you are doing this in this particular way and I must admit it doesn't seem very effective, but you could just do what Ryan_L suggested and iterate through this.Controls like this
for(int i = 0; i < this.Controls.Count; i++)
{
if(this.Controls[i] is TextBox) //skip buttons and labels
{
MessageBox.Show("" + this.Controls[i].Text);
}
}
Now, regarding your question how to define a global variable so you can access it within the save button for loop...just define the two arrays outside of the createPerson_Click event like this:
Label[] person_Name;
TextBox[] person_txtinput;
private void button1_Click(object sender, EventArgs e)
{
person_Name = new Label[(int)this.numericUpDown.Value];
person_txtinput = new TextBox[(int)this.numericUpDown.Value];
//the rest of the code
}
Hope this helps. However, you might want to reconsider your entire approach.
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 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.