question

Michael-3905 avatar image
0 Votes"
Michael-3905 asked DaisyTian-1203 answered

WPF / C# Copy and Paste w/ StringBuilder (sb insert etc...)

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());
     }
 }





windows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered

I made some update to your xaml to implement the output you are looking to achieve:

  <Grid>
         <Grid.Resources>
             <Style TargetType="TextBlock">
                 <Setter Property="HorizontalAlignment" Value="Center"></Setter>
                 <Setter Property="VerticalAlignment" Value="Center"></Setter>
                 <Setter Property="TextWrapping" Value="Wrap"></Setter>
                 <Setter Property="Width" Value="46"></Setter>
                 <Setter Property="Height" Value="22"></Setter>
                 <Setter Property="Padding" Value="0,3,0,0"></Setter>
                 <Setter Property="FontWeight" Value="Bold"></Setter>
             </Style>
    
             <Style TargetType="TextBox">
                 <Setter Property="HorizontalAlignment" Value="Left"></Setter>
                 <Setter Property="VerticalAlignment" Value="Center"></Setter>
                 <Setter Property="TextWrapping" Value="Wrap"></Setter>
                 <Setter Property="Width" Value="448"></Setter>
                 <Setter Property="Height" Value="22"></Setter>
             </Style>
         </Grid.Resources>
         <Button Content="Copy"  Height="32" Margin="10,10,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" 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="461"/>
             </Grid.RowDefinitions>
             <Grid.ColumnDefinitions>
                 <ColumnDefinition Width="1*"></ColumnDefinition>
                 <ColumnDefinition Width="8*"></ColumnDefinition>
             </Grid.ColumnDefinitions>
    
             <TextBlock Grid.Row="0" Grid.Column="0" Text="Label 1" Tag="1"/>
             <TextBox Grid.Row="0" Grid.Column="1"  Tag="1"/>
             <TextBlock Grid.Row="1" Grid.Column="0" Text="Label 2" Tag="2"/>
             <TextBox Grid.Row="1" Grid.Column="1" Tag="2"/>
             <Label Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Content="*** Header 1 *** " HorizontalAlignment="Center" Height="16"  Width="93" Padding="0,0,0,0" FontWeight="Bold"  />
    
             <TextBlock  Grid.Row="3" Grid.Column="0" Text="Label 3" Tag="3" />
             <TextBox Grid.Row="3" Grid.Column="1" Tag="3"/>
             <TextBlock Grid.Row="4" Grid.Column="0"  Text="Label 4" Tag="4"/>
             <TextBox  Grid.Row="4" Grid.Column="1"  x:Name="BoxOne"/>
    
             <Label Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" Content="*** Header 2 *** " HorizontalAlignment="Center" Height="16" Width="93" Padding="0,0,0,0" FontWeight="Bold"/>
    
             <TextBlock  Grid.Row="6" Grid.Column="0" Text="Label 5" Tag="4"/>
             <TextBox  Grid.Row="6" Grid.Column="1"  Tag="4"/>
      
             <TextBlock  Grid.Row="7" Grid.Column="0"  Text="Label 6" Tag="4"/>
             <TextBox Grid.Row="7" Grid.Column="1"  x:Name="BoxTwo"  Tag="4"/>
         </Grid>
     </Grid>

And when I replace Clipboard.SetText(sb.ToString()); with Clipboard.SetText(sb.ToString().Replace("\r\n", ""));, I will copy and paste it like below picture shown:
91975-capture.png

I can't find your dropdown and radio button, could you tell me more details of them? Each Label corresponds to a RadioButton? Then according to the selected RadioButton determine the Label that needs to be copied?


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


capture.png (9.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.