QWERTYtech

  1. How would I go about telling items in my ToolStripMenu to be "Enabled" on if there is a current "Child" window open
  2. If I have a single/multiple "Child(ren)" open and I want to Select All the text. How would I go about this
  3. Also, I want to have a "Undo/Redo" actions. I can't really find any good examples. How would I go about this
  4. How would I go about populating a "TreeView" with items through an FTP connection

Basically, I decided to make a small app that will allow me to edit/create items from any computer and save it back to my server through an FTP connection.



Re: Visual Basic General Option in MDIParent

cybertaz69

whhooooo big list...

Lest start with the check if a mdichild form is open...

Code Snippet

me.toolstripcombobox.combobox.displaymember = "Text" 'Not need for your code

For each myform as form in my.application.openforms

if myform.ismdichild then

me.toolstripcombobox.items.add(myform)

end if

This add the form instance to the combobox you can do what ever with it,,, Enable button change focus etc...






Re: Visual Basic General Option in MDIParent

cybertaz69

Item 2... What text are you referring too.. a text box on the form the form tiltle or some thing else

item 3.... Start simple... Select a control that you would like to add the undo and redo then go to forms...

item 4... Do already have the FTP code to get a directory Or just need help on the treeview






Re: Visual Basic General Option in MDIParent

QWERTYtech

Where would be the best place to put this code so that it constantly checks





Re: Visual Basic General Option in MDIParent

QWERTYtech

cybertaz69 wrote:

Item 2... What text are you referring too.. a text box on the form the form tiltle or some thing else

item 3.... Start simple... Select a control that you would like to add the undo and redo then go to forms...

item 4... Do already have the FTP code to get a directory Or just need help on the treeview

Item 2 = I have a "ChildForm" created called TextEditor.vb which has a RichTextBox docked in it.

Item 4 = Well I haven't gotten any of the FTP or TreeView setup. This one is gonna be a lot of work.





Re: Visual Basic General Option in MDIParent

cybertaz69

Assoomming the childforms are of one type..... Im' going to use mychildform as the class name...

First you need to know what instance is open.... Use the code above to get a collection of open forms.

Code Snippet

me.toolstripcombobox.combobox.displaymember = "Text" 'Not need for your code

Dim FormText() as string

Dim counter as integer = 0

For each myform as form in my.application.openforms

if myform.ismdichild then

FormText(counter) = ctype(myform,mychildform).richtextbox1.text

counter += 1

end if

Next

The array holds the text for each form.

when I get back to my office i'm post the ftp examples.... That the hard part. The tree is pretty simple...




Re: Visual Basic General Option in MDIParent

cybertaz69

Code Snippet

Private username As String = "username"

Private Password As String = "password"

Private Sub btnListFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

Handles btnListFiles.Click

Me.ListBox1.Items.Clear()

Me.ListBox1.Items.AddRange(ListDirectory)

End Sub

Public Function ListDirectory(Optional ByVal directory As String = "") As Object()

'return a simple list of filenames in directory

Dim ftp As Net.FtpWebRequest = _

