C# WPF How to transfer text from richtexbox to another richtexbox using button?
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
add a comment |
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
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
add a comment |
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
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
c# wpf
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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/
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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/
add a comment |
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/
add a comment |
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/
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/
answered Nov 13 at 14:44
Daniel W.
224111
224111
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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