|
-
May 7th, 2013, 06:46 AM
#1
Thread Starter
Lively Member
Get values from a file and put them to a DomainDropDown
Hi !
I need some help.What im trying to do:
I want to store some kind of XYZ positions and their names in a file that should be in the same directory as the application is.Then i want my app to read them, and put the names to a dropdown (then when i will select them it will set the XYZ positions)
So if the file called like "xyzpositions.txt" has the value like this:
Code:
Name(Position number 1) X(-2057,281) Y(237,2792) Z(35,40573)
Name(Position number 2) X(-1725,082) Y(196,1296) Z(3,554688)
Name(Position number 3) X(-1826,597) Y(173,6213) Z(65,14328)
It would add those items to the DomainDropDown, "Position number 1" "Position number 2" and "Position number 3"
The code that is in the txt file may be different, but i want to keep it easy to edit manually (it wont be saved in the app, but it can be added by the user)
And if he puts 10 positions to the text file, it has to add 10 items called same as the positions etc etc.
And then if i select the position for example.. 3 then it sets the variable values:
posX = X() of position 3 from the text file
posY = Y() of position 3 from the text file
posZ = Z() of position 3 from the text file
I have no idea how to do this
Thanks in advance
-
May 7th, 2013, 06:53 AM
#2
Hyperactive Member
Re: Get values from a file and put them to a DomainDropDown
You would be better off using XML over text files.
Have a look at this MSDN how to http://support.microsoft.com/kb/316730
-
May 7th, 2013, 07:19 AM
#3
Thread Starter
Lively Member
Re: Get values from a file and put them to a DomainDropDown
Well i forgot to add that i dont want XML It looks complicated for others, all those < > etc.The text file solution with code like above is the best for me
-
May 7th, 2013, 07:30 AM
#4
Hyperactive Member
Re: Get values from a file and put them to a DomainDropDown
There is nothing difficult about it, have a read through the link I sent you. All you need to do is create a class with you properties in it, then serialize it.
Code:
Dim objStreamWriter As New StreamWriter("C:\Product.xml")
Dim x As New XmlSerializer(p.GetType)
x.Serialize(objStreamWriter, p)
objStreamWriter.Close()
-
May 7th, 2013, 08:51 AM
#5
Re: Get values from a file and put them to a DomainDropDown
this uses a text file, regex + classes:
Code:
Imports System.Text.RegularExpressions
Public Class Form1
Dim XYZList As New List(Of XYZ)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lines() As String = IO.File.ReadAllLines("filename.txt")
Dim rx As New Regex("\((?<position>[\w\s]+)\)\sX\((?<x>-?\d+\,\d+)\)\sY\((?<y>-?\d+\,\d+)\)\sZ\((?<z>-?\d+\,\d+)\)")
For Each line As String In lines
Dim m As Match = rx.Match(line)
If Not m.Success Then Continue For
Dim item As New XYZ
item.displayString = m.Groups("position").Value
Dim o1 As New OneValue(m.Groups("x").Value)
item.X = o1
Dim o2 As New OneValue(m.Groups("y").Value)
item.Y = o2
Dim o3 As New OneValue(m.Groups("z").Value)
item.Z = o3
XYZList.Add(item)
Next
DomainUpDown1.Items.AddRange(XYZList)
DomainUpDown1.SelectedIndex = 0
End Sub
Private Sub DomainUpDown1_SelectedItemChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DomainUpDown1.SelectedItemChanged
If DomainUpDown1.SelectedItem Is Nothing Then Return
Dim item As XYZ = DirectCast(DomainUpDown1.SelectedItem, XYZ)
Label1.Text = String.Format("X: {0}", item.X)
Label2.Text = String.Format("Y: {0}", item.Y)
Label3.Text = String.Format("Z: {0}", item.Z)
'to split the X, Y, or Z value, use
'item.X.num1
'item.X.num2
End Sub
End Class
Code:
Public Class XYZ
Private _X As OneValue
Public Property X() As OneValue
Get
Return _X
End Get
Set(ByVal value As OneValue)
_X = value
End Set
End Property
Private _Y As OneValue
Public Property Y() As OneValue
Get
Return _Y
End Get
Set(ByVal value As OneValue)
_Y = value
End Set
End Property
Private _Z As OneValue
Public Property Z() As OneValue
Get
Return _Z
End Get
Set(ByVal value As OneValue)
_Z = value
End Set
End Property
Private _displayString As String
Public Property displayString() As String
Get
Return _displayString
End Get
Set(ByVal value As String)
_displayString = value
End Set
End Property
Public Overrides Function ToString() As String
Return Me.displayString
End Function
End Class
Code:
Public Class OneValue
Private _num1 As Integer
Public Property num1() As Integer
Get
Return _num1
End Get
Set(ByVal value As Integer)
_num1 = value
End Set
End Property
Private _num2 As Integer
Public Property num2() As Integer
Get
Return _num2
End Get
Set(ByVal value As Integer)
_num2 = value
End Set
End Property
Public Overrides Function ToString() As String
Return String.Format("{0},{1}", num1, num2)
End Function
Public Sub New(ByVal input As String)
Dim fields() As String = input.Split(","c)
Me.num1 = CInt(fields(0))
Me.num2 = CInt(fields(1))
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 9th, 2013, 10:27 AM
#6
Thread Starter
Lively Member
Re: Get values from a file and put them to a DomainDropDown
I did exactly like you posted .paul and no errors while compiling, however when i try to switch the item in the DomainUpDown1 it gives a error:
Unable to cast object of type 'System.String' to type 'XYZ
This is my filename.txt text:
Code:
Name(Position number 1) X(-2057,281) Y(237,2792) Z(35,40573)
Name(Position number 2) X(-1725,082) Y(196,1296) Z(3,554688)
Name(Position number 3) X(-1826,597) Y(173,6213) Z(65,14328)
-
May 9th, 2013, 11:07 AM
#7
Re: Get values from a file and put them to a DomainDropDown
can you post the exact code you tried?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 9th, 2013, 11:16 AM
#8
Thread Starter
Lively Member
Re: Get values from a file and put them to a DomainDropDown
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Dim XYZList As New List(Of XYZ)
Dim lines() As String = IO.File.ReadAllLines("positions.txt")
Dim rx As New Regex("\((?<position>[\w\s]+)\)\sX\((?<x>-?\d+\,\d+)\)\sY\((?<y>-?\d+\,\d+)\)\sZ\((?<z>-?\d+\,\d+)\)")
For Each line As String In lines
Dim m As Match = rx.Match(line)
If Not m.Success Then Continue For
Dim item As New XYZ
item.displayString = m.Groups("position").Value
Dim o1 As New OneValue(m.Groups("x").Value)
item.X = o1
Dim o2 As New OneValue(m.Groups("y").Value)
item.Y = o2
Dim o3 As New OneValue(m.Groups("z").Value)
item.Z = o3
XYZList.Add(item)
Next
DomainUpDown2.Items.AddRange(XYZList)
DomainUpDown2.SelectedIndex = 0
Catch
End Try
End Sub
Public Class XYZ
Private _X As OneValue
Public Property X() As OneValue
Get
Return _X
End Get
Set(ByVal value As OneValue)
_X = value
End Set
End Property
Private _Y As OneValue
Public Property Y() As OneValue
Get
Return _Y
End Get
Set(ByVal value As OneValue)
_Y = value
End Set
End Property
Private _Z As OneValue
Public Property Z() As OneValue
Get
Return _Z
End Get
Set(ByVal value As OneValue)
_Z = value
End Set
End Property
Private _displayString As String
Public Property displayString() As String
Get
Return _displayString
End Get
Set(ByVal value As String)
_displayString = value
End Set
End Property
Public Overrides Function ToString() As String
Return Me.displayString
End Function
End Class
Public Class OneValue
Private _num1 As Integer
Public Property num1() As Integer
Get
Return _num1
End Get
Set(ByVal value As Integer)
_num1 = value
End Set
End Property
Private _num2 As Integer
Public Property num2() As Integer
Get
Return _num2
End Get
Set(ByVal value As Integer)
_num2 = value
End Set
End Property
Public Overrides Function ToString() As String
Return String.Format("{0},{1}", num1, num2)
End Function
Public Sub New(ByVal input As String)
Dim fields() As String = input.Split(","c)
Me.num1 = CInt(fields(0))
Me.num2 = CInt(fields(1))
End Sub
End Class
Private Sub DomainUpDown2_SelectedItemChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DomainUpDown2.SelectedItemChanged
If DomainUpDown2.SelectedItem Is Nothing Then Return
Dim item As XYZ = DirectCast(DomainUpDown2.SelectedItem, XYZ)
Label1.Text = String.Format("X: {0}", item.X)
Label2.Text = String.Format("Y: {0}", item.Y)
Label3.Text = String.Format("Z: {0}", item.Z)
'to split the X, Y, or Z value, use
'item.X.num1
'item.X.num2
End Sub
//EDIT
I fixed it, the DomainUpDown2 was a copy of DomainUpDown1 and i though the items wont stay, there were 2 items in the DomainUpDown2, i removed them and now it works.
Anyways, could you make this for ComboBox ? When i replace DomainUpDown with ComboBox1 like this ComboBox1.Items.AddRange(XYZList)
it gives a error Value of type 'System.Collections.Generic.List(Of myapp.Form1.XYZ)' cannot be converted to '1-dimensional array of Object'.
Last edited by silentus; May 9th, 2013 at 11:22 AM.
-
May 9th, 2013, 12:41 PM
#9
Re: Get values from a file and put them to a DomainDropDown
try:
Code:
ComboBox1.Items.AddRange(XYZList.ToArray)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|