-
Nov 18th, 2012, 11:55 AM
#1
Thread Starter
New Member
[RESOLVED] WPF/C# Varying number of controls
Hi,
Could anyone please point me in the right direction to do following in WPF/C#.
Lets say I have an array of strings and the number of items in the array is varying.
What is the correct way to go and create a Label and a Text box to account to each item in the array dynamically?
When above is done, how can I refer to the controls(TextBoxes) created in code?
e.g.
Lets say I have below 3 strings in the array
- Name
- Email
- Telphone
How do I create labels and text boxes for above three items.
And after someone has entered data in the above created text boxes, how to read them in code?
Lets say they should go into a database table in below format
TextBoxName,Value
Highly appreciate any help on this.
-
Nov 18th, 2012, 04:55 PM
#2
Re: WPF/C# Varying number of controls
If you don't know how many items there will be then you obviously can't declare a variable forf each one. How can you refer to multiple objects using one variable? You alreay know one way: using an array. The alternative would be a collection of some sort. From what you've told us, a Dictionary would likely be a good option, where the Strings are the keys and the controls are the values.
You could loop through the String array and, for each element, create a TextBox, configure it, add it to the window and then add both the String and TextBox to the Dictionary. When it comes time to save, you loop through the Keys of the Dictionary get the value for each one, get the data from the TextBox, add the key and the data to a DataTable and, when you're done, save lot in a single batch.
-
Nov 19th, 2012, 09:23 AM
#3
Thread Starter
New Member
Re: WPF/C# Varying number of controls
Thanks a lot for your reply John,
I tried what you suggested and it has worked for me. Below is the code I have come up with
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private static Dictionary<string, List<string>> myQuestions = new Dictionary<string, List<string>>();
private static Dictionary<string, List<RadioButton>> myRadioButtonControls = new Dictionary<string, List<RadioButton>>();
public MainWindow()
{
InitializeComponent();
FillQuestions();
CreateControls(myStackPanel);
}
private static void FillQuestions()
{
myQuestions.Add("Favourite fruit", new List<string>() { "Apple", "Orange", "Grape" });
myQuestions.Add("Favourire vegitable", new List<string>() { "Tomato", "Potato"});
myQuestions.Add("Favoutite Animal", new List<string>() { "Lion", "Bear", "Elephant","Cow" });
}
private static void CreateControls(StackPanel s)
{
foreach (KeyValuePair<string,List<string>> item in myQuestions)
{
myRadioButtonControls.Add(item.Key, new List<RadioButton>());
foreach (string a in item.Value)
{
((List<RadioButton>)(myRadioButtonControls[item.Key])).Add(new RadioButton() { Content = a, GroupName = item.Key});
}
}
foreach (KeyValuePair<string,List<RadioButton>> item in myRadioButtonControls)
{
s.Children.Add(new TextBlock() { Text= item.Key });
foreach (RadioButton r in item.Value)
{
s.Children.Add(r);
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
StringBuilder s = new StringBuilder();
foreach (KeyValuePair<string, List<RadioButton>> item in myRadioButtonControls)
{
s.Append(string.Format("\nQ. {0}", item.Key));
foreach (RadioButton r in item.Value)
{
if ((bool)r.IsChecked)
{
s.Append(string.Format("\nA. {0}\n",r.Content));
}
}
}
MessageBox.Show(s.ToString());
}
}
}
Could you kindly let me know if I am doing things right? Also I would highly appreciate any suggestions to improve above code.
On a related note, how do I go about rapping each Quesion,Answer Set in a Group box?
Many Thanks again for your help..
-
Nov 21st, 2012, 11:56 AM
#4
Thread Starter
New Member
Re: WPF/C# Varying number of controls
Thanks a lot John,
I went further with above and created a user control to handle each question and it works very well.
I am still playing with it to see and add further functionality.
However, nothing of above would have been possible if not for your suggestion to use a Dictionary collection.
Thanks a lot!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|