Hello All,
I have been teaching myself C# on the side and as a little project am trying to make a simple WPF / C# desktop app. The main idea is the user can enter text in a textbox, select a dropdown, click a radio button and click copy and the data copy to clipboard in a simple format for pasting into another place (text doc etc...).
Here is an example of output I am looking to achieve:
Label 1: USER TEXT Here
Label 2: USER TEXT Here
" Some header label here "
Label 3: USER TEXT Here
and so on.
What I have done is looped though the controls and create a list for the textblocks (labels), another list for user text (textboxes) and a final list for the labels ( Headers ). As I have three list zip becomes difficult so I just for loop the textblocks or textboxes and build a stringbuilder to copy to clipboard. This works as intended and copies correctly. The issue is when I try to insert the (header) that separates a potential subject area from another. If no user text is entered it copies as intended but if some text in the textbox before the sb.insert, well of course it throws it off. I have tried to research a more solid way of inserting where I need the headers but have been able to find a solution. I also tried
Just wanted to see if anyone thought there was a better way to go about this that can accommodate the output I am looking for?
Xaml:
XAML
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="842" Width="567">
<Grid>
<Button Content="Copy" HorizontalAlignment="Left" Height="32" Margin="10,10,0,0" VerticalAlignment="Top" Width="71" Click="Button_Click"/>
<Grid x:Name="controlGrid" Margin="0,53,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="24*"/>
<RowDefinition Height="25*"/>
<RowDefinition Height="25*"/>
<RowDefinition Height="27*"/>
<RowDefinition Height="26*"/>
<RowDefinition Height="37*"/>
<RowDefinition Height="35*"/>
<RowDefinition Height="37*"/>
<RowDefinition Height="38*"/>
<RowDefinition Height="38*"/>
<RowDefinition Height="461*"/>
</Grid.RowDefinitions>
<TextBlock HorizontalAlignment="Left" Height="22" Margin="23,0,0,0" Text="Label 1" TextWrapping="Wrap" VerticalAlignment="Center" Width="46" FontWeight="Bold" Padding="0,3,0,0" Tag="1"/>
<TextBox HorizontalAlignment="Left" Height="22" Margin="93,0,0,0" TextWrapping="Wrap" VerticalAlignment="Center" Width="448" Tag="1"/>
<TextBlock HorizontalAlignment="Left" Height="22" Margin="23,0,0,0" Text="Label 2" TextWrapping="Wrap" VerticalAlignment="Center" Width="46" FontWeight="Bold" Padding="0,3,0,0" Grid.Row="1" Tag="2"/>
<TextBox HorizontalAlignment="Left" Height="22" Margin="93,0,0,0" TextWrapping="Wrap" VerticalAlignment="Center" Width="448" Grid.Row="1" Tag="2"/>
<TextBlock HorizontalAlignment="Left" Height="22" Margin="23,5,0,0" Text="Label 3" TextWrapping="Wrap" VerticalAlignment="Top" Width="46" FontWeight="Bold" Padding="0,3,0,0" Grid.Row="2" Tag="3" Grid.RowSpan="2"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="93,0,0,0" TextWrapping="Wrap" VerticalAlignment="Center" Width="448" Grid.Row="2" Tag="3"/>
<TextBlock HorizontalAlignment="Left" Height="22" Margin="23,4,0,0" Text="Label 4" TextWrapping="Wrap" VerticalAlignment="Top" Width="46" FontWeight="Bold" Padding="0,3,0,0" Grid.Row="3" Tag="4"/>
<TextBox x:Name="BoxOne" HorizontalAlignment="Left" Height="22" Margin="93,0,0,0" TextWrapping="Wrap" VerticalAlignment="Center" Width="448" Grid.Row="3" Tag="14"/>
<Label Content="*** Header 1 *** " HorizontalAlignment="Center" Height="16" Grid.Row="4" VerticalAlignment="Center" Width="93" Padding="0,0,0,0" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Left" Height="22" Margin="23,0,0,0" Text="Label 5" TextWrapping="Wrap" VerticalAlignment="Center" Width="46" FontWeight="Bold" Padding="0,3,0,0" Grid.Row="5" Tag="4"/>
<TextBox HorizontalAlignment="Left" Height="22" Margin="93,0,0,0" TextWrapping="Wrap" VerticalAlignment="Center" Width="448" Grid.Row="5" Tag="4"/>
<TextBlock HorizontalAlignment="Left" Height="22" Margin="23,0,0,0" Text="Label 6" TextWrapping="Wrap" VerticalAlignment="Center" Width="46" FontWeight="Bold" Padding="0,3,0,0" Grid.Row="6" Tag="4"/>
<TextBox x:Name="BoxTwo" HorizontalAlignment="Left" Height="22" Margin="93,0,0,0" TextWrapping="Wrap" VerticalAlignment="Center" Width="448" Grid.Row="6" Tag="4"/>
<Label Content="*** Header 2 *** " HorizontalAlignment="Center" Height="16" Grid.Row="7" VerticalAlignment="Center" Width="93" Padding="0,0,0,0" FontWeight="Bold"/>
</Grid>
</Grid>
</Window>
C#
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// creating the list
List<string> TBText = new List<string>();
List<string> UserText = new List<string>();
List<string> LabelText = new List<string>();
// grabbing controls from grid
var children = controlGrid.Children.OfType<Control>();
// adding textblocks to list
foreach (TextBlock tbText in controlGrid.Children.OfType<TextBlock>())
{
TBText.Add(tbText.Text);
}
// adding user text to list
foreach (var child in children)
{
if (child.GetType() == typeof(TextBox))
{
UserText.Add(((TextBox)child).Text);
}
}
// adding labels
foreach (Label labelText in controlGrid.Children.OfType<Label>())
{
LabelText.Add(labelText.Content.ToString());
}
StringBuilder sb = new StringBuilder();
for (var x = 0; x < UserText.Count; x++)
{
sb.AppendLine(TBText[x] + " " + UserText[x]);
}
//position code works until user enters text in textbox
//int pos = sb.ToString().IndexOf("Label 4") + 7 + Environment.NewLine.Length;
//sb.Insert(pos, LabelText[0]);
Clipboard.SetText(sb.ToString());
}
}
