| Author |
Message |
jrcdude

|
Posted: 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 Editions30
|
| |
|
| |
 |
ahmedilyas

|
Posted: 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

|
Posted: 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
|
| |
|
| |
 |
jrcdude

|
Posted: 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.
|
| |
|
| |
 |
Peter Ritchie

|
Posted: 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://msdn.microsoft.com/msdnmag/issues/05/03/CATWork/ for the gory 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.
|
| |
|
| |
 |
Ayman Shoukry - MSFT

|
Posted: Visual C++ Express Edition, Link |
Top |
|
| |
 |
ahmedilyas

|
Posted: Visual C++ Express Edition, Link |
Top |
Thanks Peter - you learn new things everyday :-)
|
| |
|
| |
 |
Goat Spirit

|
Posted: 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.
|
| |
|
| |
 |
jrcdude

|
Posted: 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.
|
| |
|
| |
 |
Ayman Shoukry - MSFT

|
Posted: Visual C++ Express Edition, Link |
Top |
|
| |
 |
Tall Dude

|
Posted: 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

|
Posted: 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

|
Posted: 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

|
Posted: 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

|
Posted: 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

|
Posted: 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

|
Posted: Visual C++ Express Edition, Link |
Top |
Ok, nevermind I finally got it to work. Thanks for all your help.
|
| |
|
| |
 |
| |