How can I make a hyperlink work in a RichTextBox? - c#

When I add www.stackoverflow.com into my RichTextBox and run the program it is shown in blue and as a hyperlink yet when I click it nothing happens. How can I fix this?

Make sure the text property includes a valid url. E.g. http://www.stackoverflow.com/
set the DetectUrls property to true
Write an event handler for the LinkClicked event.
Personally, I wouldn't pass "IExplore.exe" in as a parameter to the Process.Start call as Microsoft advise as this presupposes that it is installed, and is the user's preferred browser. If you just pass the url to process start (as per below) then Windows will do the right thing and fire up the user's preferred browser with the appropriate url.
private void mRichTextBox_LinkClicked (object sender, LinkClickedEventArgs e) {
System.Diagnostics.Process.Start(e.LinkText);
}

RichTextBox class allows you to customize its behavior when user clicks the hyperlink. Add an event handler for the RichTextBox.LinkClicked event
Process p = new Process();
private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
p = Process.Start("IExplore.exe", e.LinkText);
}

You should make sure that DetectUrls is set to true. If that doesn't work on its own, you may need to add a handler for the LinkClicked event.

Is yourTextBox.DetectUrls set to true? We may need some more info to provide a better answer.

Related

How to open a link from a rich text box [duplicate]

When I add www.stackoverflow.com into my RichTextBox and run the program it is shown in blue and as a hyperlink yet when I click it nothing happens. How can I fix this?
Make sure the text property includes a valid url. E.g. http://www.stackoverflow.com/
set the DetectUrls property to true
Write an event handler for the LinkClicked event.
Personally, I wouldn't pass "IExplore.exe" in as a parameter to the Process.Start call as Microsoft advise as this presupposes that it is installed, and is the user's preferred browser. If you just pass the url to process start (as per below) then Windows will do the right thing and fire up the user's preferred browser with the appropriate url.
private void mRichTextBox_LinkClicked (object sender, LinkClickedEventArgs e) {
System.Diagnostics.Process.Start(e.LinkText);
}
RichTextBox class allows you to customize its behavior when user clicks the hyperlink. Add an event handler for the RichTextBox.LinkClicked event
Process p = new Process();
private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
p = Process.Start("IExplore.exe", e.LinkText);
}
You should make sure that DetectUrls is set to true. If that doesn't work on its own, you may need to add a handler for the LinkClicked event.
Is yourTextBox.DetectUrls set to true? We may need some more info to provide a better answer.

Disable textbox after typing in a value C#

I want to disable a textbox after user has inserted a value. I have tried playing with the "KeyPress" property but the first digit I type in is also the "KeyPress", so it locks on the first character. Maybe I can get the textbox to lock after pressing Enter or using Tab. What will be the best way to do this?
It sounds like you want to disable the box after the person leave it. (I assume that from your Enter/Tab option.) If so, look into the TextBox.LostFocus event.
Though, the "Best" generally depends on the customer requirement, I would do the disabling act on the Lost Focus event.
private void textBox2_Leave(object sender, EventArgs e)
{
textBox2.Enabled = false;
}
Thanks
Just use Leave event and in body assign Enabled to false;
you can use the LostFocus event or the ManipulationCompleted event handler

Registering to event and create a new method at run time - c#

