Jump to content

All my products and services are free. All my costs are met by donations I receive from my users. If you enjoy using any of my products, please donate to support me. My bare hosting costs are currently not met so please consider donating by either clicking this text or the Patreon link on the right.

Patreon

Recommended Posts

Posted

Can ayone help me out with this? I'm using Visual C++ but I know some people here use C#. How do I dynamically change the link label to reflect a new hyperlink? I've used this info to create a link. For example. If I click a button how do I change the link to something new? Does that make sense?

<H1 class=heading>Linking to a Web Page</H1>The LinkLabel control can also be used to display a Web page with the default browser.

<H3 class=procedureSubHeading>To start Internet Explorer and link to a Web page with a LinkLabel control</H3>

  1. Set the Text property to an appropriate caption.
  2. Set the LinkArea property to determine which part of the caption will be indicated as a link.
  3. In the LinkClicked event handler, in the midst of an exception-handling block, call a second procedure that sets the LinkVisited property to true and uses the Start method to start the default browser with a URL. To use the Start method you need to add a reference to the System.Diagnostics namespace.
    Security Note:If the code below is run in a partial-trust environment (such as on a shared drive), the JIT compiler fails when the VisitLink method is called. The System.Diagnostics.Process.Start statement causes a link demand that fails. By catching the exception when the VisitLink method is called, the code below ensures that if the JIT compiler fails, the error is handled gracefully.
    Visual Basic Copy CodePrivate Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ Handles LinkLabel1.LinkClicked Try VisitLink() Catch ex As Exception ' The error message MessageBox.Show("Unable to open link that was clicked.") End TryEnd SubSub VisitLink() ' Change the color of the link text by setting LinkVisited ' to True. LinkLabel1.LinkVisited = True ' Call the Process.Start method to open the default browser ' with a URL: System.Diagnostics.Process.Start("http://www.microsoft.com")End Sub
    C# Copy Codeprivate void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e){ try { VisitLink(); } catch (Exception ex ) { MessageBox.Show("Unable to open link that was clicked."); }}private void VisitLink(){ // Change the color of the link text by setting LinkVisited // to true. linkLabel1.LinkVisited = true; //Call the Process.Start method to open the default browser //with a URL: System.Diagnostics.Process.Start("http://www.microsoft.com");}
    Visual C++ Copy Codeprivate: void linkLabel1_LinkClicked(System::Object ^ sender, System::Windows::Forms::LinkLabelLinkClickedEventArgs ^ e) { try { VisitLink(); } catch (Exception ^ ex) { MessageBox::Show("Unable to open link that was clicked."); } }private: void VisitLink() { // Change the color of the link text by setting LinkVisited // to true. linkLabel1->LinkVisited = true; // Call the Process.Start method to open the default browser // with a URL: System::Diagnostics::Process::Start("http://www.microsoft.com"); }

Posted

In the LinkClicked handler if where you open the actual link using Process.Start. So simply change that by using a switch case.

Eg.

enum LinkType { Microsoft, SomewhereElse };

enum LinkType linkLabelType = Microsoft;

void linkLabel1_LinkClicked(Object ^ sender, LinkLabelLinkClickedEventArgs ^ e)
{
switch(linkLabelType)
{
case Microsoft:
Process::Start("http://www.microsoft.com");
break;
case SomewhereElse:
Process::Start("http://www.somewhereelse.com");
break;
}
}

You can also change the Text attribute so the user knows the link has changed. Another method would be to attach a different event handler whenever you want to change the link.

Posted
In the LinkClicked handler if where you open the actual link using Process.Start. So simply change that by using a switch case.

Eg.

enum LinkType { Microsoft, SomewhereElse };

enum LinkType linkLabelType = Microsoft;

void linkLabel1_LinkClicked(Object ^ sender, LinkLabelLinkClickedEventArgs ^ e)
{
switch(linkLabelType)
{
case Microsoft:
Process::Start("http://www.microsoft.com");
break;
case SomewhereElse:
Process::Start("http://www.somewhereelse.com");
break;
}
}

You can also change the Text attribute so the user knows the link has changed. Another method would be to attach a different event handler whenever you want to change the link.

Thanks HK. I forgot about the switch case.

Guest
This topic is now closed to further replies.
×
×
  • Create New...