Spread Windows Forms 17
Spread Windows Forms 17.0 Product Documentation / Developer's Guide / Cells / Adding Hyperlink in a Cell
In This Topic
    Adding Hyperlink in a Cell
    In This Topic

    Hyperlinks can be added in cells to access relevant information present at any other location. Spread for WinForms allows you to add hyperlinks which can be used to:

    Add Hyperlinks

    You can add hyperlinks to a sheet or range by using the IWorksheet.Hyperlinks.Add or IRange.Hyperlinks.Add method respectively.

    The below example code shows how to insert hyperlinks to access a range in a workbook, a webpage, or an email address.

    C#
    Copy Code
    // Hyperlink to range locations
      fpSpread1.AsWorkbook().ActiveSheet.Hyperlinks.Add("B3", "", "Sheet1!A1", "Click here to go to A1", "Goto Cell A1");
    
    // Hyperlink to web URLs
       fpSpread1.AsWorkbook().ActiveSheet.Hyperlinks.Add("B5", "http://developer.mescius.com/", "", "Click here to go to mescius website", "Mescius Website");
    
    // Hyperlink to email
       fpSpread1.AsWorkbook().ActiveSheet.Hyperlinks.Add("B7", "mailto:spread.support@mescius.com?subject=spread.support", "", "Click here to mail for spread support", "Mail to: Spread Support");
    

    VB
    Copy Code
    ' Hyperlink to range locations
    fpSpread1.AsWorkbook().ActiveSheet.Hyperlinks.Add("B3", "", "Sheet1!A1", "Click here to go to A1", "Goto Cell A1")
    
    ' Hyperlink to web URLs
    fpSpread1.AsWorkbook().ActiveSheet.Hyperlinks.Add("B5", "http://developer.mescius.com/", "", "Click here to go to mescius website", "Mescius Website")
    ' Hyperlink to email
    
    fpSpread1.AsWorkbook().ActiveSheet.Hyperlinks.Add("B7", "mailto:spread.support@mescius.com?subject=spread.support", "", "Click here to mail for spread support", "Mail to: Spread Support")
    
    Note: If another hyperlink is set to a cell containing a hyperlink, the new hyperlink will replace the old one rather than merging it.

    The below example code shows that you can perform a custom action after disabling the default behavior of hyperlink.

    C#
    Copy Code
    // Hyperlink to custom action
    fpSpread1.HyperLinkClicked += FpSpread1_HyperLinkClicked;
     private void FpSpread1_HyperLinkClicked(object sender, HyperLinkClickedEventArgs e)
    {
      // Set e.Link to null to skip the default behavior and customize accordingly
      e.Link = null;
    }
    

    VB
    Copy Code
    'Hyperlink to custom action
    fpSpread1.HyperLinkClicked += AddressOf FpSpread1_HyperLinkClicked
    Private Sub FpSpread1_HyperLinkClicked(ByVal sender As Object, ByVal e As HyperLinkClickedEventArgs)
        e.Link = Nothing
    End Sub
    

    The below example code shows how to display the built-in 'Insert Hyperlink' dialog.

    C#
    Copy Code
    // Show HyperlinkForm
    HyperlinkForm form = new HyperlinkForm(fpSpread1.AsWorkbook().ActiveSheet);
    form.ShowDialog();
    

    VB
    Copy Code
    ' Show HyperlinkForm
    Dim form As HyperlinkForm = New HyperlinkForm(fpSpread1.AsWorkbook().ActiveSheet)
    form.ShowDialog()
    

    Edit Hyperlink Dialog

    Create Hyperlinks Automatically

    You can also generate a hyperlink automatically by entering a link like string value and setting the AutoCreateHyperlink property to true.

    C#
    Copy Code
    fpSpread1.Features.AutoCreateHyperlink = true;
     //input link like value in any cell and it will be automatically turn into hyperlink
    

    VB
    Copy Code
    fpSpread1.Features.AutoCreateHyperlink = True
    'Input link like value in any cell and it will be automatically turn into hyperlink
    

    Modify Hyperlinks

    You can edit a hyperlink in a sheet or range by changing the settings in IWorksheet.Hyperlinks[index] or IRange.Hyperlinks[index] object respectively.

    The below example code shows how to modify a hyperlink in Cell B3.

    C#
    Copy Code
    // Edit link
    fpSpread1.AsWorkbook().ActiveSheet.Hyperlinks.Add("B3", "", "Sheet1!A1", "Click here to go to A1", "Goto Cell A1");
    GrapeCity.Spreadsheet.IHyperlink hpl = fpSpread1.AsWorkbook().ActiveSheet.Cells["B3"].Hyperlinks[0];
    hpl.SubAddress = "Sheet1!A2";
    hpl.TextToDisplay = "Goto Cell A2";
    hpl.ScreenTip = "Click here to go to A2";
    

    VB
    Copy Code
     ' Edit link
     fpSpread1.AsWorkbook().ActiveSheet.Hyperlinks.Add("B3", "", "Sheet1!A1", "Click here to go to A1", "Goto Cell A1")
     Dim hpl As GrapeCity.Spreadsheet.IHyperlink = fpSpread1.AsWorkbook().ActiveSheet.Cells("B3").Hyperlinks(0)
     hpl.SubAddress = "Sheet1!A2"
     hpl.TextToDisplay = "Goto Cell A2"
     hpl.ScreenTip = "Click here to go to A2"
    


    Delete Hyperlinks

    You can delete a hyperlink while retaining the cell text by using IWorksheet.Hyperlinks[index].Delete or IRange.Hyperlinks[index].Delete method. All the links in a sheet or range can also be deleted at once by using IWorksheet.Hyperlinks.Delete or IRange.Hyperlinks.Delete method respectively.

    The below example shows how to delete a hyperlink in cell B3.

    C#
    Copy Code
    // Delete link
    fpSpread1.AsWorkbook().ActiveSheet.Hyperlinks.Add("B3", "", "Sheet1!A1", "Click here to go to A1", "Goto Cell A1");
    fpSpread1.AsWorkbook().ActiveSheet.Cells["B3"].Hyperlinks[0].Delete();
    

    VB
    Copy Code
    ' Delete link
    fpSpread1.AsWorkbook().ActiveSheet.Hyperlinks.Add("B3", "", "Sheet1!A1", "Click here to go to A1", "Goto Cell A1")
    fpSpread1.AsWorkbook().ActiveSheet.Cells("B3").Hyperlinks(0).Delete()
    


    Set Hyperlink Style

    The built-in style of hyperlink is added along with it, by default. When you click on a link, it is painted with the text color of "Followed Hyperlink" style. However, you can customize the hyperlink style by changing the settings of "Hyperlink" and "Followed Hyperlink" built-in styles.

    The below example code shows how to change the built-in hyperlink styles.

    C#
    Copy Code
    // Change setting of "Hyperlink" and "Followed Hyperlink" build-in styles
    fpSpread1.AsWorkbook().Styles[GrapeCity.Spreadsheet.BuiltInStyle.Hyperlink].Font.Color = GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Red);
    fpSpread1.AsWorkbook().Styles[GrapeCity.Spreadsheet.BuiltInStyle.FollowedHyperlink].Font.Color = GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Cyan);
    

    VB
    Copy Code
    ' Change setting of "Hyperlink" and "Followed Hyperlink" build-in styles
        fpSpread1.AsWorkbook().Styles(GrapeCity.Spreadsheet.BuiltInStyle.Hyperlink).Font.Color = GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Red)
        fpSpread1.AsWorkbook().Styles(GrapeCity.Spreadsheet.BuiltInStyle.FollowedHyperlink).Font.Color = GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Cyan)
    

    UI Behavior

    The Hyperlink displays following behavior when UI interaction is performed:

    Limitations

    See Also