How can I register to event and do some actions at runtime?
For example when the user click on a button I want to register to OnMyEvent and run MyFunc that let's say initialize some textBox with the OnMyEvent args.
I'm using winforms and .NET 4.
EDIT
Maybe I was unclear... I need the ability to register to existing event and add a new method that will run when the event will fire. All at runtime.
EDIT2
i'll try to give an example...
lets say that i have a class named "A" that have many events OnDataRecived OnDataSend etc...
when the application running the user can choose form a combobox event name to register (i got the events list via reflection because they not constracts, they are generated from xml file) and which data to update when the choosed event is fired.
so for the example the user choose to register to the OnDataReceived and he choose to update property named DataStream. some code...
in run time upon user choosing:
A.OnDataReceived += (s,e) => MyRunTimeMethod(s,e);
private void MyRunTimeMethod(object sender, EventArgs e)
{
DataStream = e.Data.Value
}
You are asking how to create a method dynamically at runtime - once you have a reference to that method in a delegate, the question of how to register it to an event is trivial.
MSDN describes how to do this with MSIL instructions. I doubt that's what you're looking for, but it is an option.
The C# FAQ blog has a much more interesting solution using expression trees. I suppose this is the one you were referring to by originally tagging your post with expression-tree.
But I would reconsider using dynamic methods at all. How exactly is the user going to specify what action to perform on the event of his choice? I suspect that the options are limited enough that you can get by with something simpler:
protected void btnRegister_Click(object sender, EventArgs e) {
switch (cmbEvents.SelectedText) {
case "OnLoad":
MyControl.OnLoad += (s, e) => SomeSelectedControl.Text = SomeInputControl.Text;
break;
//... other cases
}
}
If you're using windows forms, double clicking a button will bring you to a created on_click event. If you bring up the properties window for the button, theres an events tab. Viewing this will show you which events are available for a control.
I found the best way to understand this, was to look at the code created when adding the events.
Update:
As noted, I completely missed the point with my answer. The syntax for subscribing to an event at runtime is the same way as it's done on form Initialize. So I don't get any terminology wrong, here's the link to the msdn documentation;
http://msdn.microsoft.com/en-us/library/ms366768.aspx
What you want to achieve, does not require you to "Register to event at run time".
If button1 is the button of interest here, simply use.
button1.Click += buton1_ClickHandler;
button1_ClickHandler should be defined in the same class as your button1. and it should have the signature of the RoutedEventHandler. So, it should be
private void button1_ClickHandler(object sender, RoutedEventArgs e)
{
//method code here
}

Execute method On-Load of an object on a form in C#

E.g. instead of having a button to initiate the method, the method automatically happens without any user interaction - automatically.
private void button13_Click(object sender, EventArgs e)
{
try
{
ServiceController sc = new ServiceController();
sc.ServiceName = "Spooler";
if (sc.Status.ToString().ToLower() == "stopped")
{
serviceStatusLabel.Text = "Installed but stopped";
}
if (sc.Status.ToString().ToLower() == "running")
{
serviceStatusLabel.Text = "Installed and started";
}
}
catch
{
serviceStatusLabel.Text = "Service not installed";
}
}
I just want the Label object to show the service status when the form is loaded up, without using a button
EDIT: Given your comment, are you actually after the Form.Load event? It sounds like it. Any event handlers subscribed to that event will be executed "when the form is displayed for the first time".
(The confusing thing is that your title talks about "On-Load" of an object whereas it sounds like you really want the method to be called when the form is loaded.)
It's not really clear what you mean by "when its output on the form" but you might want to look at the TextChanged and VisibleChanged events. That's if you want something to happen when the label is altered.
If you're looking for when the service status is altered, it doesn't look like there's an event raised for that, I'm afraid. Note that it would be much cleaner to switch on the enum value rather than to convert it to a string, lower it, and then compare that with hard-coded constants.
... Do I get your question correctly?
You want a piece of code to be executed when an object or the form is loaded?
Well that's easy :p
Click on your object (or form) in the designer, in the properties dock, click the lightning bolt icon, go to the Load or Show event, and double-click the box.
A new piece of code should now be created in the code view, something like this:
private void Object_Load(blabla) handles Object.Load
{
}
Whatever code is in that event will be executed when the object is loaded or shown.
If you create a handler for the Load event, it will run when the form gets loaded.

Is it possible to disable the clicking of links in a WPF Frame control?

I have a WPF Frame control in an application that I use to load a preview of a page that doesn't exist on the server yet.
Because it doesn't exist, and because it's inside my app, I need to figure out a way to ideally disable the hyperlinks so they can't be clicked. Although, forcing it to load in a new window so it's no longer in my app is an acceptable workaround.
Unfortunately, messing with the HTML isn't an option in this instance.
Given it's IE behind the scenes, is this even possible?
You should just be able to hook the Frame::Navigating event and then set the NavigatingCancelEventArgs::Cancel property to true.
Admittedly I don't fully understand what you are trying to do but I have disable users ability to click on controlls in WPF before by setting e.Handled = true; in the correct event.
Try mouse down.
private void ContentControl_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
e.Handled = true;
}
use a System.Windows.Control.WebBrowser and on "Navigated" event turn to false a global bool variable that is initially set to true (On window "loaded" event).
In System.Windows.Controls.WebBrowser Navigating event check this bool var and set e.cancel to true if you want links to be prevented. Other Controls may be used in order to change the value of this global bool variable to true and enable navigation.

Categories