I am parsing HTML, generating a various blocks and adding them to RichTextBlock control. However, when I generate a Hyperlink from code behind I don't know how to remove underline property. I've seen posts about creating a HyperlinkButton and setting its Content property but it is definitely not something that I want because I can't make it to be in line with the rest of the text and it looks awful.
Below is the relevant code for generating a hyperlink:
private static Inline GenerateHyperLink(HtmlNode node)
{
Span s = new Span();
InlineUIContainer iui = new InlineUIContainer();
Hyperlink link = new Hyperlink();
link.Inlines.Add(new Run() { Text = node.InnerText });
link.NavigateUri = new Uri(node.Attributes["href"].Value, UriKind.Absolute);
s.Inlines.Add(link);
s.Inlines.Add(new Run() { Text = " " });
return s;
}
From MSDN:
A Hyperlink has a class inheritance that doesn't include
FrameworkElement, so it doesn't have a Style property. Nor does a
Hyperlink have a Template (it's not a true control).
Does it mean that there is no way to achieve that using Hyperlink element?
Related
Hello (and sorry for my English), I have a problem with Printing RTF using RichTextBoxPrintCtrl and TabControl.
1) The tabcontrol got no tabs on Design, when the Form Load, it will get a tab with the method AddTab(Title). (Don't mind about other variables).
private void AddTab(string Name = "Nuova Nota*")
{
RichTextBox Body = new RichTextBox();
Body.Name = "Body";
Body.Dock = DockStyle.Fill;
Body.ContextMenuStrip = contextMenuStrip1;
TabPage NewPage = new TabPage();
string DocumentText = Nome;
if (Nome == "Nuova Nota*")
{
TabCount += 1;
DocumentText = Nome + TabCount;
}
NewPage.Name = DocumentText;
NewPage.Text = DocumentText;
tabControl1.Visible = true;
NewPage.Controls.Add(Body);
tabControl1.TabPages.Add(NewPage);
tabControl1.SelectedTab = NewPage;
Nomi_Files.Add(NewPage.Text);
Path_Files.Add("");
}
2) Once the tab is created, you can start to write, change colors, fonts, etc...
To get access on the document that you are making, i use a GetCurrentDocument that return the "Body" of the selected tab:
private RichTextBox GetCurrentDocument
{
get { return (RichTextBox)tabControl1.SelectedTab.Controls["Body"];}
}
Now, all the functions (save, open, fonts, colors...) Works Fine, i wanted to print my document and keep the style, so i Googled and i found this: How to print the content of a RichTextBox control by using Visual C#
I made the RichTextBoxPrintCtrl.dll, added the resource on my project, added the item inside the toolbox, but i can't change the RichTextBox that i create from Code, with RichTextBoxPrintCtrl.
The error that i get is:
Error 1 'RichTextBoxPrintCtrl' is a 'namespace' but is used like a
'type'
How i can use that RichTextBoxPrintCtrl without drag and drop it inside the design form?
Ok, i figured out how to solve it:
instead of declaring like:
RichTextBoxPrintCtrl NameControl = new RichTextBoxPrintCtrl();
we need to declare the namespace so:
RichTextBoxPrintCtrl.RichTextBoxPrintCtrl NameControl = new RichTextBoxPrintCtrl.RichTextBoxPrintCtrl();
Everything works fine :) thanks;
I have a HyperLink control with text in its Text property.
With the following code:
var link = new HyperLink();
var img = new HtmlGenericControl("img");
img.Attributes.Add("src", "text.png");
link.Text = "Test";
link.Controls.Add(img);
When I do this, the image is rendered inside a a tag, but the text is not rendred.
Is there a way to render both the image and the text inside the Text property without throwing a third control in to the mix?
When you put any controls into the WebControl.Controls collection, it will ignore what you have inside Text. So if you want to render both text and other child controls, you should add the text into Controls:
var link = new HyperLink();
var img = new HtmlGenericControl("img");
img.Attributes.Add("src", "text.png");
link.Controls.Add(new Literal{ Text = "Test"}); // this line will add the text
link.Controls.Add(img);
I feel this should work out for you.
var link = new HyperLink();
var img = new HtmlGenericControl("img");
var lbl = new Label();
img.Attributes.Add("src", "text.png");
lbl.Text = "Test";
link.Controls.Add(img);
link.Controls.Add(lbl);
this.Controls.Add(link);
According to the MSDN article "The HyperLink control can be displayed as text or an image." So the answer is no, I'm afraid.
I'm trying to generate some html programmatically in my code behind for a user control I'm designing.
I've been looking around, but can't seem to figure out how to dynamically generate some h1 tags for content I'll be displaying.
Is it just a Label with a special property set?
var h1 = new HtmlGenericControl("h1");
h1.InnerHtml = "header content";
You could also use:
<h1 runat="server" />
You can use label or literal controls as shown below:
Label1.Text = "<h1>HI All</h1>";
Or
string heading = "HI All";
Label1.Text = string.Format("<h1>{0}</h1>", heading);
You can do this easily by
literall.Text ="<h1>ABCD</h1>";
Let, you have a server control like this,
<div class="cls" runat="server" id="MyServerControlDiv"></div>
using HtmlGenericControl
var h1 = new HtmlGenericControl("h1");
h1.InnerHtml = "header content";
MyServerControlDiv.Controls.Add(h1);
using LiteralControl
MyServerControlDiv.Controls.Add(new LiteralControl("<h1>"));
MyServerControlDiv.Controls.Add(new LiteralControl("header content"));
MyServerControlDiv.Controls.Add(new LiteralControl("</h1>"));
Use Literal control, Literal1.Text = "<h1>" + texttodisplay + "</h1>";
ASP literals may help you.
http://ganeshmohan.wordpress.com/aspnet-and-c-generate-html-controls-dynamically/
Personally I prefer to make a class for it.
public class H1: HtmlGenericElement
{
public H1(): base("h1") { }
}
Or maybe.
public class H: HtmlGenericControl
{
public H(int size): base("h" + size) { }
}
Let us assume that you have an asp placeholder in your design:
PlaceHolder placeHolder1 = new PlaceHolder();
string yourText = "Header Text";
Label myLabel = new Label();
myLabel.Text = "<h1>" + yourText + "</h1>";
PlaceHolder1.Controls.Add(myLabel);
OR
PlaceHolder1.Controls.Add(new LiteralControl ("<h1>")) // and likewise
for </h1>.
Using a generic HTML control or a literal control is fine just so long as you don't want to insert any child controls. If you do need to append a child control such as a label for example, then you will want to create you header as a Web Control like follows:
WebControl H2Header = new WebControl(HtmlTextWriterTag.H1);
And then you can append a child control like this:
Label labHeader = new Label() { Text = "Some Header Text" };
H2Header.Controls.Add(labHeader);
What i always prefer to use:
In the front end put the desired html control like
<h1 id="title" runat="server">XXX</h1>
Then in the behind code declare the desired control like
protected HtmlGenericControl PublicTitle
{
get
{
return this.title;
}
}
And anywhere in the code play whith the control
PublicTitle.InnerHtml = state.Equals("win") ? "Win" : "Lost";
PublicTitle.Attributes.Add("src", "https://xxxx");
I want to be able to have some text clickable like in webpages in WPF. The control should have both non-functional text (for display) both also some of its parts as completely clickable.
Say like Wikipedia.
But this is an independent standalone offline app.
I tried various things but I couldn't do it, especially the clicking doesn't function like web pages, i.e. 1 click to open the url contained within the tools.
If you don't have a requirement that it be a full-blown FlowDocument, then you can just use a plain old WPF TextBlock, and put Hyperlinks in it.
<TextBlock>
Here's some text with a
<Hyperlink NavigateUri="Page2.xaml">link to another XAML page</Hyperlink>
and a
<Hyperlink NavigateUri="http://msdn.microsoft.com/">link to the
Web</Hyperlink>.
</TextBlock>
If you need scrolling, just put a ScrollViewer around it.
If you need the paginated, multi-column viewer, then you'll need to go with an all-out FlowDocument, but if all you want is text with hyperlinks, TextBlock + Hyperlink should be all you need.
you should try setting the flow document manually and creating hyperlinks within the flow document...
Here is some text taken from the following link:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/99ae9d9c-1dd4-4acd-8d5d-6eb739adeb98
"
Hi,
It is possible.
Here is a small example of creating hyperlink to paragraph/section/table.
In order to navigate to website, we can create a Frame Control for navigation. The hierarchical relationship of elements in this example is like this :
Frame-->FlowDocument-->Table-->Section-->Paragraph-->Hyperlink
In the code behind:
public Window1()
{
InitializeComponent();
// add a Frame for navigation
Frame frame = new Frame();
this.Content = frame;
//add FlowDocument
FlowDocument doc = new FlowDocument();
frame.Navigate(doc);
//add Table
Table table = new Table();
doc.Blocks.Add(table );
TableRowGroup group = new TableRowGroup();
table.RowGroups.Add(group );
TableColumn col1 = new TableColumn();
TableColumn col2 = new TableColumn();
table.Columns.Add(col1 );
table.Columns.Add(col2);
TableRow row1 = new TableRow();
TableCell cel1 = new TableCell();
row1.Cells.Add(cel1);
group.Rows.Add(row1);
//add Section
Section mySection = new Section();
//add section to the Table cell.
cel1.Blocks.Add(mySection);
Paragraph paraValue = new Paragraph();
Hyperlink hl = new Hyperlink(new Run("Click Here to Google"));
hl.Foreground = Brushes.Red;
paraValue.Inlines.Add(hl);
hl.FontSize = 11;
hl .NavigateUri =new Uri ("Http://www.google.cn");
mySection.Blocks.Add(paraValue);
}
If you have any additional question about this,please feel free to ask.
Thanks. "
I'm trying to add inlines to a text block using the code below. The text block's window uses a Themes.xaml file for styling, but when I add the runs dynamically, the styling does not get applied. Can you help me understand why?
foreach (string key in wrappingOptions.Keys)
{
Hyperlink link = new Hyperlink(new Run(key));
string s = new string(wrappingOptions[key].ToCharArray());
link.Click += (o, _) => tbIn.SelectedText = string.Format("<{0}>{1}</{0}>",
s, tbIn.SelectedText);
InputLinksBlock.Inlines.Add(link);
}
Hyperlink is a FrameworkContentElement class. It does not derive it's text display properties from the parent TextBlock. You explicitly need to set a default style for a Hyperlink using <Style TargetType="Hyperlink">.