C# WPF How to transfer text from richtexbox to another richtexbox using button?












0














I'm new in C# and I've got some issues with transferring text in WPF. I want that when I paste code into the first richtexbox, it will obfuscate by using button and shows in the second richtexbox but in first rtb it should be not changed.



Here is my code:



private void ButtonClicked2(object sender, RoutedEventArgs e)
{
string keyword = "class";
string newString = obfuscateString;
TextRange text = new TextRange(_richTextBox.Document.ContentStart, _richTextBox.Document.ContentEnd);
TextPointer current = text.Start.GetInsertionPosition(LogicalDirection.Forward);
while (current != null)
{
string textInRun = current.GetTextInRun(LogicalDirection.Forward);
if (!string.IsNullOrWhiteSpace(textInRun))
{
int index = textInRun.IndexOf(keyword);
if (index != -1)
{
TextPointer selectionStart = current.GetPositionAtOffset(index, LogicalDirection.Forward);
TextPointer selectionEnd = selectionStart.GetPositionAtOffset(keyword.Length, LogicalDirection.Forward);
TextRange selection = new TextRange(selectionStart, selectionEnd);
selection.Text = newString;
selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
_richTextBox.Selection.Select(selection.Start, selection.End);
_richTextBox.Focus();
}
}
current = current.GetNextContextPosition(LogicalDirection.Forward);
}
}


Thanks for help :)










share|improve this question
























  • It is helpful if you describe what the behavior is of the existing code. How is it not working?
    – Rufus L
    Nov 13 at 0:51










  • The reason you cant tell us which line isn't working the way you expect it, is because you haven't debugged it. please do so, and be more specific about the line that you think is having the problems, why you think that, and what you expect
    – TheGeneral
    Nov 13 at 1:15










  • It's working but its only show in first RTB. I don't know how to transfer code to another RTB without changing paste code in first RTB.
    – sirswistak
    Nov 13 at 10:05
















0














I'm new in C# and I've got some issues with transferring text in WPF. I want that when I paste code into the first richtexbox, it will obfuscate by using button and shows in the second richtexbox but in first rtb it should be not changed.



Here is my code:



private void ButtonClicked2(object sender, RoutedEventArgs e)
{
string keyword = "class";
string newString = obfuscateString;
TextRange text = new TextRange(_richTextBox.Document.ContentStart, _richTextBox.Document.ContentEnd);
TextPointer current = text.Start.GetInsertionPosition(LogicalDirection.Forward);
while (current != null)
{
string textInRun = current.GetTextInRun(LogicalDirection.Forward);
if (!string.IsNullOrWhiteSpace(textInRun))
{
int index = textInRun.IndexOf(keyword);
if (index != -1)
{
TextPointer selectionStart = current.GetPositionAtOffset(index, LogicalDirection.Forward);
TextPointer selectionEnd = selectionStart.GetPositionAtOffset(keyword.Length, LogicalDirection.Forward);
TextRange selection = new TextRange(selectionStart, selectionEnd);
selection.Text = newString;
selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
_richTextBox.Selection.Select(selection.Start, selection.End);
_richTextBox.Focus();
}
}
current = current.GetNextContextPosition(LogicalDirection.Forward);
}
}


Thanks for help :)










share|improve this question
























  • It is helpful if you describe what the behavior is of the existing code. How is it not working?
    – Rufus L
    Nov 13 at 0:51










  • The reason you cant tell us which line isn't working the way you expect it, is because you haven't debugged it. please do so, and be more specific about the line that you think is having the problems, why you think that, and what you expect
    – TheGeneral
    Nov 13 at 1:15










  • It's working but its only show in first RTB. I don't know how to transfer code to another RTB without changing paste code in first RTB.
    – sirswistak
    Nov 13 at 10:05














0












0








0







I'm new in C# and I've got some issues with transferring text in WPF. I want that when I paste code into the first richtexbox, it will obfuscate by using button and shows in the second richtexbox but in first rtb it should be not changed.



Here is my code:



