Visual Basic - RichTextBox with multiple links - vb.net

I'm sure I will feel stupid when I get an answer, but it's been a while since I programmed.
I am looking at having a form with a rich text box that will have multiple lines of text and among that text it will have two or more groups of words highlighted that will be links that can be clicked (not URLs). I got it working with one link. But when I try to extend it to two link the second link does not show.
This is the Single Link version and works fine, When I click the link, I get on the console the text I clicked on.
Public Class Form1
Dim linktarget As String = ""
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim lnk1 As New LinkLabel
CreateLink(lnk1, "This is the Target", "Description of Link")
txtBox.Controls.Add(lnk1)
End Sub
Public Sub CreateLink(lnk As LinkLabel, linkTarget As String, linkText As String)
lnk.LinkColor = Color.Blue
Dim lk As LinkLabel.Link = lnk.Links.Add(0, linkText.Length, linkTarget)
lnk.Text = linkText
AddHandler lnk.LinkClicked, AddressOf LinkClicked
End Sub
Private Sub LinkClicked(ByVal sender As Object, ByVal e As EventArgs)
'send link to the browser
Console.WriteLine(sender.Text)
End Sub
End Class
If I add the second link as follows There is no change I only get one link
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim lnk1 As New LinkLabel
CreateLink(lnk1, "This is the Target", "Description of Link")
txtBox.Controls.Add(lnk1)
Dim lnk2 As New LinkLabel
CreateLink(lnk2, "Another Target", "Description of 2nd Link")
txtBox.Controls.Add(lnk2)
End Sub

Your link controls are stacked on each other : specify location, and incidentally set autosize true to avoid a truncated display.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lnk1 As New LinkLabel
CreateLink(lnk1, "This is the Target", "Description of Link")
lnk1.Location = New Point(10, 10)
lnk1.AutoSize = True
TxtBox.Controls.Add(lnk1)
Dim lnk2 As New LinkLabel
CreateLink(lnk2, "Another Target", "Description of 2nd Link")
lnk2.Location = New Point(10, 30)
lnk2.AutoSize = True
TxtBox.Controls.Add(lnk2)
end sub

Related

VB.net difficulty in using RedirectStandardInput for my program

