newbie: Binding data to the LinkLabel controll!

J

Jeff

Hi all!

IDE: VS 2003 (C# standard)
OS : XP Pro

In my program I have an array of the class Details (the Details.cs code is
listed below)

In my program I got a linklabel controll (LinkLabel1) and I want to bind the
Url property of Details to the LinkLabel1 controll!
I want to bind the Url property to LinkLabel1, in such a way that the
LinkLabel1_LinkClicked event can opens a IE, based on the Url in Details.

I have tryed this technique:
LinkLabel1.DataBindings.Add("Links",allDetails, "Url");
But the program crashes with the error message "Cannot bind to property
'Links' because it is read only."

I've read in the help file about LinkLabel, from this I understand that the
Links property of LinkLabel can't hold a collection of links but the
property LinkCollection can...

Can anyone give me a tips about how to bind (if it's possible) the Url
property of the class Details to the LinkLabel controll

My program binds the other properties of Details (strProductName and
imgImage) without any problems

allDetails:
allDetails = new
Details(pi.Details.ProductName,ImageFromUrl(pi.Details.ImageUrlMedium)
,pi.Details.Url);


//The Details class
using System;
using System.Drawing;

namespace myBooks
{
/// <summary>
/// Summary description for Details.
/// </summary>
public class Details
{
private string strProductName;
private Image imgImage;
private string strUrl;

public string Url {
get{
return strUrl;
}
set {
strUrl = value;
}
}

public string ProductName
{
get {
return strProductName;
}
set {
strProductName = value;
}
}

public Image CoverImage
{
get{
return imgImage;
}
set {
imgImage = value;
}
}

public Details(string productName, Image imageUrl, string Url)
{
this.strProductName = productName;
this.imgImage = imageUrl;
this.strUrl = Url;
}
}
}

Regards!

Jeff
 
M

Morten Wennevik

Hi Jeff,

The LinkLabel.Links property is as the error says, readonly. It will return a collection of links that a LinkLabel object holds, but you cannot set the Links property directly. You need to use Add to set a new link.

string temp = "www.google.com";

linkLabel1.Text = temp;

linkLabel1.Links.Clear();
linkLabel1.Links.Add(0, 3, "www.w3c.org");
linkLabel1.Links.Add(3, temp.Length-3, temp);

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("iexplore", (string)e.Link.LinkData);
}

This will send you to www.w3c.org if you click "www" on "www.google.com" otherwise it will send you to google.
 
J

Jeff

So from this I understand it's impossible to bind a datasource to the Link
property!

The only way to "bind" the data is to use linkLabel1.Links.Add method (in a
loop)

correct??



Morten Wennevik said:
Hi Jeff,

The LinkLabel.Links property is as the error says, readonly. It will
return a collection of links that a LinkLabel object holds, but you cannot
set the Links property directly. You need to use Add to set a new link.
string temp = "www.google.com";

linkLabel1.Text = temp;

linkLabel1.Links.Clear();
linkLabel1.Links.Add(0, 3, "www.w3c.org");
linkLabel1.Links.Add(3, temp.Length-3, temp);

private void linkLabel1_LinkClicked(object sender,
LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("iexplore", (string)e.Link.LinkData);
}

This will send you to www.w3c.org if you click "www" on "www.google.com"
otherwise it will send you to google.
 
M

Morten Wennevik

To my knowledge it is impossible to directly bind data to the Link property. You can probably achieve the same result by using the Add method when creating or changing a Details object.

Not sure what you mean by using Add in a loop, but yes, you need to use Add.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top