private void ButtonClicked2(object sender, RoutedEventArgs e)
{
string keyword = "class";
string newString = obfuscateString;
TextRange text = new TextRange(_richTextBox.Document.ContentStart, _richTextBox.Document.ContentEnd);
TextPointer current = text.Start.GetInsertionPosition(LogicalDirection.Forward);
while (current != null)
{
string textInRun = current.GetTextInRun(LogicalDirection.Forward);
if (!string.IsNullOrWhiteSpace(textInRun))
{
int index = textInRun.IndexOf(keyword);
if (index != -1)
{
TextPointer selectionStart = current.GetPositionAtOffset(index, LogicalDirection.Forward);
TextPointer selectionEnd = selectionStart.GetPositionAtOffset(keyword.Length, LogicalDirection.Forward);
TextRange selection = new TextRange(selectionStart, selectionEnd);
selection.Text = newString;
selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
_richTextBox.Selection.Select(selection.Start, selection.End);
_richTextBox.Focus();
}
}
current = current.GetNextContextPosition(LogicalDirection.Forward);
}
}


Thanks for help :)










share|improve this question















I'm new in C# and I've got some issues with transferring text in WPF. I want that when I paste code into the first richtexbox, it will obfuscate by using button and shows in the second richtexbox but in first rtb it should be not changed.



Here is my code:



private void ButtonClicked2(object sender, RoutedEventArgs e)
{
string keyword = "class";
string newString = obfuscateString;
TextRange text = new TextRange(_richTextBox.Document.ContentStart, _richTextBox.Document.ContentEnd);
TextPointer current = text.Start.GetInsertionPosition(LogicalDirection.Forward);
while (current != null)
{
string textInRun = current.GetTextInRun(LogicalDirection.Forward);
if (!string.IsNullOrWhiteSpace(textInRun))
{
int index = textInRun.IndexOf(keyword);
if (index != -1)
{
TextPointer selectionStart = current.GetPositionAtOffset(index, LogicalDirection.Forward);
TextPointer selectionEnd = selectionStart.GetPositionAtOffset(keyword.Length, LogicalDirection.Forward);
TextRange selection = new TextRange(selectionStart, selectionEnd);
selection.Text = newString;
selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
_richTextBox.Selection.Select(selection.Start, selection.End);
_richTextBox.Focus();
}
}
current = current.GetNextContextPosition(LogicalDirection.Forward);
}
}


Thanks for help :)







c# wpf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 10:17









Lennart

5,970125065




5,970125065










asked Nov 13 at 0:42









sirswistak

35




35












  • It is helpful if you describe what the behavior is of the existing code. How is it not working?
    – Rufus L
    Nov 13 at 0:51










  • The reason you cant tell us which line isn't working the way you expect it, is because you haven't debugged it. please do so, and be more specific about the line that you think is having the problems, why you think that, and what you expect
    – TheGeneral
    Nov 13 at 1:15










  • It's working but its only show in first RTB. I don't know how to transfer code to another RTB without changing paste code in first RTB.
    – sirswistak
    Nov 13 at 10:05


















  • It is helpful if you describe what the behavior is of the existing code. How is it not working?
    – Rufus L
    Nov 13 at 0:51










  • The reason you cant tell us which line isn't working the way you expect it, is because you haven't debugged it. please do so, and be more specific about the line that you think is having the problems, why you think that, and what you expect
    – TheGeneral
    Nov 13 at 1:15










  • It's working but its only show in first RTB. I don't know how to transfer code to another RTB without changing paste code in first RTB.
    – sirswistak
    Nov 13 at 10:05
















It is helpful if you describe what the behavior is of the existing code. How is it not working?
– Rufus L
Nov 13 at 0:51




It is helpful if you describe what the behavior is of the existing code. How is it not working?
– Rufus L
Nov 13 at 0:51












The reason you cant tell us which line isn't working the way you expect it, is because you haven't debugged it. please do so, and be more specific about the line that you think is having the problems, why you think that, and what you expect
– TheGeneral
Nov 13 at 1:15