CType(FtpWebRequest.Create(ftp://yourftpsite.com & directory), FtpWebRequest)

ftp.Credentials = New Net.NetworkCredential(username, Password)

ftp.KeepAlive = False

'Set request to do simple list

ftp.Method = Net.WebRequestMethods.Ftp.ListDirectory

Dim str As String = ""

Using response As FtpWebResponse = CType(ftp.GetResponse, FtpWebResponse)

Dim size As Long = response.ContentLength

Using datastream As System.IO.Stream = response.GetResponseStream

Using sr As New System.IO.StreamReader(datastream)

str = sr.ReadToEnd()

sr.Close()

End Using

datastream.Close()

End Using

response.Close()

End Using

'replace CRLF to CR, remove last instance

str = str.Replace(vbCrLf, vbCr).TrimEnd(Chr(13))

Return str.Split(Chr(13))

End Function

This is the simplest example I could come up with that gets the FTP directory....

Code explained:::

1. Create a ftpwebrequest using the FTP information

2. Add the security credentials to the request.

3. Tell the reqest what information is wanted...

4. Create a responce to capture the output of the request.

5. Read in the data stream... (close connections)

6. Get rid of the exta char at the end of the string...

7. Returns the string divided up into an object array. (Makes adding data easy for lists and other controls.)






Re: Visual Basic General Option in MDIParent

QWERTYtech

cybertaz69 wrote:

Code Snippet

Private username As String = "username"

Private Password As String = "password"

Private Sub btnListFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

Handles btnListFiles.Click

Me.ListBox1.Items.Clear()

Me.ListBox1.Items.AddRange(ListDirectory)

End Sub

Public Function ListDirectory(Optional ByVal directory As String = "") As Object()

'return a simple list of filenames in directory

Dim ftp As Net.FtpWebRequest = _

CType(FtpWebRequest.Create(ftp://yourftpsite.com & directory), FtpWebRequest)

ftp.Credentials = New Net.NetworkCredential(username, Password)

ftp.KeepAlive = False

'Set request to do simple list

ftp.Method = Net.WebRequestMethods.Ftp.ListDirectory

Dim str As String = ""

Using response As FtpWebResponse = CType(ftp.GetResponse, FtpWebResponse)

Dim size As Long = response.ContentLength

Using datastream As System.IO.Stream = response.GetResponseStream

Using sr As New System.IO.StreamReader(datastream)

str = sr.ReadToEnd()

sr.Close()

End Using

datastream.Close()

End Using

response.Close()

End Using

'replace CRLF to CR, remove last instance

str = str.Replace(vbCrLf, vbCr).TrimEnd(Chr(13))

Return str.Split(Chr(13))

End Function

This is the simplest example I could come up with that gets the FTP directory....

Code explained:::

1. Create a ftpwebrequest using the FTP information

2. Add the security credentials to the request.

3. Tell the reqest what information is wanted...

4. Create a responce to capture the output of the request.

5. Read in the data stream... (close connections)

6. Get rid of the exta char at the end of the string...

7. Returns the string divided up into an object array. (Makes adding data easy for lists and other controls.)

OMG! You are such a life saver. One quick question. Will this FTP Connection stay open constantly Or will it close after downloading list

I really want it to stay open constantly so that I can open a "file" on the server and modify it then update it via the FTP w/o having to keep making a new connection.

Also, as I was reading through the code I noticed where you had the site address hard coded to the prog. Is there a way that I can add an option in my prog that will allow the user to change that address and it be stored that way in the program. I kinda know that u have to create a string in the Properties of the Project under Settings. How can I make a window that will allow me to change it and save it.





Re: Visual Basic General Option in MDIParent

cybertaz69

No this example opens and closes the connection but it can be modified to keep the connection open but it is really Not rowbust enough to do that...

The site address is just a string so you can use textbox or anything else to enter the string... (Just remeber to check it to make sure its formated properly...) Same with the user and password.......

To save it.. Create a setting property as string...

To save

Code Snippet

my.settings.mysettingname = "mydata"

my.settings.save

To open.

Code Snippet
Dim username as string = my.settings.mysettingname






Re: Visual Basic General Option in MDIParent

QWERTYtech

Okay, so i have been looking over your Code Snippet that you gave me. The changes that i have made are in purple background. I was wanting to see if you think this will work with what im using. I'm using a TreeView instead of the ListBox. Please let me know if I am doing anything wrong.

Code Snippet

Private username As String = My.Settings.FTPusername

Private Password As String = My.Settings.FTPpassword

Private Sub btnListFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _

Handles btnListFiles.Click

Me.FTPTreeView.Items.Clear()<--Unsure what to put there.

Me.FTPTreeView.Items.AddRange(ListDirectory)<-- Unsure what to put there.

End Sub

Public Function ListDirectory(Optional ByVal directory As String = "") As Object()

'return a simple list of filenames in directory

Dim ftp As Net.FtpWebRequest = _

CType(FtpWebRequest.Create(My.Settings.FTPLocation & directory), FtpWebRequest)

ftp.Credentials = New Net.NetworkCredential(username, Password)

ftp.KeepAlive = True <-- this will make the connection stay alive unless i give a command to kill it right

'Set request to do simple list

ftp.Method = Net.WebRequestMethods.Ftp.ListDirectory

Dim str As String = ""

Using response As FtpWebResponse = CType(ftp.GetResponse, FtpWebResponse)

Dim size As Long = response.ContentLength

Using datastream As System.IO.Stream = response.GetResponseStream

Using sr As New System.IO.StreamReader(datastream)

str = sr.ReadToEnd()

sr.Close()

End Using

datastream.Close()

End Using

response.Close()

End Using

'replace CRLF to CR, remove last instance

str = str.Replace(vbCrLf, vbCr).TrimEnd(Chr(13))

Return str.Split(Chr(13))

End Function

What kind of command would I give a button to kill the FTP connection Also what command to refresh the view in the FTPTreeView

What kind of code would I have to write to make everything in like My Computer & Network Places to show up in another LocalTreeView





Re: Visual Basic General Option in MDIParent

cybertaz69

Private Password As String = My.Settings.FTPpassword For now that will work but think about encrypting it later so its not in plain text for some one to read...

Tree view uses nodes..

Me.FTPTreeView.Nodes.Clear()<--Unsure what to put there.

Me.FTPTreeView.nodes.AddRange(ListDirectory)<-- Unsure what to put there.

The list directory needs to return a array of treenodes to work with the addrange function..

Public Function ListDirectory(Optional ByVal directory As String = "") As treenodes()

ftp.KeepAlive = True <-- this will make the connection stay alive unless i give a command to kill it right No.. It will not allow it to time out but if the function falls from scope the grabage colloector will closing at some point... or when the request is closed..

Since the return type is different we need to correct it.. (or you could make a converter function)

Code Snippet

Dim StringNames() as String = str.Split(Chr(13))

Dim Names(StringNames.getupperbound(0)) as treenode

Dim counter as integer = 0

For each name as string in StringNames

Dim node as treenode

node.text = name

Names(counter) = node

counter += 1

next

Return Names

Remember: I've already posted this code is for simple grabs.. Trying to keep it open is possable but not suggested...

Also remember that this just grabs the first level of directories, you'll need to run it again for each subdirectory to keep building the tree.. You can use a recursive technique or loop though the root nodes to build the next branch on the tree...

Code Snippet

for each tn as treenode in me.treeview.nodes

if node.text = (A directory name) then 'Way to test::::: Directory normally don't have periods

tn.nodes.addrange(DirectoryList("\"&node.text)

end if






Re: Visual Basic General Option in MDIParent

cybertaz69

This is an example on how to get the total directoy size. I'm using this example becasue it shows a good example of recurive technique......

Code Snippet
Public Function DirSize(ByVal d As DirectoryInfo) As Long
Dim Size As Long = 0
' Add file sizes.
Dim fis() As FileInfo = d.GetFiles()
Dim fi As FileInfo
For Each fi In fis
Size += fi.Length
Next fi
' Add subdirectory sizes.
Dim dis() As DirectoryInfo = d.GetDirectories()
Dim di As DirectoryInfo
For Each di In dis
Size += DirSize(di)
Next di
Return Size
End Function 'DirSize

Walk Though:

It a function that let you put in a starter directory....

It collects all the files and gets the size.

It then collects the directoies.

For each directory it calls itself using the new directory name.

Once completed with all the directoies it return the total sum of all the directoies and files for all subdirectories...

The reson I show you this is it is a nessary tool need when dealing with tree views.

me.treeview.node 'The root

me.treeview.node(0).node(0) ' The first branch and first item on that branch..

me.treeview.node(0).node ' Collection of branches on the first item of the root.....

Instead of grabing the size you would create nodes with nodes on them......







Re: Visual Basic General Option in MDIParent

cybertaz69

QWERTYtech wrote:

Where would be the best place to put this code so that it constantly checks

You don't want to constantly check... Checking all the time would make your app very very very.... SLOW......

Create a event that fires when you add a new form..... In the event add it to a collection of active forms..... The same when a form is closed. Create an event that remove the form from the collection.....