hide folder name in tree list - vb.net

how do i hide specific name folder within the treelist ? i have tree list like this
and i want to hide folder name New folder and pdf. i make this from Load Data Dir. i have tweak a bit in the code like this by putting if
Dim rootfolder As String = "C:\\FFOutput"
Private Sub TreeList1_VirtualTreeGetCellValue(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.VirtualTreeGetCellValueInfo) Handles TreeList1.VirtualTreeGetCellValue
Dim di As New DirectoryInfo(CStr(e.Node))
If di.Name <> "New folder" Then
If e.Column Is TreeListColumn1 Then
e.CellData = di.Name
End If
If e.Column Is TreeListColumn2 Then
e.CellData = e.Node.ToString
End If
If e.Column Is TreeListColumn3 Then
If IsFile(di) Then
e.CellData = New FileInfo(CStr(e.Node)).Extension
Else
e.CellData = Nothing
End If
End If
End If
End Sub
Private Sub TreeList1_VirtualTreeGetChildNodes(ByVal sender As Object,
ByVal e As DevExpress.XtraTreeList.VirtualTreeGetChildNodesInfo) _
Handles TreeList1.VirtualTreeGetChildNodes
If loadDrives = False Then
Dim root As String() = Directory.GetDirectories(rootfolder)
e.Children = root
loadDrives = True
Else
Try
Dim di As New DirectoryInfo(CStr(e.Node))
If di.Name <> "New folder" Then
Dim path As String = CStr(e.Node)
If Directory.Exists(path) Then
Dim dirs As String() = Directory.GetDirectories(path)
Dim files As String() = Directory.GetFiles(path)
Dim arr(dirs.Length + files.Length) As String
dirs.CopyTo(arr, 0)
files.CopyTo(arr, dirs.Length)
e.Children = arr
Else
e.Children = New Object() {}
End If
End If
Catch
End Try
End If
End Sub
and it become like this

Related

Find REGEX in every file in a folder

