-3

I have a small problem, I have created a private chat message system using c#. Now what I need is a way to send a clickable link to other person.

When selecting a person from the list, I press invite button and a message comes to the messagebox like "to User1: join from this link"

private void InvBtn_Click(object sender, RoutedEventArgs e)
{
    selectedUser = UsersListBox.SelectedItem.ToString();
    if (selectedUser != login)
    {
        MessageBox.Show("Select other user than yourself");
        return;
    }
    else
    {               
        Msg.Text = selectedUser + " join from this 'link' ";
    }
}

After sending the other person gets the message to RichTextBox saying

From user2: join from this link

There is no need for open a website, but other other form where will be more details.

phändä
  • 1
  • 1
  • 1
    What exactly is your question? How to embed clickable elements in text-runs? How to find urls in strings? How to open a second window? Is *"what I need is a way to send a hyperlink to other person"* the relevant part? – Manfred Radlwimmer Mar 27 '18 at 12:32
  • The question was bit poorly designed. But i need a way to send a link to other person which opens a second window. Hyperlink is not that relevant. @ManfredRadlwimmer – phändä Mar 27 '18 at 12:39
  • Well, I get that, but what part of that is giving you trouble? You certainly didn't just stop your development and decided to ask on StackOverflow instead. Which part of that process blocks your progress? Also: this "hyperlink" should probably just be some kind of markup that's not actually a hyperlink (as in links to something on the internet) but just some specially formatted piece of text, right? (or is there *any* interaction with a webserver involved? - that google.fi example didn't really explain it) – Manfred Radlwimmer Mar 27 '18 at 12:41
  • Yeah, it doesn't need to be a hyperlink to website. But yeah it needs to be a kind of specially formatted text which you can click. Mainly the problem is sending that "link" to other person which he can click. @ManfredRadlwimmer – phändä Mar 27 '18 at 12:51
  • Ok, I see - do you already use some kind of markup for your chat, like bold, italics or is everything pure text until now? – Manfred Radlwimmer Mar 27 '18 at 12:51
  • Everything is now just pure text, no bolds or italics. @ManfredRadlwimmer – phändä Mar 27 '18 at 12:55
  • I might be able to write up a small example for something like that, be right back. – Manfred Radlwimmer Mar 27 '18 at 12:55
  • Check this answer: https://stackoverflow.com/a/20179317/8507673 – sTrenat Mar 27 '18 at 13:03

2 Answers2

0

You need to create custom MessageBox with Hyperlink button.

Try this out, here u need to set the height and width property properly....and make the constructor to accept the arguments so that users can design it the way they want.

 public class CustomMessageBox
    {
        public CustomMessageBox()
        {
            Window w = new Window();
            DockPanel panel = new DockPanel();
            TextBlock tx = new TextBlock();
            Paragraph parx = new Paragraph();
            Run run1 = new Run("Text preceeding the hyperlink.");
            Run run2 = new Run("Text following the hyperlink.");
            Run run3 = new Run("Link Text.");
            Hyperlink hyperl = new Hyperlink(run3);
            hyperl.NavigateUri = new Uri("http://search.msn.com");
            tx.Inlines.Add(hyperl);
            panel.Children.Add(tx);
            w.Content = panel;
            w.Show();
        }
    }

Source : https://social.msdn.microsoft.com/Forums/vstudio/en-US/57fcd28b-6e9e-4529-a583-892c8f6d7cc8/hyperlink-in-wpf-message-box?forum=wpf

Anant Dabhi
  • 10,864
  • 3
  • 31
  • 49
0

First you need to come up with a way to include your special markup in the text message. You can either pack the entire message in an existing container format (XML, JSON, etc.) or to keep things simple include special markers within the text, for example:

Hi User1, join from [This link:12345].

The same way you could include markup for other things like bold (**bold**), italics (*italics*), or actual hyperlinks to websites.

On the other side, you will need a parser that detects this special markup and replaces it with a clickable link. In the following example I'm using Regex to find and replace all text in the format [Text:Command].

private IEnumerable<Inline> Parse(string text)
{
    // Define the format of "special" message segments
    Regex commandFinder = new Regex(@"\[(?<text>.+)\:(?<command>.+)]");

    // Find all matches in the message text
    var matches = commandFinder.Matches(text);

    // remember where to split the string so we don't lose other 
    // parts of the message
    int previousMatchEnd = 0;

    // loop over all matches
    foreach (Match match in matches)
    {
        // extract the text fore it
        string textBeforeMatch = text.Substring(previousMatchEnd, match.Index - previousMatchEnd);
        yield return new Run(textBeforeMatch);

        previousMatchEnd = match.Index + match.Length;

        // extract information and create a clickable link
        string commandText = match.Groups["text"].Value;
        string command = match.Groups["command"].Value;

        // it would be better to use the "Command" property here,
        // but for a quick demo this will do
        Hyperlink link = new Hyperlink(new Run(commandText));
        link.Click += (s, a) => { HandleCommand(command); };

        yield return link;
    }

    // return the rest of the message (or all of it if there was no match)
    if (previousMatchEnd < text.Length)
        yield return new Run(text.Substring(previousMatchEnd));
}

In the method where you receive the message, you can simply integrate it like this:

// Where you receive the text
// This probably is just a `txtOutput.Text += ` until now
private void OnTextReceived(string text)
{
    txtOutput.Inlines.AddRange(Parse(text));
}

// the method that gets invoked when a link is clicked
// and you can parse/handle the actual command
private void HandleCommand(string command)
{
    MessageBox.Show("Command clicked: " + command);
}

The message Hi User1, join from [this link:1234567890] will show up as Hi User1, join from this link and will invoke HandleCommand("1234567890") when clicked.

enter image description here

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62