I have been manually doing this process for quite some time and think that their must be a better way to do such. So here is the back-story. There is a SQL Table that holds 3 fields, QuestionToAsk, Responses, ID - of course pretty self explanatory here, the QuestionToAsk of course would be the label and a question, the Responses would be textboxes and would house any entered response, and the ID is just an ID field. Some sample data from the table would be like so ----
QuestionToAsk ---- Responses ---- ID
Top 5 Movies? Movie1 1
Movie2 2
Movie3 3
Movie4 4
Movie5 5
Fav from top 5? Top5Fav 6
Fav Hobby? FavHobby 7
Top 4 Cars? Car1 8
Car2 9
Car3 10
Car4 11
I have been just manually creating each label and copy/paste the text, and creating the textboxes to be associated with them because I can not find a way to associate the multiple response textboxes with the QuestionToAsk. Meaning, How can I query to see that Top 5 Movies needs 5 textboxes, and Fav from top5 only needs 1, so that I can dynamically create all of the information?
Selva GanapathyPosted Nov 17, 2014, 12:57 AM
Hi Richard Smith,
I can modified the demo sample as per dynamic textbox to its relation matrix, Using the Same logic in Editor by using the following code.
public MyEditor(int EditorCount)
{
InitializeComponent();
for (int i = 0; i < EditorCount; i++)
{
TextBox text = new TextBox();
text.Width = 100;
text.Height = 30;
text.Left = this.label.Left +( 105 * (i + 1));
this.Controls.Add(text);
}
}
for MyEditor I am using UserControl concept by modifying the Access modifier of the child controls.
Regards,
Selva Ganapathy K
richard smithPosted Nov 14, 2014, 8:54 AM
And all label's should be generated down the left side of the page
label1 textbox1
label2 textbox2
label3 textbox3
Or if there is a 1 to 2 relationship
label1 textbox1 textbox1a
label2 textbox2
label3 textbox3
EDIT -- the MyEditor code you have, what is that, how did you create that? That would be VERY useful for other projects I have.
Selva GanapathyPosted Nov 14, 2014, 12:36 AM
Hi Richard Smith,
You can make use of the following code to generate Textbox and Labels dynamically.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
GetEditors(Convert.ToInt16(this.textBox1.Text == string.Empty ? "0" : this.textBox1.Text));
}
void GetEditors(int count)
{
this.panel1.Controls.Clear();
var left = 0;
var top = 0;
for (int i = 0; i < count; i++)
{
MyEditor editor = new MyEditor();
editor.label.Text = "Label" + (i + 1).ToString();
editor.textBox.Text = editor.label.Text + "'s Text";
editor.Left = left;
editor.Top = top;
if (editor.Left + editor.Width > (this.panel1.Width - editor.Width))
{
top = top + editor.Height + 5;
left = 0;
}
else
{
left = left + editor.Width + 5;
}
this.panel1.Controls.Add(editor);
}
}
}
}
Regards,
Selva Ganapathy K