|
I really need help with this. I have tried multiple solutions but have not found an answer. Also, time will soon be a factor because this project is due at the end of June. The problem is I need my program to display files in an organized fashion and open them. I've done this but I need to just display the file name while maintaining functionality. My attempts have always left the full path to the file intact. Please, I don't even need the code, just a description of the solution.
|
|
|
|
|
If you use the FileInfo object
<br />
FileInfo fiYourFile = new FileInfo("Path\To\File.txt");<br />
There's a list of properties. FullName and Name both contain the name of the file, FullName is the full name including the path to the file. Name is just the name of the fil including it's extension. Is this the answer you're looking for? Else I don't understand your question..
|
|
|
|
|
I have this part down so far. If I use the .name property then I get the output I am looking for but I don't get path to the file necessary to open it. I need a way to give a list item one value and display another. I'd rather not use XML or databases.
|
|
|
|
|
Can you provide more description of what you are looking to achieve?
Do you want entire path intact or not?
Best Regards,
Sam Xavier
www.componentone.com
|
|
|
|
|
The program I am creating is a jump drive organizer. It is supposed to be able to organize various file types into separate lists. from these lists the file needs to be able to be selected and opened. My problem is that I don't want to display the fullpath to the file but only the name while still being able to open the file. (So far i've been manipulating strings for such things as choosing a directory.) I need the value of an item to be different than what is displayed the the end user.
|
|
|
|
|
Could you not create your own class with your overloaded "ToString" function... e.g.:
Public Class MyFileName
Public FileName As String
Public Overrides Function ToString() As String
Return System.IO.Path.GetFileName(FileName)
End Function
End Class
Then, when you add one of these items to a listbox, it will only display the filename, however, when you access the object, you can use the Filename string:
MessageBox.Show(DirectCast(ListBox1.Items(0), MyFileName).FileName)
|
|
|
|
|
I've just began learning about the concepts of object oriented programming this year. (second year of taking the comp. sci. course). I can use built in classes but still get a little confused making my own. Thanks for the help.
|
|
|
|
|
Hi,
No problem. It is really a simple class, but very useful in this instance (assuming you are displaying the list of files in a listbox, and using vb.net!!!).
If you are still struggling, here is a bit of messy code that may show you a bit more. All you need to do is create a new form with one listbox called listbox1, and paste this code into your app.
Public Class MyFileNameClass
Public FileName As String
Public Overrides Function ToString() As String
Return System.IO.Path.GetFileName(FileName)
End Function
End Class
Private Sub AddItemsToListBox(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Me.ListBox1.Items.Clear()
For Each FileNameStr As String In IO.Directory.GetFiles("c:\")
Dim o As New MyFileNameClass
o.FileName = FileNameStr
Me.ListBox1.Items.Add(o)
Next
End Sub
Public Sub DisplayInfo(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.Click
If ListBox1.SelectedIndices.Count > 0 Then
For Each i As Integer In ListBox1.SelectedIndices
Dim o As Object = ListBox1.Items(i)
If TypeOf o Is MyFileNameClass Then
MessageBox.Show(DirectCast(o, MyFileNameClass).FileName)
End If
Next
End If
End Sub
This code should populate the listbox with all the files in your c:\ folder, but only display the name. When you click on a file, a messagebox will pop up with the full path & filename. Nothing exciting, but I think it may get you closer to what you want to do.
|
|
|
|
|
thanks, you saved me hours of work.
|
|
|
|
|
I appreciate the help but I need to ask one more favor. Your code works but I can't integrate it "as is" into my code because my program requires a file search within a given directory. I cannot put a type fileinfo value through this class. (sorry if my working knowledge of classes seems limited.). I tried converting the data to string but that didn't work. heres the code I attempted to use:
Dim dir As String = ComboBox1.Text
Dim files As String
Dim search As DirectoryInfo = New DirectoryInfo("m:\") '(dir & ":\")
For Each file As FileInfo In search.GetFiles("*.doc", SearchOption.AllDirectories)
Dim cls As New MyFileNameClass
cls.FileName = (file.ToString)
ListBox1.Items.Add(cls)
Next
I would repeat the for each/ next statement for every subsequent search I wanted to make. (I commented out the user directory selection because it was causing problems.)
|
|
|
|
|
Looking at it in a bit more detail, you can actually put an object of type "FileInfo" into a listbox, and it will have the same functionality:
Dim dir As String = "C:\"
Dim search As DirectoryInfo = New DirectoryInfo("c:\Windows\") '(dir & ":\")
For Each file As FileInfo In search.GetFiles("*.*", SearchOption.AllDirectories)
ListBox1.Items.Add(file)
Next
and in the click handler:
Public Sub DisplayInfo(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.Click
If ListBox1.SelectedIndices.Count > 0 Then
For Each i As Integer In ListBox1.SelectedIndices
Dim o As Object = ListBox1.Items(i)
If TypeOf o Is FileInfo Then
MessageBox.Show(DirectCast(o, FileInfo).FullName)
End If
Next
End If
End Sub
Maybe that is more suitable? It will keep the whole FileInfo block in the listbox.
|
|
|
|
|
You could create a DataTable with columns of Path and FileName, then bind the DataTable to your control, setting the Path as the ValueMember and FileName as the DisplayMember .
This obviously is dependent on the control you are using.
Steve Jowett
-------------------------
It is offen dangerous to try and see someone else's point of view, without proper training. Douglas Adams (Mostly Harmless)
|
|
|
|
|
Hello all,I am wondering anyone can guide me how to read tiny text column for MySQL using VB.NET Datareader.
The following are the code I used to read the column:
Public Function StartupData(ByVal strKeySet as String)
Dim test as string
Dim MyTinyText as string
Try
ObjComm = New OdbcCommand("SELECT SettingValue FROM pos_setting WHERE LocationCode='KK' AND SettingKey='ADD_BINLOG'", ObjConn)
ObjConn.Open()
ObjRead = ObjComm.ExecuteReader()
If ObjRead.Read Then
test = ObjRead.Item(0)
MessageBox.Show("This is " & test & "")
Else
MessageBox.Show("No value")
End If
ObjConn.Close()
Catch ex As Exception
Finally
Objconn.Close
End Try
And my database table as follow:
---------------------------------------------
Field Type Collation Null Key Default Extra Privileges Comment
------------ ----------- ----------------- ------ ------ ------- ------ ------------------------------- -------
LocationCode varchar(10) latin1_swedish_ci NO PRI select,insert,update,references
SettingKey varchar(10) latin1_swedish_ci NO PRI select,insert,update,references
SettingValue tinytext latin1_swedish_ci YES (NULL) select,insert,update,references
I'm wondering why its keep showing there is no data read inside the datareader? Actually there is a '0' inside the SettingValue for that row of data but I'm wondering why it doesn't read it? Anyone can give me a solution for this?
Thanks for reading~!
|
|
|
|
|
|
I'm trying to write a program that navigates an XML file with XPath but I'm having trouble if the XML file is structured a certain way. For example here is the code that navigates the XML file:
Dim XDoc as XPathDocument
Dim xmlNav as XPathNavigator
Dim xmlNI as XPathNodeIterator
XDoc = new XPathDocument("C:\sop.xml")
xmlNav = XDoc.CreateNavigator
xmlNI = xmlNav.Select("/SOP_FILE/QuestionList/Question")
While xmlNI.MoveNext
MsgBox(xmlNI.Current.Name & " - " & xmlNI.Current.Value)
End While
This was just a test function I wrote to try out because this is my first time using XPath; hence it doesn't do much. Now, if I the XML is formated as follows, a message box is never displayed because the xmlNI count is zero:
<?xml version="1.0" encoding="utf-8"?>
<SOP_FILE xmlns="http://schemas.mycompany.com/SOP" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<QuestionList>
<Question Number="1">
<Text>Has a risk assessment been conducted?</Text><Choice Text="Yes" Index="1" PointsTo="2" />
</Question>
<Question Number="2">
<Text>Has an inspection been conducted?</Text><Choice Text="Yes" Index="1" PointsTo="3" />
<Choice Text="No" Index="2" PointsTo="4" />
</Question>
<Question Number="3">
<Text>Is the property lead-based paint free?</Text>
</Question>
</QuestionList>
</SOP_FILE>
However if the second line of the XML file is changed to:
<SOP_FILE>
Instead of:
<SOP_FILE xmlns="http://schemas.mycompany.com/SOP" xmlns:xs="http://www.w3.org/2001/XMLSchema">
Then the test function works properly and loops through all the questions. Can someone please explain to me why this is happening and how to modify the test function so it will work properly. Thanks in advance for any help.
|
|
|
|
|
You'ld probably have better luck getting an answer in the XML forum, however the problem is that the XML document is scoped by a namespace which will have to be addressed in the XPath query.
First though you will need to create a namespace manager:
Dim xmlnsManager As New XmlNamespaceManager(XDoc.NameTable)
Then you'll need to add the namespace:
xmlnsManager.AddNamespace("alias", "http://www.w3.org/2001/XMLSchema")
Then you can use it in the XPath query:
xmlNI = xmlNav.Select("/alias:SOP_FILE/QuestionList/Question", xmlnsManager )
I'm largely language agnostic
After a while they all bug me
|
|
|
|
|
I tried adding the code you mentioned, however the IDE reports that "NameTable" is not a member of XPathDocument. Did you mean to use xmlNav instead? I tried to use xmlNav and still it does not work. Here is the code now with your recommendations:
Dim XDoc As XPathDocument
Dim xmlNav As XPathNavigator
Dim xmlNI As XPathNodeIterator
XDoc = New XPathDocument("C:\file.xml")
xmlNav = XDoc.CreateNavigator
Dim xmlnsManager As New XmlNamespaceManager(xmlNav.NameTable)
xmlnsManager.AddNamespace("alias", "http://www.w3.org/2001/XMLSchema")
xmlNI = xmlNav.Select("/alias:SOP_FILE/QuestionList/Question", xmlnsManager)
While xmlNI.MoveNext
MsgBox(xmlNI.Current.Name & " - " & xmlNI.Current.Value)
End While
|
|
|
|
|
I should probably actually check whether stuff works before posting, I was writing from memory and didn't even notice the XmlPathDocument instead of XmlDocument ...
Anyhow I messed up that, I referenced the wrong uri for the namespace and left off the namespace from the other nodes, this works:
Dim XDoc As XPathDocument
Dim xmlNav As XPathNavigator
Dim xmlNI As XPathNodeIterator
Dim str As New StringReader(*snipped xml*)
XDoc = New XPathDocument(str)
xmlNav = XDoc.CreateNavigator
Dim xmlnsManager As New XmlNamespaceManager(xmlNav.NameTable)
xmlnsManager.AddNamespace("sop", "http://schemas.mycompany.com/SOP")
xmlnsManager.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema")
xmlNI = xmlNav.Select("/sop:SOP_FILE/sop:QuestionList/sop:Question", xmlnsManager)
While xmlNI.MoveNext
MsgBox(xmlNI.Current.Name & " - " & xmlNI.Current.Value)
End While
I'm largely language agnostic
After a while they all bug me
|
|
|
|
|
I am getting data from Com1 from a scale. I need to save info in a textbox. I have read that I need a backgroundworker in order to access the textbox that was created on a different thread. I just need a little help with this. Let me show you what I have.
BackgroundWorker.WorkerReportsProgress = True
BackgroundWorker.WorkerSupportsCancellation = True
Private Sub DataReceived( _
ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles serialPort.DataReceived
stringCom = serialPort.ReadExisting.ToString()
BackgroundWorker.RunWorkerAsync()
End Sub
Private Sub backgroundworker_Dowork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker.DoWork
Dim argument As Object = e.Argument
For Value As Integer = 0 To 1
If BackgroundWorker.CancellationPending Then
Exit For
End If
BackgroundWorker.ReportProgress(Value, stringCom)
BackgroundWorker.CancelAsync()
Next
Threading.Thread.Sleep(100)
End Sub
Private Sub backgroundworker_Progresschanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker.ProgressChanged
Me.txtbxWeight.Text = e.UserState
End Sub
I know I have somethings in here I probably don't need but I was copying code from a book and was adding and taking out things. My goal is to get the weight stored in txtbxweight.text. This code gives me an error still saying it is of a different thread.
Any help is appreciated.
|
|
|
|
|
I would guess that it might be from your use of the stringCom variable. It looks like it must be declared somewhere on form, then set in the DateReceived method, and then you are referencing it inside the background workers DoWork event. What you need to do is pass it into the DoWork when you call the BackgroundWorker.RunWorkerAsync() instead of just referencing it. If this is not the issue, please post more information about the error you are getting and where in the code you are getting it.
Hope this helps.
|
|
|
|
|
I passed it in like you suggested
code Changes:
Private Sub DataReceived( _
ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles serialPort.DataReceived
Dim stringCom As String = serialPort.ReadTo("G")
serialPort.ReadExisting()
BackgroundWorker.RunWorkerAsync(stringCom)
End Sub
Private Sub backgroundworker_Dowork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker.DoWork
Dim argument As Object = e.Argument
For Value As Integer = 0 To 1
If BackgroundWorker.CancellationPending Then
Exit For
End If
BackgroundWorker.ReportProgress(Value, argument)
BackgroundWorker.CancelAsync()
Next
' Threading.Thread.Sleep(100)
End Sub
Private Sub backgroundworker_Progresschanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker.ProgressChanged
Me.txtbxWeight.Text = e.UserState This is where it says "Cross-thread operation not valid: Control 'txtbxWeight' accessed from a thread other than the thread it was created on."
End Sub
Any ideas?
|
|
|
|
|
I'm a little stumped. Perhaps it is because of your Backgroundworker.CancelAsync statement. I'm not exactly sure why you have that in there. Also, you may want to use Me.txtbxWeight.Text = e.UserState.ToString instead....to make sure the cast to a string is done when setting the text....
What happens if you put a break point on the statement that causes the error, and when it hits the break point look at some of the values...does it work the first time it hits hit (When Value is zero) but crashes the second time (when value is one)? Or is it crashing right away?
|
|
|
|
|
It is because you are starting the background worker when you recieve data from the serial port.
"DataRecieved" gets called on a different thread, so, in this instance your background worker is being called on this different thread. I think that you can ditch the background worker altogether, as it doesnt seem to be doing anything important. Instead:
Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialPort.DataReceived
Dim stringCom As String = serialPort.ReadExisting.ToString()
TextChange(StringCom)
End Sub
Delegate Sub TextChangeDelegate(ByVal Str As String)
Sub TextChange(ByVal Str As String)
If Me.txtbxWeight.InvokeRequired Then
Dim d As New TextChangeDelegate(AddressOf TextChange)
Me.Invoke(d, Str)
Else
Me.txtbxWeight.Text = Str
End If
End Sub
This aught to work - I havent compiled it, but it looks about right )
|
|
|
|
|
I have a circle, or any polygon, drawn on a form via VB.NET (2k5 in this case). I would like to know if my cursor is inside of the circle/polygon. Is there an easy way to know this. I've been tinkering with the use of the x,y location of the mouse pointer, but don't know how to convert this into the location of the circle.
Thanks in advance for any advice.
|
|
|
|
|
I am not 100% sure of how to do this, but you could try looking at the Region.Intersect function.
For example, if each of your graphics objects have an associated region, you could test to see if a point (mouse cursor) intersects with your region. If the result region is empty then there is no hit.
|
|
|
|