How to make paragraphed text appear in a multi-line textbox when a treenode is selected - vb.net

I reckon this is probably a simple thing to do but I can't seem to know how to pull it off as I am new to VB.Net.
On my form, I have a treeview control I have populated with nodes using the Nodes Collection TreeNode Editor. I also have a multi-line textbox. When a node is selected, some pre-defined text assigned to that particular node appears in my textbox.
What I want to achieve is for paragraphs of text with formatting, to be inputted into the textbox and not just single lines of text as shown in my code below.
For instance, when the user clicks a node titled 'Soccer', I would like a formatted body of text like:
“This is a sport.
It is a sport played between two teams of eleven players with a spherical ball.
It is played by 250 million players in over 200 countries.”
to appear in my textbox. Please how can I get the code to do this? I am using Visual Basic 2010 Express. The code I am using at the moment is shown below. Thank you in advance.
Private Sub TreeView1_AfterSelect(sender As System.Object, e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
Dim SelectedNode As TreeNode
SelectedNode = TreeView1.SelectedNode
If SelectedNode.Text = "Soccer" Then
TextBox1.Text = "This is a sport." 'I would like to have paragraphed text appear in 'textbox1 instead of a single line of text
Else
If SelectedNode.Text = "Moon" Then
TextBox1.Text = "This is the name of a car." 'I would like to have paragraphed text 'appear in textbox1 instead of a single 'line of text
End If
End If
End Sub

You should add '\n' in end of your line.

Related

Is there a way to retrieve some text from a webpage to a textbox in VB?

I'm trying to make it so I can have several text boxes in my form show pieces of information from a specific webpage. For example, would there be a way I would be able to retrieve the title of this question to a variable with the click of a button in Visual Basic?
It not hard but you have to look at the source page, and identify the elements.
In nicely formed pages, usually the div elements have a tag ID, but often they don't, so you have to grab say by a attribute name - often you can use the class name of the div in question.
So, to grab the Title, and you question text of this post?
This works:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim xDoc As New Xml.XmlDocument
Dim strURL As String = "https://stackoverflow.com/questions/55753982"
Dim xWeb As New WebBrowser
xWeb.ScriptErrorsSuppressed = True
xWeb.Navigate(strURL)
Do Until xWeb.ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
Loop
Dim HDoc As HtmlDocument = xWeb.Document
Debug.Print(HDoc.GetElementById("question-header").FirstChild.InnerText)
Debug.Print(FindClass(HDoc, "post-text"))
End Sub
Function FindClass(Hdoc As HtmlDocument, strClass As String) As String
' get all Divs, and search by class name
Dim OneElement As HtmlElement
For Each OneElement In Hdoc.GetElementsByTagName("div")
If OneElement.GetAttribute("classname") = strClass Then
Return OneElement.InnerText
End If
Next
' we get here, not found, so return a empty stirng
Return "not found"
End Function
OutPut:
(first part is title question)
Is there a way to retrieve some text from a webpage to a textbox in VB?
(second part is question text)
I'm trying to make it so I can have several text boxes in my form show pieces of
information from a specific webpage. For example, would there be a way I would be
able to retrieve the title of this question to a variable with the click of a button
in Visual Basic?

search controls in flowlayoutpanel with "like" searchkey VB.NET

I have a flowlayoutpanel listing the titles of an album. Assuming the flowlayoutpanel has a lot of songs and I want to find a particular one. Let's say, I would like to get the song of Ed Sheeran "Thinking Out loud." So I would type the word "Thinking" in the searchbox, and without even finishing the "Out Loud" words, I would like to filter the flowlayoutpanel showing me the control that has the title "Thinking Out Loud" and hiding all the controls without the word "Thinking." It's like an SQL search LIKE query. But I don't want to do SQL. is it possible with FLOWLAYOUTPANEL and a SEARCH TEXTBOX?
You can use the search Textbox's TextChanged event to trigger a processing of the FlowLayoutPanel's controls collection setting each control's Visible property based on the result of a VB Like Operator comparison.
Private Sub tbSearch_TextChanged(sender As Object, e As EventArgs) Handles tbSearch.TextChanged
If tbSearch.Text.Length > 0 Then
Dim compareTo As String = String.Concat("*", tbSearch.Text.ToLowerInvariant, "*")
For Each c As Control In FlowLayoutPanel1.Controls
c.Visible = (c.Text.ToLowerInvariant Like compareTo)
Next
Else
For Each c As Control In FlowLayoutPanel1.Controls
c.Visible = True
Next
End If
End Sub

VB.NET Listbox item color [duplicate]

So I'm trying to make a listbox with 2 buttons.
Listbox is supposed to display files from a specific folder (I get back to this)
And the two buttons are supposed to be called "Set.." (As in set directory)
and Update (As the list will be refreshed (Something I do every time the Windows Form Runs.
So as of now, when I start my application, and go to the form with the listbox, the listbox is empty. When pressing "Update", the List box picks up files from an address located on my Harddrive (So this is a static address located in my code).
It also finds 7 different extensions (Filetypes), and lists all of them correctly.
My problem is as follows, I want the set Button to open a File Dialog for the user on First-Time Runtime, so the user himself can choose what folder the program "Indexes or Searches" if you will. And then when he runs the application again, and finds the listbox, he can only press Update, and the listbox shows the content of the folder he choose last time.
The Set - button doesn't do anything in my code right now.
Second up, I want each filetype to be labeled or colored with a specific color.
Like; .txt should be blue, .jpg is red, ect..
Running Visual Studio 2013 if that helps.
Also, when checking my code, if you have any suggestions too, how I can improve the code, make it easier, shorter, and just to change things to avoid duplicate codes, please let me know.
Here is from the Design in VS2013
Code:
Private Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
FolderBrowserDialog1.SelectedPath = "xxx\xxx\xxx\xxx"
System.IO.Directory.GetCurrentDirectory()
Private Sub updateButtonGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updateButtonGame.Click
If FolderBrowserDialog1.SelectedPath = "xxx\xxx\xxx\xxx" Then
ListFiles(FolderBrowserDialog1.SelectedPath)
End If
End Sub
Private Sub ListFiles(ByVal folderPath As String)
filesListBox.Items.Clear()
Dim fi = From f In New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath).GetFiles().Cast(Of IO.FileInfo)() _
Where f.Extension = ".z64" OrElse f.Extension = ".nds" OrElse f.Extension = ".BIN" OrElse f.Extension = ".smc" OrElse f.Extension = ".ISO" OrElse f.Extension = ".nes" OrElse f.Extension = ".gb"
Order By f.Extension
Select f
For Each fileInfo As System.IO.FileInfo In fi
filesListBox.Items.Add(fileInfo.Name)
Next
End Sub
Another thing, this is more optional..
My list is completely black, so I choose to have the "Items" in the Listbox turn Light gray.
I played around with something called e.Graphics in hope to achieve Coloring a specific filetype, and it turned ALL items either Black, Red, or whatever I put it to.
But after removing the code, all Items turns into the same color as the Background color of the Listbox. So I can no longer see the elements actually being there, other than the Scroll-bar popping up on the side (Since its many Items in the folder I picked)
Also, I am not that good with coding/visual studio yet, as I started around 1 week ago to date.
Started with VB 2010 and then went to VS2013 to see if I managed to fix some issues, also related to List Box.
If I explained rather poorly, let me know and I'll update with better info.
Project was also first created in VB 2010, and then "Migrated" or opened in VS 2013.
A much, much better way to do this is with a ListView and an ImageList with some standard images for Text, Image, PDF etc in the list, then just set the image key for each item when you add them to the list.
Alternatively, you could emulate the same thing in a listbox (using OwnerDrawFixed) to draw a specified image to indicate the file type. A really good way to implement this is as an ExtenderProvider using code similar to that below as a starting point. As an EP, you can link any cbo or listbox to an image list to provide image cues very much like the ListView works:
The reason you do not see your colored item idiom very often is that whatever colors you pick will not look right on all systems. The more colors, the more likely and more often they wont have enough contrast, be readable etc with the user's color scheme. You also dont need a "Legend" to explain what the colors mean - an image is self explanatory. That said, the DrawItem code would be something like this:
NB: Listbox control is set to OwnerDrawFixed, ItemHeight = 16
Private Sub lb_DrawItem(sender As Object,
e As DrawItemEventArgs) Handles lb.DrawItem
Dim TXT As Color = Color.Black
Dim JPG As Color = Color.Green
Dim PDF As Color = Color.Blue
Dim EXE As Color = Color.Gray
Dim SEL As Color = SystemColors.HighlightText
Dim thisColor As Color = Color.Orange
Dim ndx As Integer = e.Index
' isolate ext ans text to draw
Dim text As String = lb.Items(ndx).ToString()
Dim ext As String = System.IO.Path.GetExtension(text).ToLowerInvariant
' dont do anything if no item being drawn
If ndx = -1 Then Exit Sub
' default
e.DrawBackground()
' color selector
Select Case ext
Case ".jpg"
thisColor = JPG
Case ".txt"
thisColor = TXT
Case ".exe"
thisColor = EXE
Case ".pdf"
thisColor = PDF
End Select
' override color to use default when selected
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
thisColor = SEL
End If
' render the text
TextRenderer.DrawText(e.Graphics, text, lb.Font, e.Bounds,
thisColor, TextFormatFlags.Left)
' default
e.DrawFocusRectangle()
End Sub
Result:
Works on My SystemTM

How to remove textboxes on button click (vb.net)?

I am not knowing about how to delete textboxes on the click of a button in my Windows form.
Here is my story:
Initially, I wanted to add textboxes on the click of a button, and upon searching the net, I was able to find out, on this forum, how to do this. https://stackoverflow.com/questions/15461978/adding-new-textbox-with-button-click
I used the code that user "Rajaprabhu Aravindasam" (2nd answer) gave. Here is only part of my code that I used (in order not to confuse you):
Private Sub Button_AddTask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_AddTask.Click
count += 1
TabPage_TaskStructure.Controls.Add(New Label() With _
{.Name = "Label_Task" & count})
TabPage_TaskStructure.Controls.Add(New RichTextBox() With _
{.Name = "RichTextBox" & count})
End Sub
Now, as you can see, the purpose of the button 'Button_AddTask" ('+' button on my form) is to create rich textboxes and their respective label. Assume that the rich textboxes and the labels are being created below one another.
Beside the '+' button, there is a '-' button. What I want is to use this '-' button to delete all created textboxes sequentially up. That is, if I have created 4 textboxes with the button '+', textbox no.4 will be deleted first when I click the '-' button, then no. 3 after a second click, then no.2 after a third click and so on.
The sequential part is not a problem, I know perfectly how to do it. Here is part of the code I tried:
Private Sub Button_DeleteTask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_DeleteTask.Click
TabPage_TaskStructure.Controls.Remove(New Label() With _
{.Name = "Label_Task" & count})
TabPage_TaskStructure.Controls.Remove(New RichTextBox() With _
{.Name = "RichTextBox" & count})
count -= 1
If count = 1 Then
Button_DeleteTask.Visible = False
End If
End Sub
What I did, logically, was simply replace 'Add' with 'Remove', but I am not knowing with what to replace 'New'. And so my question is: What needs to be used instead of 'New'? If I need to use an entirely different code, please do tell me.
Any help is greatly appreciated.
Update:
Ok, I have done some research since I've asked this question and I've been able to deduce that the removal of the controls (Label & Rich Text Box) can be done by using a For Each loop. In my code for the '-' button, I have replaced the first 4 lines of code with this:
Dim Ctrl As Control
For Each Ctrl In TabPage_TaskStructure.Controls
If TypeOf Ctrl Is Label And Ctrl.Name = "Label_Task" & count Then
TabPage_TaskStructure.Controls.Remove(Ctrl)
ElseIf TypeOf Ctrl Is RichTextBox And Ctrl.Name = "RichTextBox" & count Then
TabPage_TaskStructure.Controls.Remove(Ctrl)
End If
Next
And so, when I click the '-' button, the program checks whether each control on the tab page (TabPage_TaskStructure) is a label with the name ("Label_Task" & count') or a rich text box with the name ("RichTextBox" & count), and if they are, they will be removed. The rest of the code is the same.
It's working, however not completely. It's working for the labels as they are successfully being removed, but not for the rich text boxes. I cannot understand why. I have tried the code for other controls such as date time pickers and text boxes, and it's not working for any of these either. It seems to be working for labels only.
Also, I have tried using the code on a default rich text box, and it worked! But for rich text boxes being created at run time, it's not working.
Can anyone clarify me on this?
I'm not really good at vb but from simple logic if and when an "IF" argument is accepted the code will move to "End If" and skip "Else" altoghter.
Try this instead:
Dim Ctrl As Control
For Each Ctrl In TabPage_TaskStructure.Controls
If TypeOf Ctrl Is Label And Ctrl.Name = "Label_Task" & count Then
TabPage_TaskStructure.Controls.Remove(Ctrl)
End If
If TypeOf Ctrl Is RichTextBox And Ctrl.Name = "RichTextBox" & count Then
TabPage_TaskStructure.Controls.Remove(Ctrl)
End If
Next

Visual Basic 2008 Mobile Project: How to Filter Data From Combo Box to DataGrid

I'm working on a mobile project, and I have this:
A search textbox (fillby method)
A combobox (bound to the data)
A datagrid
I am able to do this:
input a search query into the textbox using the fillby method and the datagrid shows the appropriate rows.
I need help with this:
To filter the same data with a combobox. If I use the Add Query method (fillby method) to a combobox it creates another textbox search query. I don't want that. I want to be able to filter the datagrid by the combobox.
Here is my code for the ComboBox Sub:
Private Sub CityComboBox_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CityComboBox.SelectedValueChanged
Dim RestIDShort As Short 'primary key
Dim RestDataRow As DataRow 'complete data row of selected value
Dim RestDataRows As DataRow() 'holding the data
Try
'get the restID for the selected city
RestIDShort = Convert.ToInt16(CityComboBox.SelectedValue)
'find the row from the table for the selected city
RestDataRow = RestaurantEateriesDataSet.RestaurantTable.FindByRestID(RestIDShort)
'Grab the variables here. Don't really need them. Just to see if I can pull data.
'NameStringShow = RestDataRow("Name")
'FoodTypeStringShow = RestDataRow("FoodCat")
'CityStringShow = RestDataRow("City")
'test to see if we can write to screen
'successfully wrote this to the screen onload but not on combobox change
'TextBox1.Text = NameStringShow
'retrieve the array for the selected data row
'not sure if this is how to call when there is only one table????
RestDataRows = RestDataRow.GetChildRows("RestaurantTable")
'fill the datagrid with the array of selected value rows
'I don't know how to do this part:
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
I do have a query created that I can call (if needed). The query works when I call it in a textbox, so if there is a way to call it in a combo box, and then display the selected fields in the datagrid . . . all would be good.
Any help, much appreciated.
Private Sub bttnFilter Click(...) Handles bttnFilter.Click
Dim filter As String
filter = InputBox("Enter product name, or part of it")
ProductsBindingSource.Filter = "ProductName LIKE '%" & filter.Trim & "%'"
End Sub
the same is applied to combobox, use combobox1.selectedItem() in place of the filter.
its the basics of searching, it you have any other question you're welcome.