I have a WPF app i'm working on.
There is a label on my window I use as a status window that I manage with a singleton.
How do i insert a link into that that label?
All the answers I see on this have to do with changing the XAML or the nature of the label. These solutions don't work, because literally anything can appear in this status window.
I want the link to raise a custom event when clicked.
Does anyone know how to do this?
Example of how the text uploaded to the label should operate. I need to raise an event when the hypertext formatted word is clicked:
"Inserting a link into text" is the same thing as adding a Hyperlink to the Inlines collection of a TextBlock:
TextBlock tb = new TextBlock();
var hp = new Hyperlink(new Run("error"));
hp.Click += (s, e) => { /* do something */ };
tb.Inlines.Add(new Run("There was as an "));
tb.Inlines.Add(hp);
tb.Inlines.Add(new Run(" on run"));
This is the one and only way to do this in WPF and it is very simple.
Related
I'm just learning how to make universal apps for windows 10. Most of the tutorials show you how to work with XAML and how to assign functions to elements when you click on them but I couldn't find a way to make a new control appear when I click a button.
I'm making a note taking application. I've designed most of the stuff that I need. Now I want whenever I click a button to create a new textblock where the user can write their note.
//Create a new note when clicking the add button
private void newNoteBtn_Click(object sender, RoutedEventArgs e)
{
TextBox newNote = new TextBox();
newNote.Text = "Enter your text";
}
This is the code that runs when the button is clicked. When I run the application nothing happens. I think I have to put the new textbox in some kind of Grid or something.
Most of the tutorials are really old or mention windows forms and use some sort of this.Controls.Add(newNote); but Visual studio doesn't give me the Controls.Add option. I've also created a <Grid x:Name="notes"></Grid> which I thought I could use as a placeholder for the notes that are being created but I can't access the Grid element through the code.
Container Controls like Grid have Children property so you should use Childern like this:
TextBox newNote = new TextBox();
newNote.Text = "Enter your text";
notes.Childern.Add(newNote);
When defining
<Grid x:Name="notes"></Grid>
in XAML on the page, you be able to use notes as the identifier to access this Grid from the page's code behind:
notes.Children.Add(newNote);
I am using a RichTextbox in my program to show multiple line strings in c sharp. I also need to add clickable control like button, label etc. I can add the control inside the RichTextBox properly by the following url below:
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/33dd0de0-1e30-4f68-a3e2-7b4b2f2170af/
Button b = new Button();
b.Text = "123";
b.Size = new Size(30, 30);
b.Location = new Point(50, 50);
b.Click += delegate(object sender, EventArgs e)
{
this.richTextBox1.Text += "123";
};
this.richTextBox1.Controls.Add(b);
But when I scroll the RichTextBox the controls do not move accordingly along with the text. Is there any way to move the controls as well with the text?
Thank you all!
This is incorrect. All you're doing with this code is adding a control into the RTB which acts as a container. It isn't part of the RTF document underneath.
You can draw buttons in there.. via the use of some Shape-Drawing RTF. I will point you to the RTF Specification on how to do that.
However, as for wiring up a click event to those RTF buttons.. I have no idea unfortunately.
(PS, you might be better off going with WPF's FlowDocument for this, which should do what you're after, but requires a bit more learning)
I'm trying to programmatically add a ContextMenu to a Hyperlink. I've searched in the documentation and in forums, and seems that the correct solution should be this:
var link = new Hyperlink();
link.Inlines.Add(new Run() { Text = linkText });
link.FontWeight = FontWeights.Bold;
link.TargetName = linkText;
link.Click += new RoutedEventHandler(link_Click);
ContextMenu menu = new ContextMenu();
MenuItem item = new MenuItem();
item.Click += new RoutedEventHandler(CopyLink);
item.Header = "copy link";
item.Tag = linkText;
menu.Items.Add(item);
ContextMenuService.SetContextMenu(link, menu);
This code compiles and does not throw any exception. I've tested and the ContextMenu is indeed added to the Hyperlink. The problem is that it will not show anytime. I can tap & hold the link all the time of the world and the menu will not appear. Also tried to add a listener with GestureService and GestureListener, but the Hold event does not fire at all.
Anyone can help me? Thanks.
You can't do ContextMenus on Hyperlinks. You can do it on a HyperlinkButton, though. I'm not exactly sure of the reason, but it does work. Depending on what you're trying to do, HyperlinkButton may have been what you wanted anyway (Hyperlink is usually only used inside documents of text).
I just found the answer while reading the Windows Phone 7.5 Unleashed book by Daniel Vaughan. My problem was that I needed to add a hyperlink in a RichTextBox, and I can only use Inlines to show text. Hyperlink is an Inline, but does not support ContextMenu. As Tim suggested, I needed to use HyperlinkButton. The solution is that there is a class called InlineUIContainer. So, this
var inline = new InlineUIContainer { Child = SomeHyperlinkButton }
made the trick for me.
i want to show steps on how to cook something in winform c# .net as steps. Something like a set of text area would be nice but:
-> list box considers the whole string of one step as one item so user needs to scroll horizontally to view the whole step.
-> datagridview is also not suitable as i want the text to word wrapped.
i also want the user to be able to edit the step.
any suggestions of custom control would be nice.
Maybe a wizard like app would be suitable for you. AFAIK there's no native wizard control in C# but you could implement one using tabs or using one of many in the web.
A multi line text box will do the job great. just take a simple text box and do the following to it, and it will turn to a text area:
TextBox listBoxNewInput = new TextBox();
//Initialize label's property
listBoxNewInput.Multiline = true;
// Add vertical scroll bars to the TextBox control.
listBoxNewInput.ScrollBars = ScrollBars.Vertical;
// Allow the RETURN key in the TextBox control.
listBoxNewInput.AcceptsReturn = true;
// Allow the TAB key to be entered in the TextBox control.
listBoxNewInput.AcceptsTab = true;
// Set WordWrap to true to allow text to wrap to the next line.
listBoxNewInput.WordWrap = true;
listBoxNewInput.Width = 315;
listBoxNewInput.Height = 150;
listBoxNewInput.DoubleClick += new EventHandler(listBoxNewInput_DoubleClick);
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
flowLayoutPanel1.Controls.Add(labelInput);
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
flowLayoutPanel1.Controls.Add(list
BoxNewInput);
I have this XAML:
<TextBlock TextWrapping="Wrap" Foreground="Green"
Text="This is some Green text up front. ">
<TextBlock Foreground="Blue">
This is some Blue text.
</TextBlock>
This is some Green text following the Blue text.
<Hyperlink>
<TextBlock Text="And finally, this is a Hyperlink." TextWrapping="Wrap"/>
</Hyperlink>
</TextBlock>
And I'd like to know how to replicate it procedurally in C#.
I know how to create TextBlocks in C# with something like:
TextBlock tb = new TextBlock();
tb.Text="Some text"
And I can put multiple TextBlocks together in a panel within C#. But I don't see how you go about putting TextBlocks into other TextBlocks, and TextBlocks into Hyperlinks into TextBlocks.
Are some container objects and extra TextBlock objects being created automatically, somehow? Or does the TextBlock have some methods/properties that allow it to contain the other items?
Other related questions:
1. What's the best way to add something like a Click() event to the Hyperlink?
2. Is there a way to get the blue text to wrap more cleanly? In the above XAML, as soon as the rightmost word would need to wrap, the entire block of blue text is wrapped instead.
Thanks for any illumination you can provide.
You can modify the collection of Inlines exposed via the TextBlock's Inlines property. The above XAML sample would look a little something like this:
TextBlock tb = new TextBlock
{
Text = "This is some Green text up front.",
Foreground = Brushes.Green
};
InlineCollection tbInlines = tb.Inlines;
tbInlines.Add(new Run
{
Text = "This is some Blue text.",
TextWrapping = TextWrapping.Wrap,
Foreground = Brushes.Blue
});
tbInlines.Add(new Run
{
Text = "This is some Green text following the Blue text."
});
Run hyperlinkRun = new Run("And finally, this is a Hyperlink.");
tbInlines.Add(new Hyperlink(hyperlinkRun));
As for your related questions:
1A) While it's possible to hook an event handler to every single Hyperlink instance using the RequestNavigate event on the class, that can be costly to setup and use more memory than is necessary. Instead I recommend leveraging routed events and simply hooking the RequestNavigate event at the container where all of your hyperlinks will be. You can do this like so:
myContainer.AddHandler(
Hyperlink.RequestNavigateEvent,
new RequestNavigateEventHandler(
(sender, args) =>
{
/* sender is the instance of the Hyperlink that was clicked here */
}));
2A) In your XAML example it's treating the inner TextBlock as an element that needs to be wrapped all together. If you're using my Run based approach the wrapping should be inherited from the containing text block.
I don't know about TextBlocks, but here is how you would do it in a RichTextBox. You could use a RichTextBox instead of a TextBlock.
RichTextBox rtbTest = new RichTextBox();
rtbTest.IsDocumentEnabled = true;
FlowDocument fd = new FlowDocument();
Paragraph para = new Paragraph();
Run r4 = new Run("Some Text To Show As Hyperlink");
Hyperlink h4 = new Hyperlink(r4);
h4.Foreground = Brushes.Red; //whatever color you want the HyperLink to be
// If you want the Hyperlink clickable
h4.NavigateUri = new Uri("Some URL");
h4.RequestNavigate += new RequestNavigateEventHandler(h_RequestNavigate);
// Leaving the two previous lines out will still make the Hyperlink, but it won't be clickable
// use this if you don't want an underline under the HyperLink
h4.TextDecorations = null;
para.Inlines.Add(h4);
fd.Blocks.Add(para);
rtbTest.Document = fd;
For normal text, I just used the HyperLink, but removed the two lines that made it clickable. This would give the text the color, but it wouldn't be clickable. Though it still makes the cursor change still. Though I am sure there is a property to change that also.