I am trying to make a simple chat program in vb using netcat. Here is my code
Public Class Form1
Dim p As New Process
Dim pstrt As New System.Diagnostics.ProcessStartInfo
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Go()
End Sub
Sub Go()
pstrt.FileName = "cmd.exe"
pstrt.Arguments = "/c nc -l -p1234"
pstrt.UseShellExecute = False
pstrt.RedirectStandardOutput = True
pstrt.RedirectStandardInput = True
AddHandler p.OutputDataReceived, AddressOf yo
p.StartInfo = pstrt
p.Start()
p.BeginOutputReadLine()
End Sub
Sub yo(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
UpdateTextBox(e.Data)
End Sub
Private Delegate Sub UpdateTextBoxDelegate(ByVal Text As String)
Private Sub UpdateTextBox(ByVal tot As String)
If Me.InvokeRequired Then
Dim delgt As New UpdateTextBoxDelegate(AddressOf UpdateTextBox)
Dim args As Object() = {tot}
Me.Invoke(delgt, args)
Else
RichTextBox1.Text &= tot & Environment.NewLine
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
'Shell(TextBox1.Text)
'Console.WriteLine(TextBox1.Text)
p.StandardInput.WriteLine(TextBox1.Text)
End Sub
End Class
The form has a richtextbox, a textbox and two buttons.
The problem is that:
when using redirectstandardinput, the received text is not displayed in richtextbox but the sent text can be viewed by the receiver.
when I don't redirectstandardinput then the received text is displayed in the richtextbox but the sent text cannot be viewed by the receiver.
I have also tried to use the commented code (for button2 click code) to use to send text when not using redirectstandardinput.
For me, your code is working correctly without changes, with
pstrt.RedirectStandardOutput = True
pstrt.RedirectStandardInput = True
See the screenshot:
I guess your problem can be with netcat.
Try with simple commands first like seen on the sample.
Try on another machine. I tested on Windows 8.0, .NET 4.0.

Create list box in runtime and change its color via menu in runtime

I need to write this small program in visual basic, but i face a problem which is i can't change the color or add list form text file during runtime.
this is picture of what i wont to do
http://i62.tinypic.com/30tghh0.png
And this is the code i wrote so far,,
Public Class Form1
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
Dim listBox1 As New System.Windows.Forms.ListBox
listBox1.Text = "New List"
listBox1.Location = New Point(25, 25)
listBox1.Size = New Size(380, 280)
Me.Controls.Add(listBox1)
OpenToolStripMenuItem.Enabled = True
SaveToolStripMenuItem.Enabled = True
SaveAsToolStripMenuItem.Enabled = True
CloseToolStripMenuItem.Enabled = True
EditToolStripMenuItem.Enabled = True
End Sub
Private Sub ExirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExirToolStripMenuItem.Click
End
End Sub
Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
If (OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
Dim myfile As String = OpenFileDialog1.FileName
Dim allLines As String() = File.ReadAllLines(myfile)
For Each line As String In allLines
ListBox1.Items.Add(line)
Next
End If
End Sub
End Class
The problem as you see is in OpenToolStripMenuItem sub with line ListBox1.Items.Add(line)
is there is no listbox1 because is not created yet.the same with color, save and the rest.
so, please help me to solve it.
Move the declaration of ListBox1 out to the Class level:
Public Class Form1
Private ListBox1 As System.Windows.Forms.ListBox
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
ListBox1 = New System.Windows.Forms.ListBox
...
End Sub
...
End Class
*But why create it at run-time like this? You could add it at design-time and set the Visible() property to False. When the New button is clicked, you change Visible() to True. Unless you need a new ListBox to be created each time so you have more than one? If so, how will they be laid out?...

danamically creating controlltabs that can be used

I'm trying to dynamically create tabcontrols which works fine; the tabs are being created however. Once created I would also like them to become clickable and execute other code, this now posing a problem.
The code I’m using to create a tab is as follows
' do whatever wtih filename
Dim myTabPage As New TabPage()
myTabPage.Text = TextBox4.Text
TabControl1.TabPages.Add(myTabPage)
TabPage1.Hide()
Not so nice cause I can now fill my form with as many tabs as I like however none of them can be clicked to execute futher code???
EDIT:
Private Sub TabControl_SelectedIndexchaged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
Dim TabName As String
TabName = TabControl1.SelectedTab.Name
If TabName.Contains("TabPage") Then
' Do something
MsgBox("new tab created")
End If
End Sub
You have to add an event handler for the TabPage click event:
Dim myTabPage As New TabPage
myTabPage.Text = TextBox4.Text
AddHandler myTabPage.Click, AddressOf TabPage1_Click
TabControl1.TabPages.Add(myTabPage)
Which will call this code:
Private Sub TabPage1_Click(sender As Object, e As EventArgs)
MessageBox.Show(DirectCast(sender, TabPage).Text)
End Sub
Per your edit, you would have to add the name property:
myTabPage.Name = TextBox4.Text
And your SelectedIndexChanged event:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) _
Handles TabControl1.SelectedIndexChanged
If TabControl1.SelectedTab IsNot Nothing Then
MessageBox.Show(TabControl1.SelectedTab.Name)
End If
End Sub

VB.NET ~ How can I encapsulate my code to be used over and over?

I know I seem to ask the same types of questions but with much trial and error I finally got my app to do what I needed it to do. I have a form with a button on it. When clicked it creates a form, a web browser, a picture box, a text box, and a button.
The web browser, picture box, textbox, and the button are all placed onto this form at run time.
My question is now how can I encapsulate this to be used over and over again. I've tried just putting this into a class and creating an instance of the class but I keept getting an error on my document completing stating my code isn't referring to an object.
But here is my code.
Thanks
Public Class CaptchaWindow
Dim myform As New Form
Dim webbrowser As New WebBrowser
Dim picturebox As New PictureBox
Dim textbox As New TextBox
Dim submitbtn As New Button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ProfileMaker.Show()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Call myspecs()
AddHandler submitbtn.Click, AddressOf captchasubmit
AddHandler webbrowser.DocumentCompleted, AddressOf webcompleteddesignfloat
webbrowser.Navigate("http://www.designfloat.com/register/")
End Sub
Sub myspecs()
'FORM SIZE AND LOCATION
myform.Size = New Drawing.Size(1542, 771)
myform.Location = New Drawing.Point(15, 15)
'WEBBROWSER SIZE AND LOCATION
webbrowser.Size = New Drawing.Size(1000, 577)
webbrowser.Location = New Drawing.Point(12, 181)
webbrowser.ScriptErrorsSuppressed = True
'PICTUREBOX SIZE AND LOCATION
picturebox.Size = New Drawing.Size(299, 83)
picturebox.Location = New Drawing.Point(12, 12)
picturebox.BorderStyle = BorderStyle.Fixed3D
'TEXBOX SIZE AND LOCATION
textbox.Size = New Drawing.Size(299, 20)
textbox.Location = New Drawing.Point(12, 101)
'BUTTON SIZE AND LOCATION
submitbtn.Size = New Drawing.Size(75, 23)
submitbtn.Location = New Drawing.Point(118, 127)
submitbtn.Text = "Submit"
myform.Controls.Add(webbrowser)
myform.Controls.Add(picturebox)
myform.Controls.Add(textbox)
myform.Controls.Add(submitbtn)
myform.Show()
End Sub
Sub webcompleteddesignfloat()
For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("input")
If element.GetAttribute("name") = "reg_username" Then
element.SetAttribute("value", ProfileMaker.Username.Text)
End If
Next
For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("input")
If element.GetAttribute("name") = "reg_email" Then
element.SetAttribute("value", ProfileMaker.Email.Text)
End If
Next
For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("input")
If element.GetAttribute("name") = "reg_password" Then
element.SetAttribute("value", ProfileMaker.Password.Text)
End If
Next
For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("input")
If element.GetAttribute("name") = "reg_password2" Then
element.SetAttribute("value", ProfileMaker.Password.Text)
End If
Next
If webbrowser.ReadyState = WebBrowserReadyState.Complete Then
For Each Captcha As HtmlElement In webbrowser.Document.Images
If Captcha.GetAttribute("src").Contains("http://www.google.com/recaptcha/api/image? c=") Then
picturebox.Load(Captcha.GetAttribute("src"))
End If
Next
End If
End Sub
Sub captchasubmit()
If webbrowser.ReadyState = WebBrowserReadyState.Complete Then
For Each Captcha As HtmlElement In webbrowser.Document.Images
If Captcha.GetAttribute("src").Contains("http://www.google.com/recaptcha/api/image?c=") Then
picturebox.Load(Captcha.GetAttribute("src"))
End If
Next
End If
For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("input")
If element.GetAttribute("id") = "recaptcha_response_field" Then
element.SetAttribute("value", textbox.Text)
End If
Next
For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("button")
If element.GetAttribute("name") = "submit" Then
element.InvokeMember("click")
End If
Next
myform.Dispose()
End Sub
The below part is when I tried to create an instance of this from a class.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim mystuff As New CaptchaClass
mystuff.Handler()
mystuff.captchasubmit()
mystuff.webcompleteddesignfloat()
End Sub
End Class
You should Imports your class namespace to the current scope (page) at the beggining of the code
Imports Namespace.CaptchaWindow
Class Page1
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim mystuff As New CaptchaClass
End Sub
End Class
Also look at Namespaces if you doesn't know what it is.

Showing Textbox ToolTip

I am trying to work in the tooltip in vb.net. What I am trying to do is whatever I write the text in the textbox control show it in the tooltip. I am able to show text in tooltip but my question is when I edit input text it will show old and new text in the popup tooltip. Here is what I have done so far.
Public Class Form1
Dim s As String = ""
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
s = TextBox1.Text
Dim tooltip1 As System.Windows.Forms.ToolTip = New System.Windows.Forms.ToolTip()
tooltip1.SetToolTip(Button1, s)
End Sub
End Class
Thank you.
It's hard to figure out why this is useful, but try using the TextChanged event of the textbox to update the tooltip:
Private _ToolTip As New ToolTip()
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) _
Handles TextBox1.TextChanged
_ToolTip.Show(TextBox1.Text, TextBox1)
End Sub