:: Home

  login:         
  passwords:  

WPF FAQs

WPF General Questions
Concepts Element Tree
Concepts Dependency Property
Concepts Attached Property
Concepts Routed Events
Concepts Resources.
Concepts Animation
Concepts Freezable Object
Concepts Input and Commands.
Concepts Layouts
XAML Inline Styles and Templates
XAML XML Namespaces
XAML Code Behind
XAML Custom Class
XAML Type Converters
Content Model ItemControl
Content Model HeaderedItemsControl
Content Model HeaderedContentControl
Content Model Content Control
Content Model Decorator Content Model
Content Model Panel Content Model
Documents Serialization and Storage
Documents Annotations
Documents Flow Content
Documents Printing
Graphics and Multimedia Rendering Graphics
Graphics and Multimedia Animation and Timing
Graphics and Multimedia 2D Graphics
Graphics and Multimedia 3D Graphics
Graphics and Multimedia Visual Layer
Control Customization Adorners
Control Customization Stylable controls
Control Customization ControlTemplates
Data Data Binding
Data DragandDrop
Data Serialization
Globalization and Localization Attributes
Globalization and Localization Comments
Globalization and Localization Globalization Struc
Application and Deployment ClickOnce
Application and Deployment Frame
Application and Deployment Page
Application and Deployment Navigation
Application and Deployment Setup
Application and Deployment Window
Interoperability Message Loops Between
Interoperability Win32 in WPF
Interoperability WPF in Win32
Interoperability Windows Forms and WPF
Security Trusted Security
Security Partial Trust Security
Tools Microsoft Expression Blend
Tools ZAM3D
Tools XAMLPAD
Controls ToolTip
Controls TextBlock
Controls TabControl
Controls ProgressBar
Controls PrintDialog
Controls Popup
Controls TextBox
Controls Canvas
Controls ComboBox
Controls ListBox
Controls StatusBar
Controls ToolBar
Controls Context Menu
Controls Expander
Controls DocumentViewer
Controls FlowDocumentReader
Controls GroupBox
Controls GridSplitter
Controls Image
Controls Menu
Controls ScrollBar
Controls Slider
Controls RichTextBox
Controls Border
Controls Buttons

SilverLight Interview Qs

SAP Interview Questions

Oracle Interview Questions

PHP Interview Questions

Ajax Interview Questions

IIS 7.0

OOP Interview Questions

Ruby Interview Questions

Sql Server Interview Questions

Winforms Interview Questions

SharePoint 2007 Questions

Microsoft Crm Questions

Controls RichTextBox Interview Questions & FAQs

Questions and answers for Controls RichTextBox.

77. Controls RichTextBox

    77.1 How do I add a hyperlink to a RichTextBox control ?
    77.2 How do I create and save a RichTextFormat (RTF) file?
    77.3 How do I support drag-and-drop for a RichTextBox?
    77.4 How can I Save, Load, and Print RichTextBox Content ?

77.1 How do I add a hyperlink to a RichTextBox control ?

To add a hyperlink to the RichTextBox, so that it opens up the link you click on, ensure that "DetectUrls" property is set to true and call:

[C#]

private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)

{

System.Diagnostics.Process.Start( e.LinkText );

}


77.2 How do I create and save a RichTextFormat (RTF) file?

One way to do this is to use the "RichTextBox.SaveFile()" method.

[C#]

private void button1_Click( object sender, EventArgs e )

{

// get a file name from the user

SaveFileDialog saveFile1 = new SaveFileDialog();

saveFile1.DefaultExt = "*.rtf";

saveFile1.Filter = "RTF Files|*.rtf";

if ( saveFile1.ShowDialog() == DialogResult.OK )

richTextBox1.SaveFile( saveFile1.FileName, RichTextBoxStreamType.RichText )

}


77.3 How do I support drag-and-drop for a RichTextBox?

You must set the "AllowDrop" property on the RichTextBox control to true and also handle both the "DragEnter" and "DragDrop" events.

Add event handlers to the control as given below.

$$[C#] using System.Windows.Forms; richTextBox1.DragEnter += new DragEventHandler( richTextBox1_DragEnter ); richTextBox1.DragDrop += new DragEventHandler( richTextBox1_DragDrop ); Write event handlers : { private void richTextBox1_DragEnter(object sender, DragEventArgs e) { e.Effect = e.Data.GetDataPresent(DataFormats.Text) ? DragDropEffects.Copy : DragDropEffects.None; } private void richTextBox1_DragDrop(object sender, DragEventArgs e) { // Load the file into the control string text = (string)e.Data.GetData("Text"); richTextBox1.LoadFile(text, RichTextBoxStreamType.RichText); } public Window1() { InitializeComponent(); } } $$


77.4 How can I Save, Load, and Print RichTextBox Content ?

This can be done using the XAML code as given below.

[XAML]

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

x:Class="SDKSample.SaveLoadPrintRTB" >

<StackPanel>

<RichTextBox Name="richTB">

<FlowDocument>

<Paragraph>

<Run>Paragraph 1</Run>

</Paragraph>

</FlowDocument>

</RichTextBox>

<Button Click="SaveRTBContent">Save RTB Content</Button>

<Button Click="LoadRTBContent">Load RTB Content</Button>

<Button Click="PrintRTBContent">Print RTB Content</Button>

</StackPanel>

</Page>

 

Below is the code-behind for the example.

[C#]

// Handle "Save RichTextBox Content" button click.
void SaveRTBContent(Object sender, RoutedEventArgs args)
{

// Send an arbitrary URL and file name string specifying
// the location to save the XAML in.
SaveXamlPackage("C:\\test.xaml");
}

// Handle "Load RichTextBox Content" button click.
void LoadRTBContent(Object sender, RoutedEventArgs args)
{
// Send URL string specifying what file to retrieve XAML
// from to load into the RichTextBox.
LoadXamlPackage("C:\\test.xaml");
}

// Handle "Print RichTextBox Content" button click.
void PrintRTBContent(Object sender, RoutedEventArgs args)
{
PrintCommand();
}

// Save XAML in RichTextBox to a file specified by _fileName
void SaveXamlPackage(string _fileName)
{
TextRange range;
FileStream fStream;
range = new TextRange(richTB.Document.ContentStart, richTB.Document.ContentEnd);
fStream = new FileStream(_fileName, FileMode.Create);
range.Save(fStream, DataFormats.XamlPackage);
fStream.Close();
}

// Load XAML into RichTextBox from a file specified by _fileName
void LoadXamlPackage(string _fileName)
{
TextRange range;
FileStream fStream;
if (File.Exists(_fileName))
{
range = new TextRange(richTB.Document.ContentStart, richTB.Document.ContentEnd);
fStream = new FileStream(_fileName, FileMode.OpenOrCreate);
range.Load(fStream, DataFormats.XamlPackage);
fStream.Close();
}
}

// Print RichTextBox content
private void PrintCommand()
{
PrintDialog pd = new PrintDialog();
if ((pd.ShowDialog() == true))
{
//use either one of the below
pd.PrintVisual(richTB as Visual, "printing as visual");
pd.PrintDocument((((IDocumentPaginatorSource)richTB.Document).DocumentPaginator), "printing as paginator");
}
}

bottom user control
::  Home :: Services ::  Prices ::  Request Quote
Copyright 2007, Megasolutions Ltd