user1219310
user1219310

Reputation: 732

wpf text out of alignment in textbox

How to solve the text out of alignment in text box UI, but it was fine in copy to clipboard function to text file.

Thanks

enter image description here

XAML:

     <Grid>
         <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <StackPanel Orientation="Vertical" HorizontalAlignment="Left" Grid.Column="1" >
            <TextBox Name="txtServiceParameter" Width="550" Height="460" IsReadOnly="True" Margin="10, 10, 0, 0" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" Text="{Binding DefaultText}" FontSize="12" FontFamily="Microsoft Sans Serif" HorizontalContentAlignment="Left"/>
            <Button Content="Copy to Clipboard" Width="150" Height="30" Margin="10, 10, 0, 0" Command="{Binding CopyToClipboardCommand}" />
        </StackPanel>
    </Grid>

C#:

        defaultText += "Hello World - Class 1 Day 1 " + DateTime.Now.ToString("dd/MM/yyyy H:mm:ss");
        defaultText += Environment.NewLine;
        defaultText += Environment.NewLine;
        defaultText += Environment.NewLine;
        defaultText += "************************************************************************";
        defaultText += Environment.NewLine;
        defaultText += "Mathemathic Class";
        defaultText += Environment.NewLine;
        defaultText += "************************************************************************";
        defaultText += Environment.NewLine;
        defaultText += Environment.NewLine;
        defaultText += string.Format("{0, -3}, {1,-34}, {2}", "01", "Hello", "EEE910");
        defaultText += Environment.NewLine;
        defaultText += string.Format("{0, -3}, {1,-34}, {2}", "02", "Belo haha", "");          

        private void ExecuteCopyToClipboardCommand()
        {
          Clipboard.SetText(defaultText);
        }

Upvotes: 1

Views: 296

Answers (2)

gayan1991
gayan1991

Reputation: 795

Normally With Spaces I mostly go for somekind of multiplication of white space characters Like If 10 white spaces are needed I multiply 1- with 2. It's not the best answer but it works

Upvotes: 0

Trevor Tubbs
Trevor Tubbs

Reputation: 2117

Change the font family on the TextBox to one that is Monospaced. The problem is the font you are using. It does not have a fixed with for each character.

Upvotes: 1

Related Questions