RichTextBox in VB.NET - vb.net

I want set a line of text link in rich text box of vb.net. Like for example: I want to know you
The word want, I want to set a link word.
Can I do that?

This is how I would do it.
Dim linkLa As New LinkLabel
linkLa.LinkColor = Color.Red
Dim link As LinkLabel.Link = linkLa.Links.Add(0, 13, "http://www.stackoverflow.com")
linkLa.Text = "Stackoverflow"
AddHandler linkLa.LinkClicked, AddressOf Link_Clicked
richTextBox1.Controls.Add(linkLa)
Private Sub Link_Clicked(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("clicked")
End Sub

I have an answer for you. This will allow you to show the link target address as a tooltip. (little pop up bubble.) Other than that, it's similar to Stan R.'s answer.
Put this code under the "add link" button (or whatever you're calling it) in your program
Note: I put comments before each line, so it's easier to follow!
'define the text and link targets
Dim linktext As String = LinkTextbox.Text 'LinkTextbox is just the textbox where the user inputs the text of the link
Dim linktarget As String = LinkTargetTextbox.Text 'LinkTargetTextBox is just the textbox where the user inputs the target URL of the link
'Define the LinkLabel
Dim lnk As New LinkLabel
'if you want, you can set the different properties, like font or linkcolor, programmatically after defining the linklabel, for instance:
lnk.LinkColor = Color.Blue
'set tooltip
lnk.Tooltip = linktarget
'set the link target
Dim lk As LinkLabel.Link = lnk.Links.Add(0, 13, linktarget)
'set the link text
lnk.Text = linktext
'EventHandler
AddHandler lnk.LinkClicked, AddressOf LinkClicked
'Add the control to the richtextbox
RichTextBox1.Controls.Add(lnk)
'This is the Subroutine that the label will run when clicked (Make sure to put your "End Sub" before this, because it's not part of the button's subroutine)
Private Sub LinkClicked(ByVal sender As Object, ByVal e As EventArgs)
'send link to the browser
Process.Start(linktarget)
End Sub

Related

is there any way to simplify this code? vb.net [duplicate]

i have been created buttons and textboxs by coding in next loop,
the result
'T(x).Name = "text_1"
'T(x).Name = "text_2"
'T(x).Name = "text_3"
'....
'B(x).Name = "button_1"
'B(x).Name = "button_2"
'B(x).Name = "button_3"
'...
and i want to get textbox property whene i click the button,
i can get button property when click like button_1.Name.ToString
but i cant get the text_1,2,3 .... property.
i do some trick by split function button_1.Name.ToString and get the last number
and add it to the textbox name like "text_" & button_1.Name.ToString but i can't convert this string to object.
Update
Here's the code I'm using to load the controls in the loop:
C_A_TEXT(x) = New TextBox()
C_A_TEXT(x).Dock = System.Windows.Forms.DockStyle.Fill
C_A_TEXT(x).Location = New System.Drawing.Point(270, 5)
C_A_TEXT(x).Margin = New System.Windows.Forms.Padding(0)
C_A_TEXT(x).Size = New System.Drawing.Size(70, 27)
C_A_TEXT(x).TabIndex = 5
C_A_TEXT(x).Name = "NEW_RECHARGE_COUNT_TEXT_" & x
Update 2
Here's some more code:
AddHandler C_A_BUTTONS(x).Click, AddressOf C_A_BUTTON
Private Sub C_A_BUTTON(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim thisButton As Button = sender Dim A = CType(Me.Controls("NEW_RECHARGE_COUNT_TEXT_1"), TextBox)
MsgBox(A.Text.ToString) 'Error!
End Sub
You can access the controls by name via the Form.Controls property, for instance:
Dim text1 As TextBox = CType(Me.Controls("text_1"), TextBox)
As a quick useful tip to note, you don't seem to have to specify the type of control within the CType statement for purposes of accessing a control on your form. I came across this when trying to access multiple types of form controls, such as buttons and textboxes, all with the same line of code.
CType(Controls("NAME_OF_CONTROL"), Control)
Note that, rather than specifying exactly what type of control, such as 'TextBox' or 'Button', you simply state 'Control'. This allows you to universally change any type of control, without needing to specify its type.
I couldn't find this anywhere else, so I thought I'd share it!
Below is the code.
Dim oObj As Object = Me.Controls.Find("control name", True).FirstOrDefault()
Obj.Property = Value
I hope it helps.
Dim sometext As TextBox = CType(Me.Controls("sometext "), TextBox)
The title of the thread and your description of the problem at hand seem a little different from each other.
To answer your title (to find a control by its name) use the following:
Dim myControlToFind = LayoutRoot.FindName("NAMEOFCONTROL")
More information on this method can be found here .
To answer the description of your issue as (to access a code generated control after it is clicked) do the following:
In the loop where you are creating the control(s) add the following handler
Addhandler YOURCONTROL.Clicked, AddressOf Textbox_Clicked
...and then this will handle the click event
Private Sub Textbox_Clicked(sender as object, e as RoutedEventArgs)
Dim tbClicked = Ctype(sender, TextBox)
'You can now access any of the properties of the textbox, for example
Dim txt as String = tbClicked.Text
Dim name as String = tbClicked.Name
Dim height as Double = tbClicked.Height
End Sub
None of the above worked for me. This does:
Dim selVal As String = CType(Form.FindControl(myListName), DropDownList).SelectedValue

Create a link to a part of a webpage in VB.Net

How would I go about creating a link to a specific part of a webpage in a vb.net program?
I want it to act like the links in the contents page on Wikipedia.
The link will go here:
Private Sub LauncherHelp_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button <> MouseButtons.Right Then Return
Dim cms = New ContextMenuStrip
Dim item1 = cms.Items.Add("Copy Link")
item1.Tag = 1
AddHandler item1.Click, AddressOf menuChoice_LauncherHelp
cms.Show(popuw_Link, e.Location)
End Sub
Private Sub menuChoice_LauncherHelp(ByVal sender As Object, ByVal e As EventArgs)
Dim item = CType(sender, ToolStripMenuItem)
Dim selection = CInt(item.Tag)
If selection = 1 Then
My.Computer.Clipboard.SetText("link") '<== The link goes here.'
MessageBox.Show("Link Copied to Clipboard.")
End If
End Sub
I'm grateful for any help.
Regards
T54
P.S. Please feel free to correct my spelling.
This is not related to VB. It is achieved with HTML anchors.
These are placed at the particular position within the HTML markup an being referenced in the link via hash tag #
For example you put <a href="#chapter1"> in the source of i.e. index.html and open it in the browser via index.html#chapter1
The browser will then scroll to the position of the anchor tag.

Button Array - how to pass a parameter to shared handler

I have a bit of code where i have a dynamically created array or buttons with staff pictures on them, as well as the staff's name. I've added one handler to handle any button click from any of the buttons. where i am stuck is, if you look at the code below, it all works fine, and if you click any of the buttons you get the "aha" test message. but i want the name of the staff clicked on (so btnArray(i).Text) to be passed to the handler for further processing. I tried adding a ByVal parameter to the handler but that caused an error. what's the correct way to do this? As i said, the code below works for me, i just am at a loss as to how to add the extra functionality.
Dim btnArray(staffcount) As System.Windows.Forms.Button
For i As Integer = 1 To staffcount - 1
btnArray(i) = New System.Windows.Forms.Button
btnArray(i).Visible = True
btnArray(i).Width = 80
btnArray(i).Height = 101
btnArray(i).BackgroundImage = Image.FromFile(picloc(i))
btnArray(i).BackgroundImageLayout = ImageLayout.Stretch
btnArray(i).Text = staffname(i)
Dim who As String
who = btnArray(i).Text
AddHandler btnArray(i).Click, AddressOf Me.theButton_Click
btnArray(i).ForeColor = Color.White
btnArray(i).TextAlign = ContentAlignment.BottomCenter
Dim fnt As Font
fnt = btnArray(i).Font
btnArray(i).Font = New Font(fnt.Name, 10, FontStyle.Bold)
FlowLayoutPanel1.Controls.Add(btnArray(i))
Next i
End Sub
Private Sub theButton_Click()
MsgBox("aha")
End Sub
First, correct the signature of your shared handler.
Private Sub theButton_Click(sender As Object, e As EventArgs)
End Sub
Once that is done getting the text of the button clicked is a simple matter.
Private Sub theButton_Click(sender As Object, e As EventArgs)
Dim textOfButtonClicked As String = DirectCast(sender, Button).Text
MessageBox.Show(textOfButtonClicked)
End Sub
The sender is the button that was clicked. Since signatures use objects for the sender the DirectCast 'changes' it to button and you then can access the .Text property of the button.
If there are more manipulations you want to perform on the clicked button you could do it this way
Private Sub theButton_Click(sender As Object, e As EventArgs)
Dim whBtn As Button = DirectCast(sender, Button) ' get reference to button clicked
Dim textOfButtonClicked As String = whBtn.Text
MessageBox.Show(textOfButtonClicked)
'e.g. change the color
whBtn.BackColor = Color.LightYellow
End Sub

Add event to dynamically created button

I have 4 picture boxes on my form. Whenever a new picture is selected the next available box populated and a button is created within that picture box. I would like that button to be able to delete the image within that particular picturebox. I know how to create an event handler and then add the address to the button, what I do not know how to do is how to write the code so as to actually delete the assigned image on the assigned box. Here is my code to load the pictures and create the button:
Private Sub btnAddImage_Click(sender As Object, e As EventArgs) Handles btnAddImage.Click, btnUploadImage.Click 4
Dim btn As Button = New Button
btn.Text = "Remove Image"
'Procedure places the pictures in each empty picturebox in sequence
ofdBrowsePictures.Multiselect = False
ofdBrowsePictures.Title = "Select Image to Upload"
ofdBrowsePictures.Filter = "Image Files |*.jpg*"
If ofdBrowsePictures.ShowDialog() = Windows.Forms.DialogResult.OK Then
'create array of each picture box and check if they are empty
'Check if the picturebox contains a tag with the image path
Dim PBs() As PictureBox = {picMainImage, picImage2, picImage3, picImage4}
Dim nextPB = PBs.Where(Function(x) IsNothing(x.Image)).FirstOrDefault
If Not IsNothing(nextPB) Then
'if the box does not contain a image path, then place the picture on that box
nextPB.ImageLocation = ofdBrowsePictures.FileName
nextPB.Tag = nextPB.ImageLocation.ToString
'add a button
nextPB.Controls.Add(btn)
'Create a border style on the image
nextPB.BorderStyle = BorderStyle.FixedSingle
End If
End If
End Sub
what I do not know how to do is how to write the code so as to actually delete the assigned image on the assigned box
I assume "delete image" means remove from the picture box, not delete from disk.
' what is 4???
Private Sub btnAddImage_Click(sender As Object,
e As EventArgs) Handles btnAddImage.Click, btnUploadImage.Click 4
Dim btn As Button = New Button
btn.Text = "Remove Image"
' bla bla bla set the imagelocation
AddHandler btn.Click, AddressOf RemoveImage_Click
pb1.Controls.Add(btn) ' btn.parent = this pb
End Sub
Private Sub RemoveImage_Click(sender As Object, e As EventArgs) Handles Button9.Click
Dim btn As Button = CType(sender, Button)
' clear image
CType(btn.Parent, PictureBox).ImageLocation = ""
RemoveHandler btn.Click, AddressOf RemoveImage_Click
' remove the control
pb1.Controls.Remove(btn)
' if you remove a control, dispose of it
btn.Dispose()
End Sub
I am not sure I would invoke the dialog before checking for the next PB (nor would I create the new Button), since you seem to want to do nothing if there is no nextPB to play with.
For more information about Disposing of controls you Remove, see:
Memory Leak
What are Parking Windows
Adding / removing controls
Basically, forms dispose of controls when you close them. If you Remove a control, the form no longer has a reference and cannot. As a result, you should be disposing them.
Either of these clear a picturebox image set with ImageLocation:
PictureBox1.Image = Nothing
PictureBox1.ImageLocation = ""
PictureBox1.ImageLocation = String.Empty

How to change only one tooltip?

I am populating a FlowLayout with Pictureboxes. As I populate i give each of them a tooltip. I have a seperate function to change the pictures how can I change the tooltip as well?
dim laytt as tooltip = new tooltip
For i = 1 To count
Dim newPic As PictureBox = New PictureBox()
newPic.Image = p.Image
newPic.Size = p.Size
newPic.SizeMode = p.SizeMode
laytt.SetToolTip(newPic, ttstring)
AddHandler newPic.Click, AddressOf LayoutComponent_Clicked
sys.Add(a_component)
LayoutFlowLayout.Controls.Add(newPic)
Next
later i have a function to change the pics in it I want to be able to change the tool tip
Private Sub LayoutComponent_Clicked(ByVal sender As Object, ByVal e As EventArgs)
Dim i As Integer = LayoutFlowLayout.Controls.IndexOf(sender)
If deleteModeOn Then
sys.components.RemoveAt(i)
LayoutFlowLayout.Controls.RemoveAt(i)
Exit Sub
End If
'get index in sys from layout?
If (sys.components.Item(i).GetType() = GetType(Transpositor)) Then
Form2.ShowDialog(Me)
sys.components.Item(i).divert = tempTranspositorDivert
'here I want to do something like this
laytt.RemoveAt(i) <--- THIS DOESN'T EXIST
End If
End Sub
TL;DR I want to remove/change only one tooltip text at a specific index
Since the sender parameter is the picture box control that was clicked, you can use that variable to specify which control you want to alter. For instance, this will remove the tool tip:
laytt.SetToolTip(sender, Nothing)
This will change it:
laytt.SetToolTip(sender, "new value")