The reason you cant tell us which line isn't working the way you expect it, is because you haven't debugged it. please do so, and be more specific about the line that you think is having the problems, why you think that, and what you expect
– TheGeneral
Nov 13 at 1:15












It's working but its only show in first RTB. I don't know how to transfer code to another RTB without changing paste code in first RTB.
– sirswistak
Nov 13 at 10:05




It's working but its only show in first RTB. I don't know how to transfer code to another RTB without changing paste code in first RTB.
– sirswistak
Nov 13 at 10:05












1 Answer
1






active

oldest

votes


















0














In would not do this in CodeBehind, it is simple with Binding. So define a ViewModel where you define the OriginalText and ModifyedText as Property and a Command for the Transformation in case the Button is clicked.



So this can be like:



public class MyViewModel: INotifyPropertyChanged
{
private String _originalText = "My Original Text";
private String _modifyedText = "Place for Obfuscatet Text";

public String OriginalText
{
get => _originalText;
set
{
if (Equals(value, _originalText)) return;
_originalText = value;
OnPropertyChanged();
}
}

public String ModifyedText
{
get => _modifyedText;
set
{
if (Equals(value, _modifyedText)) return;
_modifyedText = value;
OnPropertyChanged();
}
}

public ICommand Obfuscate { get; set; }

public GuestViewModel()
{
Obfuscate = new RelayCommand(DoObfuscate, o => true);
}

private void DoObfuscate(object obj)
{
ModifyedText = DoModify(OriginalText);
}

//Implementation of INotifyPropertyChanged and DoModify etc...
}


Now bind the String properties to the two RichTextBoxes (there are multiple option for doing this see Richtextbox wpf binding) and the Button to the Command.



Xaml may be like:



<Grid DataContext="{Binding TODO}">
<Grid.RowDefinitions>
<RowDefinition Height="150"></RowDefinition>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="150"></RowDefinition>
</Grid.RowDefinitions>
<RichTextBox Grid.Row="0">
<FlowDocument PageHeight="180">
<Paragraph>
<Run Text="{Binding OriginalText, Mode=TwoWay}"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
<Button Grid.Row="1" Content="Process Text" Command="{Binding Obfuscate}"></Button>
<RichTextBox Grid.Row="2">
<FlowDocument PageHeight="180">
<Paragraph>
<Run Text="{Binding ModifyedText, Mode=TwoWay}"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
</Grid>


