Which code segment should you use?

You are creating a Windows Presentation Foundation (WPF) application by using Microsoft .NET Framework 3.5.
You plan to implement a search functionality for a text editor. You write the following code segment for the WPF screen.

<DockPanel>
<WrapPanel DockPanel.Dock="Top">
<TextBox Name="tbxTextToFind" Width="200" />
<Button Name="btnFind" Width="80" Click="btnFind_Click">Find</Button>
</WrapPanel>
<RichTextBox Name="rtbText" />
</DockPanel>

You need to ensure that on clicking the Find button, the value of the tbxTextToFind text box is selected in the rtbText control.
Which code segment should you use?

You are creating a Windows Presentation Foundation (WPF) application by using Microsoft .NET Framework 3.5.
You plan to implement a search functionality for a text editor. You write the following code segment for the WPF screen.

<DockPanel>
<WrapPanel DockPanel.Dock="Top">
<TextBox Name="tbxTextToFind" Width="200" />
<Button Name="btnFind" Width="80" Click="btnFind_Click">Find</Button>
</WrapPanel>
<RichTextBox Name="rtbText" />
</DockPanel>

You need to ensure that on clicking the Find button, the value of the tbxTextToFind text box is selected in the rtbText control.
Which code segment should you use?

A.
FlowDocument doc = rtbText.Document;
string text = (new TextRange(doc.ContentStart, doc.ContentEnd)).Text;
int index = text.IndexOf(tbxTextToFind.Text);
TextPointer start = doc.ContentStart.GetPositionAtOffset(index);
TextPointer end = start.GetPositionAtOffset(tbxTextToFind.Text.Length);
rtbText.Selection.Select(start, end);

B.
TextPointer cur = rtbText.Document.ContentStart;
while (cur != null)
{
TextPointer end = cur.GetPositionAtOffset(tbxTextToFind.Text.Length);
if (end != null )
{
TextRange search = new TextRange(cur, end);
if (search.Text == tbxTextToFind.Text)
{
rtbText.Selection.Select(search.Start, search.End);
break;
}
}
cur = cur.GetNextContextPosition(LogicalDirection.Forward);
}

C.
TextPointer cur = rtbText.Document.ContentStart;
while (cur != null)
{
TextPointer end = cur.GetPositionAtOffset(tbxTextToFind.Text.Length);
if (end != null )
{
TextRange search = new TextRange(cur, end);
if (search.Text == tbxTextToFind.Text)
{
rtbText.Selection.Select(search.Start, search.End);
break;
}
}
cur = cur.GetNextInsertionPosition(LogicalDirection.Forward);
}

D.
FlowDocument doc = rtbText.Document;
string text = (new TextRange(doc.ContentStart, doc.ContentEnd)).Text;
int index = text.LastIndexOf(tbxTextToFind.Text);
TextPointer start = doc.ContentStart.GetPositionAtOffset(index + 1);
TextPointer end = start.GetPositionAtOffset(tbxTextToFind.Text.Length);
rtbText.Selection.Select(start, end);



Leave a Reply 0

Your email address will not be published. Required fields are marked *