Answered by:
Cant create dynamically Textboxes in another form C#

Question
-
Cant create dynamically Checkboxes in another form..
Have tried everything but i cant get it working..
Here is my code:
files = Directory.GetFiles(KundMapp, "*.sql", SearchOption.AllDirectories); var wordResults = new List<string>(); foreach (var f in files) { var wordList = ( from line in File.ReadAllLines(f) from Match match in Regex.Matches(line.ToLower(), @"(?<!\w)momentum_\w+") select match.Value ).ToList(); wordResults.AddRange(wordList); } var distinctWords = wordResults.Distinct().ToArray(); string result = string.Join("\n", distinctWords); foreach (string s in distinctWords) { HittaNamn.Add(s); } //MessageBox.Show(result.ToString()); for(int i = 0; i < HittaNamn.Count(); i++) { textBoxes = new TextBox(); textBoxes.Tag = i; textBoxes.Text = HittaNamn[i].ToString(); textBoxes.Location = new Point(form1.LaddaHittaErsätt.Location.X, form1.LaddaHittaErsätt.Location.Y + form1.LaddaHittaErsätt.Height + 25 + (i * 25)); form1.Controls.Add(textBoxes); }
Thankful for help..
- Edited by Carlo Goretti Friday, November 15, 2019 11:52 AM
- Moved by KareninstructorMVP, Moderator Friday, November 15, 2019 2:39 PM moved from C# forum
Answers
-
Okay, the following code sample is a mock up (don't have time to integrate it into your code).
Form1 has a button
Form2 has a FlowControl with AutoScroll = true, the width is set to what might be considered the max width of text and the background is white so you can see it.
Class to create TextBox controls from a list you created of words.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CreateControlOnChildForm { public class Creator { private Form _form; private Control _parentControl; private List<string> _wordList; public Creator(Form form, Control parentControl, List<string> wordList) { _form = form; _parentControl = parentControl; _wordList = wordList; } /// <summary> /// Create TextBox controls from /// </summary> public void Make() { for (int index = 0; index < _wordList.Count -1; index++) { var tb = new TextBox() { Parent = _parentControl, Text = _wordList[index], Tag = index }; _parentControl.Controls.Add(tb); } } } }
Form2 has a overload for the new constructor which accepts your list. In the constructor the controls are created in the FlowLayoutPanel.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CreateControlOnChildForm { public partial class Form2 : Form { public Form2() { InitializeComponent(); } public Form2(List<string> wordList) { InitializeComponent(); var ops = new Creator( this, flowLayoutPanel1, wordList); ops.Make(); } } }
In form1 I use a mockup list, in this case day names.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CreateControlOnChildForm { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { var f = new Form2(GetDayNames().ToList()); f.ShowDialog(); } public string[] GetDayNames() { return CultureInfo.CurrentCulture.Name.StartsWith("en-") ? new[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" } : CultureInfo.CurrentCulture.DateTimeFormat.DayNames; } } }
Result
Another mock up to show scrolling
var f = new Form2( Enumerable.Range(1, 30).Select(item => $"Item{item}").ToList()); f.ShowDialog();
I did this fast so make the panel a bit wider to lose the horizontal scrollbar.
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
- Edited by KareninstructorMVP, Moderator Friday, November 15, 2019 1:27 PM
- Marked as answer by Carlo Goretti Friday, November 15, 2019 9:46 PM
All replies
-
The code is one Form and form1 it the another Form where you try to add TextBoxes right? What is the outcome of the code error or text boxes are not visible?
For reference following code would add three text boxes to form2 in event handler of form1 and text boxes should be visible:
private void button1_Click(object sender, EventArgs e) { string[] texts = new string[] { "text1", "text2", "text3" }; Form2 form2 = new Form2(); int y = 10; foreach (string text in texts) { TextBox textBox = new TextBox(); textBox.Text = text; textBox.Location = new Point(10, y); textBox.Width = 120; y += 40; form2.Controls.Add(textBox); } form2.Show(); }
-
-
Hello,
Creating the CheckBox controls is easy but what is missing is "have you considered once they are created how will you interact with them?" That is the missing part and once known I can provide insight to doing this.
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
-
-
Okey but i dont understand what im missing.. All im trying to do is to get this textboxes to show in the form..
You said
Cant create dynamically Checkboxes in another form..
Have tried everything but i cant get it working..And in the title said
Cant create dynamically Textboxes in another form C#
So if a CheckBox(s) when someone checks the check box would action should be taken or will you query which CheckBoxes are checked later and what is the logic behind this.
If TextBox(es) do you want to perform actions e.g. when the user enters text or perhaps wait and see what's in the TextBox controls.
Bottom line is there is no reason to place controls on a form and not have a plan to use them.
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
-
-
-
-
Okay, the following code sample is a mock up (don't have time to integrate it into your code).
Form1 has a button
Form2 has a FlowControl with AutoScroll = true, the width is set to what might be considered the max width of text and the background is white so you can see it.
Class to create TextBox controls from a list you created of words.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CreateControlOnChildForm { public class Creator { private Form _form; private Control _parentControl; private List<string> _wordList; public Creator(Form form, Control parentControl, List<string> wordList) { _form = form; _parentControl = parentControl; _wordList = wordList; } /// <summary> /// Create TextBox controls from /// </summary> public void Make() { for (int index = 0; index < _wordList.Count -1; index++) { var tb = new TextBox() { Parent = _parentControl, Text = _wordList[index], Tag = index }; _parentControl.Controls.Add(tb); } } } }
Form2 has a overload for the new constructor which accepts your list. In the constructor the controls are created in the FlowLayoutPanel.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CreateControlOnChildForm { public partial class Form2 : Form { public Form2() { InitializeComponent(); } public Form2(List<string> wordList) { InitializeComponent(); var ops = new Creator( this, flowLayoutPanel1, wordList); ops.Make(); } } }
In form1 I use a mockup list, in this case day names.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CreateControlOnChildForm { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { var f = new Form2(GetDayNames().ToList()); f.ShowDialog(); } public string[] GetDayNames() { return CultureInfo.CurrentCulture.Name.StartsWith("en-") ? new[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" } : CultureInfo.CurrentCulture.DateTimeFormat.DayNames; } } }
Result
Another mock up to show scrolling
var f = new Form2( Enumerable.Range(1, 30).Select(item => $"Item{item}").ToList()); f.ShowDialog();
I did this fast so make the panel a bit wider to lose the horizontal scrollbar.
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
- Edited by KareninstructorMVP, Moderator Friday, November 15, 2019 1:27 PM
- Marked as answer by Carlo Goretti Friday, November 15, 2019 9:46 PM
-
-
Im trying to use one list in a method that is added in a different method.
Any ideas why i cant get any values when im using it?
for (int i = 0; i < HittaNamn.Count(); i++) { textBoxes = new TextBox(); textBoxes2 = new TextBox(); textBoxes.Tag = "Momentum_Vänster" + i; textBoxes.Text = HittaNamn[i].ToString(); textBoxes.Location = new Point(form1.HittaLbl.Location.X + 3, form1.HittaLbl.Location.Y + form1.HittaLbl.Height + 5 + (i * 25)); textBoxes.Width = 200; textBoxes.ReadOnly = true; textBoxes.BackColor = SystemColors.Window; ÄndraFrånLista.Add(textBoxes); form1.Controls.Add(textBoxes); textBoxes2.Tag = "Momentum_Höger" + i; textBoxes2.Text = ""; textBoxes2.Location = new Point(form1.ErsättLbl.Location.X + 3, form1.ErsättLbl.Location.Y + form1.ErsättLbl.Height + 5 + (i * 25)); textBoxes2.Width = 200; ÄndraTillLista.Add(textBoxes2); form1.Controls.Add(textBoxes2); } if (form1.CreateBtn.Location.Y + form1.CreateBtn.Height < textBoxes.Location.Y + textBoxes.Height || form1.CreateBtn.Location.Y + form1.CreateBtn.Height < textBoxes2.Location.Y + textBoxes2.Height) { form1.CreateBtn.Location = new Point(form1.CreateBtn.Location.X, textBoxes.Location.Y + textBoxes.Height + 15); } if (form1.Location.Y + form1.Height < form1.CreateBtn.Location.Y + form1.CreateBtn.Height) { form1.Height = form1.CreateBtn.Location.Y + form1.CreateBtn.Height + 50; } form1.Show(); } else { MessageBox.Show("Fanns inte några ord som innehöll 'Momentum_' i någon av filerna."); return; } } public void ÄndraText() { for (int i = 0; i < ÄndraFrånLista.Count; i++) { foreach (string f in files) { string content = File.ReadAllText(f); if (content.Contains(ÄndraFrånLista[i].Text)) { content = content.Replace(ÄndraFrånLista[i].Text, ÄndraTillLista[i].Text); File.WriteAllText(f, content); } } } }
Best Regards- Merged by KareninstructorMVP, Moderator Friday, November 15, 2019 2:38 PM Same topic
-
I just have one Form and one class! i create them in the class..
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CreateControlOnChildForm { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { var ops = new Creator( this, flowLayoutPanel1, Enumerable.Range(1, 30).Select(item => $"Item{item}").ToList()); ops.Make(); } } }
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
-
-
It works when i use form1.show() but i dosent work if i dont use it..
That does not make sense, if you have one form then the form is fired up from Program.cs, when there is one form there is zero reasons to use the form's Show method.
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace CreateControlOnChildForm { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
Given the above this can be done in form load or form shown event.
namespace CreateControlOnChildForm { public partial class Form1 : Form { public Form1() { InitializeComponent(); Shown += Form1_Shown; } private void Form1_Shown(object sender, EventArgs e) { var ops = new Creator( this, flowLayoutPanel1, Enumerable.Range(1, 30).Select(item => $"Item{item}").ToList()); ops.Make(); } } }
If none of the above holds true for you than rethink your logic. And remember that the hard coded list would be replaced with a dynamic list from your operation to get words in a list.
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
-
Since we frown upon duplicate questions I'm merging this with the other question.
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
-
Okey thanks but i dont know how i can make it in my app.. it dosent refresh.. It only appears when im using form1.Show();
My other question is why i get null in this list when im using it in the other method..
for (int i = 0; i < HittaNamn.Count(); i++) { textBoxes = new TextBox(); textBoxes2 = new TextBox(); textBoxes.Tag = "Momentum_Vänster" + i; textBoxes.Text = HittaNamn[i].ToString(); textBoxes.Location = new Point(form1.HittaLbl.Location.X + 3, form1.HittaLbl.Location.Y + form1.HittaLbl.Height + 5 + (i * 25)); textBoxes.Width = 200; textBoxes.ReadOnly = true; textBoxes.BackColor = SystemColors.Window; ÄndraFrånLista.Add(textBoxes); form1.Controls.Add(textBoxes); textBoxes2.Tag = "Momentum_Höger" + i; textBoxes2.Text = ""; textBoxes2.Location = new Point(form1.ErsättLbl.Location.X + 3, form1.ErsättLbl.Location.Y + form1.ErsättLbl.Height + 5 + (i * 25)); textBoxes2.Width = 200; ÄndraTillLista.Add(textBoxes2); form1.Controls.Add(textBoxes2); } if (form1.CreateBtn.Location.Y + form1.CreateBtn.Height < textBoxes.Location.Y + textBoxes.Height || form1.CreateBtn.Location.Y + form1.CreateBtn.Height < textBoxes2.Location.Y + textBoxes2.Height) { form1.CreateBtn.Location = new Point(form1.CreateBtn.Location.X, textBoxes.Location.Y + textBoxes.Height + 15); } if (form1.Location.Y + form1.Height < form1.CreateBtn.Location.Y + form1.CreateBtn.Height) { form1.Height = form1.CreateBtn.Location.Y + form1.CreateBtn.Height + 50; } form1.Show(); } else { MessageBox.Show("Fanns inte några ord som innehöll 'Momentum_' i någon av filerna."); return; } } public void ÄndraText() { for (int i = 0; i < ÄndraFrånLista.Count; i++) { MessageBox.Show(ÄndraFrånLista[i].Text); //foreach (string f in files) //{ // string content = File.ReadAllText(f); // if (content.Contains(ÄndraFrånLista[i].Text)) // { // content = content.Replace(ÄndraFrånLista[i].Text, ÄndraTillLista[i].Text); // File.WriteAllText(f, content); // } //} } }