C# Read text formatting? - c#

I want to be able to read text from a Silverlight TextBlock (TextBlock Control) (Silverlight & C#) and check what formatting (as in: bold, italic, font size, etc...) has been applied to it, so I can store it in an XML file.
Is it possible to find out what formatting has been applied to a piece of text with C# and Silverlight so it can be stored and re-used later? The text would be contained within a textbox or textblock control.
Storage used can be XML but I've just found out Silverlight doesn't support XSL, so just XML.
Regards,
T

Just make sure you give your control a name.
<Textblock x:Name="myTextBlock" />
In your code behind you can then access the TextBlock but calling it's name (myTextBlock).
Here you can add logic like:
if (myTextBlock.FontWeight == "Bold")
{
//Do Something
}
From reading your needs you'll most likely be passing the object to a function and creating your xml file from there. Good luck.

By formatting you mean a phone number or date format?
If yes. Use regular expressions.
Take a look at the System.Text.RegularExpressions namespace. Everything there should help you.

Related

how to automatically extract strings from project and add them to resx file in wpf

I have a very large wpf project and after two years of development i requested to make it bilingual, is there any way to automatically extract strings in labels and grid headers in xaml files so i can translate them instead of manually extract them?
I don't know if there is any existing tool to help, but you can implement it yourself, I faced such a problem when I was converting a large solution of above 100 C++ projects from ansi to unicode strings, I implemented a simple tool with one window containing 2 rich edit boxes, one for the original text with replaces colored, and other for the replacement, for manipulating the resex files have a look at Working with .resx Files Programmatically, Don't forget that regular expression is your friend to accomplish this tool.
You can automate this process by finding all Label controls in WPG root Grid element (e.g. mainGrid) and extracting their content using the following C# code snippet:
IEnumerable<Label> _collection = mainGrid.Children.OfType<Label>();
foreach(Label _control in _collection)
{
string _text = _control.Content.ToString();
// add you code here
}
The results can be placed in Resource file (for example, CSV) with your translation added. Other solution (more sophisticated) pertains to the creation of multilingual XML file, or the local multilingual database with all label contents added to the first field, translation added to the next fields, etc.
Hope this may help.

A method to visually separate text lines in Richtextbox

I would like to know if there is an easier way to add any kind of visual separation like a line between text lines in a Richtextbox?
If not, is there another component that I can use in a Form that would be able to do this for text lines?
Thank you.
It has been a few years, but at one point I wrote a RichTextBox wrapper that ended up manipulating the underlying markup. It wasn't pretty and it wasn't for the faint of heart, but it did work. I ended up using it to highlight some text I believe.
The key thing you need to know is the specification for the RTF markup language: http://en.wikipedia.org/wiki/Rich_Text_Format should get you started.
The RichTextBox class documentation and the Document property should get you started at modifying the underlying structure of the document after loading or during editing.

C# HTML entities from XML string in DataGridView

I'm working with a Web Service that returns an XML string. I need to display the contents of the XML string in a DataGridView to users.
The XML string resembles something like this:
<Field id="13598" type="1"><p><strong>This </strong>is our <em>response</em></p></Field>
So far, I've managed to format output that's something like this which is displayed in my DataGridView:
e.g.
<p><strong>This </strong>is our <em>statement</em></p>
Using System.Net.WebUtility.HtmlDecode
Is there a way for me to display the formatted text in the DataGridView?
e.g.
This is our statement
I'm hoping to achieve useability as the intended users of the program might not be familiar with HTML.
I would've just removed the entities, but I need to retain them as they will be required for writing to an XML string later on as well, due to any editing.
e.g.
<p><strong>This</strong> is our updated <em>statement</em></p>
I've been toying with the idea of using a new form with a TextBox to enable editing as some of these strings might be much longer than this, and could contain other forms of formatting such as bullet points and the like. I'm not too sure how to go about with this. I'm not entirely sure if what I'm hoping to achieve is possible. If not, are there alternative methods for me to achieve this?
You could use the WebBrowser control, and set its content using the NavigateToString method

Integrating Visio exported XAML into Silverlight application

It is possible to export Microsoft Visio drawings as a Website containing Silverlight content. This is described on this blog-post.
The output of such an export are the following:
xaml_1.xaml (contains the structure of the control)
data.xml (contains all text content such as labels, etc)
several java-script files
*.htm pages with a Silverlight container
other files such as *.css and images
I would like to integrate the exported XAML code into another existing Silverlight application. I found this blog-post telling me how to load XAML code dynamically during runtime.
What I would like to know is how to "merge" the XAML-file and the data.xml and how I can get a reference to the items of the XAML code, in order to change certain texts...
In the associated xaml js file (eg xaml_1.js) there's a handleMouseUp function that reads the shape ID from the (XAML) 'name' string and then calls OnShapeClick in frameset.js. This method, which is common to all of the js-based Save as web output types, then calls other methods to populate the details table or retrieve hyperlinks found in data.xml. If you have a look at the FindShapeXML function in frameset.js you'll see that it gets the appropriate data based on the page and shape IDs (note that shape IDs are unique to a page as per Visio itself).
In terms of creating data-bound or dynamic shape text, one workaround for the glyphs issue that #slfan highlights is prevent the text from being output. For example, prior to running Save As Web in Visio, you could loop through all of the shapes and set their HideText ShapeSheet cell to true. This will prevent all of the glyphs xaml being generated and you'll still have access to the text string in data.xml. I guess you wouldn't then benefit from the correct font scaling, but it depends on your scenario. If it was really important to get the scale right then you could parse the RenderTransform attribute (which is described in attribute syntax rather than property element syntax) of the glyph elements.
Glyphs are there (I'm guessing) because it mirrors how Visio works in the application ie in Visio you can select individual characters within a shape's text and apply different fonts and formatting, but if you don't need that, I'd be tempted to ditch the glyphs collection and just use a TextBlock as #slfan suggests.
I think you have to tweak the generated XAML a little bit. Unfortunately Visio generates glyphs for every single character. If you want to change the text at runtime, you will have to remove this glyphs and add the required controls (e.g. TextBlock) yourself.
You can load the XAML into Silverlight with XamlReader.Load. A good description you find here: http://blogs.silverlight.net/blogs/msnow/archive/2008/10/09/silverlight-tip-of-the-day-60-how-to-load-a-control-straight-from-xaml.aspx.
All JavaScript and HTML files you can ignore, the XML-file you need to identify your controls. The ID's in the XML refer to the corresponding elements in the XAML-file.

change font of a string in C#

I am reading a txt file and writing it's content to another txt file.
Before writing the content to new file i have to change the font of the string (string that is read from another file).
How should i do this.
Please help.
Strings don't inherently have fonts. If the string represents RTF or something like that, then that's a different matter - it's effectively changing the font within the RTF format.
However, if you're just writing a plain text file, it's entirely up to the display client which font it uses.
Sorry!! friend. you can't change the fonts or size of string. C# don't know where your are going to write the string text... Console or any text file or label...
Just think what happens if it supports, lets u set some format to string and in place of writing to a file you are printing to console (Console is not rich in fonts)
anyway !! but if you want to do it, it depends on the type of file you are writting. notepad, or rtf orword document. There you can find changing fonts very easily.
If it is pure text file, then you cannot!
If you are writing a Word Doc, using some Office API then you can!
a simple string has no font. A font is used for printing or displaying a string, but a string itself is only text. If you are talking about some encoded text (like rtf) you are reading, you will need to parse the text into something interpretable and go on from there.
If you only want to put out one or more strings in a textbox using different fonts, you will need to use a control that supports formatting.

Categories

Resources