More Information about bindings can be found here: https://blogs.msdn.microsoft.com/jerrynixon/2012/10/12/xaml-binding-basics-101/






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272153%2fc-sharp-wpf-how-to-transfer-text-from-richtexbox-to-another-richtexbox-using-but%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    In would not do this in CodeBehind, it is simple with Binding. So define a ViewModel where you define the OriginalText and ModifyedText as Property and a Command for the Transformation in case the Button is clicked.



    So this can be like:



    public class MyViewModel: INotifyPropertyChanged
    {
    private String _originalText = "My Original Text";
    private String _modifyedText = "Place for Obfuscatet Text";

    public String OriginalText
    {
    get => _originalText;
    set
    {
    if (Equals(value, _originalText)) return;
    _originalText = value;
    OnPropertyChanged();
    }
    }

    public String ModifyedText
    {
    get => _modifyedText;
    set
    {
    if (Equals(value, _modifyedText)) return;
    _modifyedText = value;
    OnPropertyChanged();
    }
    }

    public ICommand Obfuscate { get; set; }

    public GuestViewModel()
    {
    Obfuscate = new RelayCommand(DoObfuscate, o => true);
    }

    private void DoObfuscate(object obj)
    {
    ModifyedText = DoModify(OriginalText);
    }

    //Implementation of INotifyPropertyChanged and DoModify etc...
    }


    Now bind the String properties to the two RichTextBoxes (there are multiple option for doing this see Richtextbox wpf binding) and the Button to the Command.



    Xaml may be like:



    <Grid DataContext="{Binding TODO}">
    <Grid.RowDefinitions>
    <RowDefinition Height="150"></RowDefinition>
    <RowDefinition Height="25"></RowDefinition>
    <RowDefinition Height="150"></RowDefinition>
    </Grid.RowDefinitions>
    <RichTextBox Grid.Row="0">
    <FlowDocument PageHeight="180">
    <Paragraph>
    <Run Text="{Binding OriginalText, Mode=TwoWay}"/>
    </Paragraph>
    </FlowDocument>
    </RichTextBox>
    <Button Grid.Row="1" Content="Process Text" Command="{Binding Obfuscate}"></Button>
    <RichTextBox Grid.Row="2">
    <FlowDocument PageHeight="180">
    <Paragraph>
    <Run Text="{Binding ModifyedText, Mode=TwoWay}"/>
    </Paragraph>
    </FlowDocument>
    </RichTextBox>
    </Grid>


    More Information about bindings can be found here: https://blogs.msdn.microsoft.com/jerrynixon/2012/10/12/xaml-binding-basics-101/






    share|improve this answer


























      0














      In would not do this in CodeBehind, it is simple with Binding. So define a ViewModel where you define the OriginalText and ModifyedText as Property and a Command for the Transformation in case the Button is clicked.



      So this can be like:



      public class MyViewModel: INotifyPropertyChanged
      {
      private String _originalText = "My Original Text";
      private String _modifyedText = "Place for Obfuscatet Text";

      public String OriginalText
      {
      get => _originalText;
      set
      {
      if (Equals(value, _originalText)) return;
      _originalText = value;
      OnPropertyChanged();
      }
      }

      public String ModifyedText
      {
      get => _modifyedText;
      set
      {
      if (Equals(value, _modifyedText)) return;
      _modifyedText = value;
      OnPropertyChanged();
      }
      }

      public ICommand Obfuscate { get; set; }

      public GuestViewModel()
      {
      Obfuscate = new RelayCommand(DoObfuscate, o => true);
      }

      private void DoObfuscate(object obj)
      {
      ModifyedText = DoModify(OriginalText);
      }

      //Implementation of INotifyPropertyChanged and DoModify etc...
      }


      Now bind the String properties to the two RichTextBoxes (there are multiple option for doing this see Richtextbox wpf binding) and the Button to the Command.



      Xaml may be like:



      <Grid DataContext="{Binding TODO}">
      <Grid.RowDefinitions>
      <RowDefinition Height="150"></RowDefinition>
      <RowDefinition Height="25"></RowDefinition>
      <RowDefinition Height="150"></RowDefinition>
      </Grid.RowDefinitions>
      <RichTextBox Grid.Row="0">
      <FlowDocument PageHeight="180">
      <Paragraph>
      <Run Text="{Binding OriginalText, Mode=TwoWay}"/>
      </Paragraph>
      </FlowDocument>
      </RichTextBox>
      <Button Grid.Row="1" Content="Process Text" Command="{Binding Obfuscate}"></Button>
      <RichTextBox Grid.Row="2">
      <FlowDocument PageHeight="180">
      <Paragraph>
      <Run Text="{Binding ModifyedText, Mode=TwoWay}"/>
      </Paragraph>
      </FlowDocument>
      </RichTextBox>
      </Grid>


      More Information about bindings can be found here: https://blogs.msdn.microsoft.com/jerrynixon/2012/10/12/xaml-binding-basics-101/






      share|improve this answer
























        0












        0








        0






        In would not do this in CodeBehind, it is simple with Binding. So define a ViewModel where you define the OriginalText and ModifyedText as Property and a Command for the Transformation in case the Button is clicked.



        So this can be like:



        public class MyViewModel: INotifyPropertyChanged
        {
        private String _originalText = "My Original Text";
        private String _modifyedText = "Place for Obfuscatet Text";

        public String OriginalText
        {
        get => _originalText;
        set
        {
        if (Equals(value, _originalText)) return;
        _originalText = value;
        OnPropertyChanged();
        }
        }

        public String ModifyedText
        {
        get => _modifyedText;
        set
        {
        if (Equals(value, _modifyedText)) return;
        _modifyedText = value;
        OnPropertyChanged();
        }
        }

        public ICommand Obfuscate { get; set; }

        public GuestViewModel()
        {
        Obfuscate = new RelayCommand(DoObfuscate, o => true);
        }

        private void DoObfuscate(object obj)
        {
        ModifyedText = DoModify(OriginalText);
        }

        //Implementation of INotifyPropertyChanged and DoModify etc...
        }


        Now bind the String properties to the two RichTextBoxes (there are multiple option for doing this see Richtextbox wpf binding) and the Button to the Command.



        Xaml may be like:



        <Grid DataContext="{Binding TODO}">
        <Grid.RowDefinitions>
        <RowDefinition Height="150"></RowDefinition>
        <RowDefinition Height="25"></RowDefinition>
        <RowDefinition Height="150"></RowDefinition>
        </Grid.RowDefinitions>
        <RichTextBox Grid.Row="0">
        <FlowDocument PageHeight="180">
        <Paragraph>
        <Run Text="{Binding OriginalText, Mode=TwoWay}"/>
        </Paragraph>
        </FlowDocument>
        </RichTextBox>
        <Button Grid.Row="1" Content="Process Text" Command="{Binding Obfuscate}"></Button>
        <RichTextBox Grid.Row="2">
        <FlowDocument PageHeight="180">
        <Paragraph>
        <Run Text="{Binding ModifyedText, Mode=TwoWay}"/>
        </Paragraph>
        </FlowDocument>
        </RichTextBox>
        </Grid>


        More Information about bindings can be found here: https://blogs.msdn.microsoft.com/jerrynixon/2012/10/12/xaml-binding-basics-101/






        share|improve this answer












        In would not do this in CodeBehind, it is simple with Binding. So define a ViewModel where you define the OriginalText and ModifyedText as Property and a Command for the Transformation in case the Button is clicked.



        So this can be like:



        public class MyViewModel: INotifyPropertyChanged
        {
        private String _originalText = "My Original Text";
        private String _modifyedText = "Place for Obfuscatet Text";

        public String OriginalText
        {
        get => _originalText;
        set
        {
        if (Equals(value, _originalText)) return;
        _originalText = value;
        OnPropertyChanged();
        }
        }

        public String ModifyedText
        {
        get => _modifyedText;
        set
        {
        if (Equals(value, _modifyedText)) return;
        _modifyedText = value;
        OnPropertyChanged();
        }
        }

        public ICommand Obfuscate { get; set; }

        public GuestViewModel()
        {
        Obfuscate = new RelayCommand(DoObfuscate, o => true);
        }

        private void DoObfuscate(object obj)
        {
        ModifyedText = DoModify(OriginalText);
        }

        //Implementation of INotifyPropertyChanged and DoModify etc...
        }


        Now bind the String properties to the two RichTextBoxes (there are multiple option for doing this see Richtextbox wpf binding) and the Button to the Command.



        Xaml may be like:



        <Grid DataContext="{Binding TODO}">
        <Grid.RowDefinitions>
        <RowDefinition Height="150"></RowDefinition>
        <RowDefinition Height="25"></RowDefinition>
        <RowDefinition Height="150"></RowDefinition>
        </Grid.RowDefinitions>
        <RichTextBox Grid.Row="0">
        <FlowDocument PageHeight="180">
        <Paragraph>
        <Run Text="{Binding OriginalText, Mode=TwoWay}"/>
        </Paragraph>
        </FlowDocument>
        </RichTextBox>
        <Button Grid.Row="1" Content="Process Text" Command="{Binding Obfuscate}"></Button>
        <RichTextBox Grid.Row="2">
        <FlowDocument PageHeight="180">
        <Paragraph>
        <Run Text="{Binding ModifyedText, Mode=TwoWay}"/>
        </Paragraph>
        </FlowDocument>
        </RichTextBox>
        </Grid>


        More Information about bindings can be found here: https://blogs.msdn.microsoft.com/jerrynixon/2012/10/12/xaml-binding-basics-101/







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 at 14:44









        Daniel W.

        224111




        224111






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272153%2fc-sharp-wpf-how-to-transfer-text-from-richtexbox-to-another-richtexbox-using-but%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)