Link  
Author Message
jrcdude





PostPosted: Visual C++ Express Edition, Link Top

Hi, im new to C++ and I was would like to know how to make a link in it. What do I need to add to the click event Thanks, jrcdude.



Visual Studio Express Editions29  
 
 
ahmedilyas





PostPosted: Visual C++ Express Edition, Link Top

well depends on what the link format is. if you want to launch a site for example:

System.Diagnostics.Process.Start("link")

or you can use the ProcessStartInfo class, gives you more options:

Dim thePS as new System.Diagnostics.ProcessStartInfo("iexplore.exe")

thePS.Arguments = "link"

System.Diagnostics.Process.Start(thePS)

 

typically you would implement the LinkClicked event and then do this:

 

Dim theTarget as string = CType(e.link.LinkData, string)

if theTarget is nothing = false then

   System.Diagnostics.Process.Start(theTarget)

end if

 

does this help is this what you are after



 
 
Jonathan Caves - MSFT





PostPosted: Visual C++ Express Edition, Link Top

I'm not sure what you mean by a "link". Could you explain in more detail what you are attempting to do

 
 
Peter Ritchie





PostPosted: Visual C++ Express Edition, Link Top

Hi Goat Spirit, ahmedilyas' solution will work; unfortunately it suffers from a couple of issues.

One, it only uses IE and ignores the user's default browser setting. And two, it causes your UI to block until the browser is properly loaded--which could take several seconds, leaving your UI unresponsive and unable to paint correctly if you switch to another application and back (or another application pops-up). A pattern I use is to use a BackgroundWorker object to use a background thread to spawn the browser. To make it more complex, Process.Start doesn't support executing the URL as a command (see http://www.hide-link.com/ for the****details) and you have to enlist the help of URL.DLL and it's FileProtocolHandler entry. The following is the pattern:

'...

Me.spawnBrowserBackgroundWorker = New BackgroundWorker

AddHandler Me.spawnBrowserBackgroundWorker.DoWork, New DoWorkEventHandler(AddressOf Me.spawnBrowserBackgroundWorker_DoWork)

'...

Private Sub spawnBrowserBackgroundWorker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)

Dim url As String = TryCast(e.Argument, String)

If (Not url Is Nothing) Then

Dim process As New Process

process.StartInfo.FileName = "rundll32.exe"

process.StartInfo.Arguments = ("url.dll,FileProtocolHandler " & url)

process.StartInfo.UseShellExecute = True

process.Start()

End If

End Sub

Private Sub linkLabel1_LinkClicked(ByVal sender As Object, ByVal e As LinkLabelLinkClickedEventArgs)

Me.spawnBrowserBackgroundWorker.RunWorkerAsync(linkLabel1.Text)

End Sub

It manually creates the BackgroundWorker object and hooks up the DoWork event handler for clarity.

 
 
jrcdude





PostPosted: Visual C++ Express Edition, Link Top

Ok, I want to have a button or link label that when clicked, opens a url in your default browser.
 
 
Ayman Shoukry - MSFT





PostPosted: Visual C++ Express Edition, Link Top

If you are using winforms, you can make use of the HttpResponse.Redirect method. Take a look at http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemwebhttpresponseclassredirecttopic.asp for more details.

Thanks,
Ayman Shoukry
VC++ Team


 
 
ahmedilyas





PostPosted: Visual C++ Express Edition, Link Top

Thanks Peter - you learn new things everyday :-)

 
 
jrcdude





PostPosted: Visual C++ Express Edition, Link Top

I mean like in visual basic, you use the Process.Start("http://www.microsoft.com/") in the click event, i need to know the code like that.
 
 
Goat Spirit





PostPosted: Visual C++ Express Edition, Link Top

I tried this, but got some errors:
AddHandler - Syntax Error.
Me.spawnBrowserBackgroundWorker - Syntax Error.
If (Not url Is Nothing) Then - Variable 'url' has been used before being assigned a value. A null exception could occur at runtime.

Not sure if I declared everything right, try it to see if it works.

 
 
Ayman Shoukry - MSFT





PostPosted: Visual C++ Express Edition, Link Top

You can use the same method in VC++. Take a look at http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemdiagnosticsprocessclassstarttopic.asp

Thanks,
Ayman Shoukry
VC++ Team


 
 
Tall Dude





PostPosted: Visual C++ Express Edition, Link Top

Public Class Form1

Private Sub spawnBrowserBackgroundWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)

Dim url As String = TryCast(e.Argument, String)

If (Not url Is Nothing) Then

Dim process As New Process

process.StartInfo.FileName = "rundll32.exe"

process.StartInfo.Arguments = ("url.dll,FileProtocolHandler " & url)

process.StartInfo.UseShellExecute = True

process.Start()

End If

End Sub

Dim spawnBrowserBackgroundWorker As New System.ComponentModel.BackgroundWorker

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

AddHandler spawnBrowserBackgroundWorker.DoWork, AddressOf Me.spawnBrowserBackgroundWorker_DoWork

End Sub

Private Sub LinkLabel1_LinkClicked_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked

spawnBrowserBackgroundWorker.RunWorkerAsync(LinkLabel1.Text)

End Sub

End Class



 
 
jrcdude





PostPosted: Visual C++ Express Edition, Link Top

It's getting closer but I still cant get it to work right.

Can you take a look at my click event and tell me if you see something wrong   Thanks, jrcdude.

This is just supposed to open Internet Explorer. 

}

#pragma endregion

private: System::Void linkLabel1_LinkClicked(System::Object^ sender, System::Windows::Forms::LinkLabelLinkClickedEventArgs^ e)

{

Process::Start(S"C:\Program Files\Internet Explorer\iexplore.exe");

}

 

 


 
 
nobugz





PostPosted: Visual C++ Express Edition, Link Top

The "S" string prefix only works with the old MC++ syntax. Try this:
System::Diagnostics::Process::Start("C:\\Program Files\\Internet Explorer\\iexplore.exe");



 
 
jrcdude





PostPosted: Visual C++ Express Edition, Link Top

Nope still doesn't work. I click the link and nothing happens. I also tryed it with a link and still didn't work. Any suggestions Thanks, jrcdude.
 
 
nobugz





PostPosted: Visual C++ Express Edition, Link Top

Set a breakpoint on the line of code. Start the debug and click the link. If it breaks, you have some kind of problem with Internet Explorer. If it doesn't, you have some kind of problem with the link event handler.


 
 
Jrcdud3





PostPosted: Visual C++ Express Edition, Link Top

Could you please give me a sample project Just a 1 form application with a link that works I would like to see if I'm doing something wrong. Thanks, jrcdude.
 
 
Jrcdud3





PostPosted: Visual C++ Express Edition, Link Top

Ok, nevermind I finally got it to work. Thanks for all your help.