I need to evaluate each file in a folder for the ICN string. Then add each ICN to an output file. I found the code below and have made changes to it to meet my needs but it only adds one found file in the ICN.log instead of looping through all files.
Private Sub btnFindICN_Click(sender As Object, e As EventArgs) Handles btnFindICN.Click
Dim Regex = New Regex("[<][!]ENTITY (ICN.*?)[.]\w+")
Dim output = New List(Of String)
Dim tLoc = txtFolderPath.Text
Dim txtFiles = Directory.EnumerateFiles(tLoc, "*.xml", SearchOption.AllDirectories)
For Each tFile In txtFiles
Dim input = File.ReadAllText(tFile)
If Regex.IsMatch(input) Then
Console.Write("REGEX found in " + tFile)
output.Add(tFile)
Exit For
End If
Next
File.WriteAllLines(tLoc.TrimEnd("\"c) & "\ICN.log", output)
End Sub
After I removed the for exit for the code works.
Private Sub btnFindICN_Click(sender As Object, e As EventArgs) Handles btnFindICN.Click
Dim Regex = New Regex("[<][!]ENTITY (ICN.*?)[.]\w+")
Dim output = New List(Of String)
Dim tLoc = txtFolderPath.Text
Dim txtFiles = Directory.EnumerateFiles(tLoc, "*.xml", SearchOption.AllDirectories)
For Each tFile In txtFiles
'MsgBox(tFile)
Dim input = File.ReadAllText(tFile)
If Regex.IsMatch(input) Then
Console.Write("REGEX found in " + tFile)
output.Add(tFile)
'Exit For
End If
Next
File.WriteAllLines(tLoc.TrimEnd("\"c) & "\ICN.log", output)
MsgBox("Function Complete")
End Sub

Remove duplicate characters from strings and find the shortest

I can not figure out how to select from the result or the shortest line itself or its number
(Yes, the solution is needed in such ancient operators)
Imports System.IO
Public Class Form1
Sub readputh(ByRef s As String)
s = ""
OpenFileDialog1.Filter = "Textfiles (*.txt)|*.txt"
OpenFileDialog1.ShowDialog()
Do While s = ""
s = OpenFileDialog1.FileName
Loop
End Sub
Sub writeputh(ByRef s As String)
s = ""
SaveFileDialog1.Filter = "Textfiles (*.txt)|*.txt"
SaveFileDialog1.ShowDialog()
Do While s = ""
s = SaveFileDialog1.FileName
Loop
End Sub
Sub ch(ByVal Str As String, ByRef Res As String)
Dim a As Char
Res = Mid(Str, 1, 1)
For i = 2 To Len(Str)
a = CChar(Mid(Str, i, 1))
If InStr(Res, a) = 0 Then
Res = Res + a
End If
Next
End Sub
Sub resh(ByVal filename1 As String, ByVal filename2 As String, ByRef lb1 As ListBox, ByRef lb2 As ListBox)
Dim rf As StreamReader
Dim wf As StreamWriter
Dim s1, s2, s3 As String
s2 = ""
s3 = ""
Try
rf = New StreamReader(filename1)
wf = New StreamWriter(filename2, True)
Do While Not rf.EndOfStream()
s1 = rf.ReadLine()
lb1.Items.Add(s1)
ch(s1, s2)
wf.WriteLine(s2)
lb2.Items.Add(s2)
Loop
wf.Close()
rf.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim filename1, filename2 As String
readputh(filename1)
writeputh(filename2)
resh(filename1, filename2, ListBox1, ListBox2)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End
End Sub
End Class
Input file:
youtubeyoutubeyotube
dogdogdogdog
geeksforgeeks
Output file:
youtbe
dog
geksfor
But I expect Output file of only "dog"
I just couldn't handle the old code. One based functions? No! You can translate it back it you want but output is now dog.
Private Function GetOpenPath() As String
OpenFileDialog1.Filter = "Textfiles (*.txt)|*.txt"
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Return OpenFileDialog1.FileName
Else
Return Nothing
End If
End Function
Private Function GetSavePath() As String
SaveFileDialog1.Filter = "Textfiles (*.txt)|*.txt"
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Return SaveFileDialog1.FileName
Else
Return Nothing
End If
End Function
Private Function ch(ByVal Str As String) As String
Dim a As Char
Dim Res = Str.Substring(0, 1)
For i = 1 To Str.Length - 1
a = CChar(Str.Substring(i, 1))
If Res.IndexOf(a) = -1 Then
Res &= a
End If
Next
Return Res
End Function
Sub resh(ByVal filename1 As String, ByVal filename2 As String)
Dim lines = File.ReadAllLines(filename1)
Dim NewLines As New List(Of String)
For Each line In lines
Try
ListBox1.Items.Add(line)
Dim s2 = ch(line)
NewLines.Add(s2)
ListBox2.Items.Add(s2)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Next
Dim Shortest = NewLines.OrderByDescending(Function(s) s.Length).Last()
File.WriteAllText(filename2, Shortest)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim OpenPath = GetOpenPath()
Dim SavePath = GetSavePath()
If String.IsNullOrEmpty(OpenPath) OrElse String.IsNullOrEmpty(SavePath) Then
MessageBox.Show("You must provide a file. Try again.")
Return
End If
resh(OpenPath, SavePath)
End Sub

How to remove path in my search code on vb.net

I am very new to VB and I have an assignment which requires me to have search code in the program.The search code works but it shows the path of the file and I just want it to show the name of the txt file.
Private Sub SearchOrdersToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SearchOrdersToolStripMenuItem.Click
txtSearch.Visible = True
Lblsearch.Visible = True
Dim backslash As String() = FrmLogIn.FolderDirectory.Split("\")
Dim filename As String = backslash(6)
ListOfAllFileNames = System.IO.Directory.GetFiles(FrmLogIn.FolderDirectory)
TxtShoworders.Lines = ListOfAllFileNames
End Sub
Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
If txtSearch.Text = "" Then
TxtShoworders.Lines = ListOfAllFileNames
Return
Else
Dim SearchResults As New List(Of String)
For Each currentFileName In ListOfAllFileNames
If currentFileName.Contains(txtSearch.Text) Then
SearchResults.Add(currentFileName)
End If
Next
TxtShoworders.Lines = SearchResults.ToArray()
End If
End Sub
Link to what the program looks like and the directory showing
If anyone could help me with this that would be great, thanks.
You'll have to use Path.GetFileName for this, example:
SearchResults.Add(Path.GetFileName(currentFileName))
To keep your list of paths but only show the filenames, try this:
Private Sub SearchOrdersToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SearchOrdersToolStripMenuItem.Click
txtSearch.Visible = True
Lblsearch.Visible = True
Dim backslash As String() = FrmLogIn.FolderDirectory.Split("\")
Dim filename As String = backslash(6)
ListOfAllFileNames = System.IO.Directory.GetFiles(FrmLogIn.FolderDirectory)
TxtShoworders.Lines = GetFileNames(ListOfAllFileNames)
End Sub
Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
If txtSearch.Text = "" Then
TxtShoworders.Lines = GetFileNames(ListOfAllFileNames)
Return
Else
Dim SearchResults As New List(Of String)
For Each currentFileName In ListOfAllFileNames
If currentFileName.Contains(txtSearch.Text) Then
SearchResults.Add(currentFileName)
End If
Next
TxtShoworders.Lines = GetFileNames(SearchResults.ToArray())
End If
End Sub
Private Function GetFileNames(Byval paths as String()) As String()
Return paths.Select(Function(p) Path.GetFileName(p)).ToArray()
End Function
This will keep your ListOfAllFileNames containing the paths while only showing the file names using GetFileNames.

how to filter a datagridview when a database is not being used

I am able get the contents of a csv file to read into DataGridView1, but I am having trouble filtering the data from textbox2. I tried different things I have found online, but nothing has worked so far. this is what i have:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim fName As String = ""
OpenFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.Filter = "CSV Files (*.csv)|*.csv"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True
If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
fName = OpenFileDialog1.FileName
End If
Me.TextBox1.Text = fName
GetData(fName, DataGridView1, True)
End Sub
Private Sub GetData(ByVal Path As String, ByRef DG As DataGridView, Optional ByVal NoHeader As Boolean = False)
Dim Fields() As String
Dim Start As Integer = 1
If NoHeader Then Start = 0
If Not File.Exists(Path) Then
Return
End If
Dim Lines() As String = File.ReadAllLines(Path)
Lines(0) = Lines(0).Replace(Chr(34), "")
Fields = Lines(0).Split(",")
If NoHeader Then
For I = 1 To Fields.Count - 1
Fields(I) = Str(I)
Next
End If
For Each Header As String In Fields
DG.Columns.Add(Header, Header)
Next
For I = Start To Lines.Count - 1
Lines(I) = Lines(I).Replace(Chr(34), "")
Fields = Lines(I).Split(",")
DG.Rows.Add(Fields)
Next
End Sub
I just want to be able to filter say column 5 (no column headers in csv file) by typing something in textbox2. Any help would be greatly appreciated. Thank you
Don't add the values to a DGV, but instead use a DataTable then just make a query on that. Example considers you have a combobox for some filter choices - they are other ways to get this to depending on your needs. This method requires you to have headers though.
Private dt As New DataTable
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim fName As String = ""
OpenFileDialog1.InitialDirectory = "C:\"
OpenFileDialog1.Filter = "CSV Files (*.csv)|*.csv"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True
If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
fName = OpenFileDialog1.FileName
End If
Me.TextBox1.Text = fName
LoadData(fName)
FilterData(combobox1.Text, "some column name")
End Sub
Private Sub LoadData(ByVal Path As String)
Dim Fields() As String
Dim Start As Integer = 1
If NoHeader Then Start = 0
If Not File.Exists(Path) Then
Return
End If
Dim Lines() As String = File.ReadAllLines(Path)
Lines(0) = Lines(0).Replace(Chr(34), "")
Fields = Lines(0).Split(",")
For I = 1 To Fields.Count - 1
Fields(I) = Str(I)
Next
For Each Header As String In Fields
dt.Columns.Add(Header, Header)
Next
For I = Start To Lines.Count - 1
Lines(I) = Lines(I).Replace(Chr(34), "")
Fields = Lines(I).Split(",")
dt.Rows.Add(Fields)
Next
End Sub
Private Sub FilterData(filter As String, columnName as string)
Dim query = From dr As DataRow In dt.Rows Where dr(columnName).ToString = filter
DGV.DataSource = query
End Sub

VB.NET File Explorer

I'm making a File Explorer in VB.NET. Everything is going fine except one thing. When you click the dynamically created label to "open" a folder, I need to get the value of the label (so I can set a variable to it.) I can't get the value because the label was dynamically create. Therefore the object doesn't exist. Here's my code:
Imports System.IO
Public Class Form1
Dim Path As String
Dim FolderCount = 0
Dim FolderWidth = 128
Dim FolderHeight = 128
Dim WidthAndPadding = FolderWidth + 10
Dim HeightAndPadding = FolderHeight + 10
Dim FolderTitle As String
Dim CombinedWidth
Dim FolderRows = 0
Dim OnNewLine = False
Dim FolderTop = 0
Dim FolderLeft = 0
Dim FullPath
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.AutoScroll = True
Path = "C:\Program Files\"
For Each Dir As String In Directory.GetDirectories(Path)
CreateFolders(Dir)
Next
End Sub
Public Sub CreateFolders(ByVal StrDirectory)
FolderTitle = StrDirectory.Substring(Path.Length)
Dim Folder As New Label
FolderLeft = WidthAndPadding * FolderCount
Folder.Left = FolderLeft
Folder.Top = FolderTop
Folder.Width = FolderWidth
Folder.Height = FolderHeight
Folder.Image = My.Resources.Folder
Folder.TextAlign = ContentAlignment.BottomCenter
If FolderTitle.Length > 15 Then
FolderTitle = FolderTitle.Substring(0, 15) + "..."
End If
Folder.Text = FolderTitle
Folder.Font = New Font("Arial", 9.5)
FolderCount += 1
CombinedWidth = FolderCount * WidthAndPadding
If CombinedWidth >= Me.Width Then
OnNewLine = False
If OnNewLine = False Then
FolderRows = FolderRows + 1
FolderCount = 0
CombinedWidth = 0
Folder.Left = FolderCount * FolderWidth
FolderTop = FolderRows * FolderHeight
Folder.Top = FolderTop
OnNewLine = True
'FolderCount += 1
End If
End If
Me.Controls.Add(Folder)
AddHandler Folder.DoubleClick, AddressOf Folder_DoubleClick
FullPath = Path + FolderTitle
End Sub
Private Sub Folder_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
'MsgBox(Folder.Text)
End Sub
End Class
The event handler's sender parameter contains the label that generated the event.
You can cast it back to Label by writing CType(sender, Label).