Reputation: 1
I have 10 textboxes(tb1,tb2,tb3,...) and when I press the button , the 10 textboxes will be filled with values based on the startingNum input by user, e.g:10, by addition (+1,+2,+3,..) according to the sequence of the 10 textboxes.
My code is working but is it possible to use loops or any better way to improve my codes?
private void genBtn_Click(object sender, EventArgs e)
{
firstNum.Text = alphaBox.Text + (intFirst + 0).ToString();
SecondNum.Text = alphaBox.Text + (intFirst + 1).ToString();
thirdNum.Text = alphaBox.Text + (intFirst + 2).ToString();
fourthNum.Text = alphaBox.Text + (intFirst + 3).ToString();
fifthNum.Text = alphaBox.Text + (intFirst + 4).ToString();
sixNum.Text = alphaBox.Text + (intFirst + 5).ToString();
seventhNum.Text = alphaBox.Text + (intFirst + 6).ToString();
eighthNum.Text = alphaBox.Text + (intFirst + 7).ToString();
ninthNum.Text = alphaBox.Text + (intFirst + 8).ToString();
tenthNum.Text = alphaBox.Text + (intFirst + 9).ToString();
}
Upvotes: 0
Views: 487
Reputation: 28968
Your example is best implemented using a ListBox
. The ListBox
should have a UniformGrid
with two columns defined as panel.
Because you display each TextBox
together with an associated label you need to create a data model, for example MyDataModel
, to hold both values.
Then create a list of MyDataModel
that you bind to the ListBox
. ListBox
will then create a TextBox
and an TextBlock
to display the text and label for each MyDataModel
item.
To define the layout and how the MyDataModel
is displayed by the ListBox
, you have to create a DataTemplate
.
Note that for displaying text only you would use the TextBlock
.
You may want to read the documentation to learn the required skills:
INotifyPropertyChanged
on the data source/data model (required for data binding)The advantage of following the following example is that it is fully dynamic. The number of elements is not hardcoded. This significantly reduces lines of code. For example, when generating 20 elements, hardcoding this would require to write 20 times the same code. This is not desirable. Aside from such code being not maintainable, you probably don't want to waste your time writing such repetitive code.
MyDataModel.cs
class MyDataModel : INotifyPropertyChanged
{
public string TextValue { get; }
public string Label { get; }
public MyDataModel(string textValue, string label)
{
this.TextValue = textValue;
this.Label = label;
}
}
MainWindow.xaml.cs
partial class MainWindow : Window
{
public ObservableCollection<MyDataModel> MyDataModels { get; }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
this.MyDataModels = new ObservableCollection<MyDataModel>();
}
private void genBtn_Click(object sender, EventArgs e)
{
this.MyDataModels.Clear();
// Dynamically create 20 TextBoxes aligned in two columns
for (int count = 0; count < 20; count++)
{
string id = $"{this.intFirst + count}";
string textValue = $"{this.alphaBox.Text}{id}";
string label = $"#{id}";
var newItem = new MyDataModel(textValue, label);
this.MyDataModels.Add(newItem);
}
}
}
MainWindow.xaml
<Window>
<StackPanel>
<Button Content="Generate"
Click="genBtn_Click" />
<ListBox ItemsSource="{Binding MyDataModels}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="2" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:MyDataModel}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Label}" />
<TextBox Text="{Binding TextValue}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Window>
Upvotes: 1
Reputation: 1
You can loop on textboxes doing :
int i=0;
foreach ( TextBox tb in this.Controls.OfType<TextBox>()) {
tb.Text = alphaBox.Text + (intFirst + i).ToString();
i++;
}
Upvotes: 0