[]
        
(Showing Draft Content)

Adding Hyperlink in a Cell

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:

  • Access a webpage URL - For example, https://developer.mescius.com/

  • Open mail box with recipient's email address - For example, spread.sales@mescius.com

  • Access a range location in current workbook - For example, Sheet2!A1:B2

  • Perform any custom operation - For example, showing a popup instead of navigating to URL.

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.

// 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");
' 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")

!type=note

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.

// 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;
}
'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.

// Show HyperlinkForm
HyperlinkForm form = new HyperlinkForm(fpSpread1.AsWorkbook().ActiveSheet);
form.ShowDialog();
' Show HyperlinkForm
Dim form As HyperlinkForm = New HyperlinkForm(fpSpread1.AsWorkbook().ActiveSheet)
form.ShowDialog()

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

  • If a link like value starts with www, the hyperlink will consider it as a url

  • If a link like value matches the email link but it doesn't start with mailto:, the hyperlink will consider it as a url

  • If a link like value matches the workbook location type which starts with 'spread://', the hyperlink will set the value as a url

fpSpread1.Features.AutoCreateHyperlink = true;
 //input link like value in any cell and it will be automatically turn into hyperlink
fpSpread1.Features.AutoCreateHyperlink = True
'Input link like value in any cell and it will be automatically turn into hyperlink

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.

// 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";
' 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"

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.

// 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();
' 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()

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.

// 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);
' 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:

  • The painting area of a cell containing hyperlink is used for navigating to the target location (by displaying Hand cursor).

  • The cross cursor is shown for other areas, if all of the below are set:

    • Horizontal alignment is either Left, Right, Center

    • Vertical alignment is either Top, Bottom, Center

    • TextRotation is 0

    • WrapText is False

  • The tooltip is shown when mouse hovers over hyperlink cell text and hides when mouse leaves the hyperlink cell text.

  • To select the cell without navigating to the target location, click into the cell and hold for a moment until the cursor shows as a cross cursor.

  • Hyperlink supports formulas. However, the visited state like actual hyperlink is not supported.

  • Hyperlink supports overflow but the overflowed content cannot be clicked as link.

  • When you type a formatted URL directly in a cell and press ENTER, the cell edit action is finished. The link is automatically created by a separate create link action. Now if you use undo command or CTRL+Z, only the link is removed from the cell, but the text of URL is retained. If the undo command or CTRL+Z is executed again, then the text URL is removed from the cell.

Limitations

  • Hyperlinks can only be copied when RichClipboard is set to true.

  • Hyperlink with DragFill is not supported.

See Also

Adding Hyperlink to a Shape