|
I am programming in VB.NET.
I have a csv comma delimited file with this data in it.
1212.5,10:01:56,2
1212.25,10:01:59,1
1212.5,10:02:00,1
1212.25,10:02:01,1
1212.5,10:02:04,7
1212.25,10:02:04,2
1212.5,10:02:05,2
1212.25,10:02:07,16
1212.5,10:02:10,1
1212.25,10:02:10,1
1212.5,10:02:11,1
I read the second field, which is the time field into this structure TickData(i).dTime.Str But I can't figure out how to put the hour into TickData(i).dTime.H and the minute into TickData(i).dTime.M and the seconds into TickData(i).dTime.S.
My structure's are as follow.
Structure TickTime
Dim H As Single
Dim M As Single
Dim S As Single
Dim Str As String
End Structure
Structure TickD
Dim CPrice As Double
Dim dTime As TickTime
Dim Vol As Double
End Structure
Could someone Please help!
Thank You
Gary
|
|
|
|
|
The key is the Split method.
Dim timeParts() as string
timeparts = TickData(i).dTime.Str.Split(":")
TickData(i).dTime.H = Ctype(timeParts(0), Single)
TickData(i).dTime.M = Ctype(timeParts(1), Single)
TickData(i).dTime.S = Ctype(timeParts(2), Single)
Unless there's a reason for using Single in the Structure for the time elements, you might want to save some bytes, and eliminate any possible rounding issues by using Integer or even Short.
|
|
|
|
|
Thank You very much. I will try this right away!
Gary
|
|
|
|
|
I tried it but this is the error I got.
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="TickChart"
StackTrace:
/////////////////////////////////////////////////////////////////////////////
This is how I did the loop!
Dim timeParts() As String
For i = 1 To UBound(TickData)
timeParts = TickData(i).dTime.Str.Split(":")
TickData(i).dTime.H = CType(timeParts(0), Single)
TickData(i).dTime.M = CType(timeParts(1), Single)
TickData(i).dTime.S = CType(timeParts(2), Single)
Next
|
|
|
|
|
Looks like a confusion of variable names. The object "TickChart" has apparently not been instantiated. I don't see "TickChart" anywhere in this discussion except in the unhandled exception. I may have caused the problem by using "TickData" in my code. Should that be "TickTime" instead?
|
|
|
|
|
Here is how I started
Structure TickTime
Dim H As Single
Dim M As Single
Dim S As Single
Dim Str As String
End Structure
Structure TickD
Dim CPrice As Double
Dim dTime As TickTime
Dim Vol As Double
End Structure
Public TickData() As TickD 'price data array
Then I loaded the data into TickData in a LoadData sub
Sub LoadData()
ReDim TickData(50)
end sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Dim timeParts() As String
For i = 1 To UBound(TickData)
timeParts = TickData(i).dTime.Str.Split(":")
TickData(i).dTime.H = CType(timeParts(0), Single)
TickData(i).dTime.M = CType(timeParts(1), Single)
TickData(i).dTime.S = CType(timeParts(2), Single)
Next
end sub
But I still get that same error from the above Bold Text!
|
|
|
|
|
1. Looks like there is a possible scope issue. Try making both of the Structures and all of their components Public. E.g.:
Public Structure TickTime
Public H As Single
Public M As Single
Public S As Single
Public Str As String
End Structure
2. All arrays in .NET are zero based. Your For...Next loop starts at 1. You might also use the .NET version for getting array bounds. With the below changes the loop will look at all 51 of the array elements.
For i = 0 To TickData.GetUpperbound(0)
3. I'm assuming there is more code in the LoadData sub other than the Redim statement. Are all 51 elements (including TickData(0)) of the TickData array being populated? If not, that could also cause the error. If there is less than 51 data records being read into the array, the size of the TickData array cannot be larger than the number of data records minus 1.
|
|
|
|
|
Thanks alot George, I was trying to read more elements than I had, which was causing the error. Now I will be using TickData.GetUpperbound(0).
Thanks again
Gary
|
|
|
|
|
|
Look into the DateTime.ParseExact function. You may also need to look into the format specifiers
|
|
|
|
|
Thanks you I will do that.
|
|
|
|
|
I am not a VB programmer.
But have to create a macro in excel sheet which uses VB code.
Actually on closing of Excel sheet i have a message box which asks to save the file.
In that i am not able to make VB code to save the file to particular location.
Can anybody provide me VB code so that i can save excel file to desired or predefined location.
I have tried some codes from net but they are not working.
Please help me out as whole module on which i am being working from one week is stuck because of this.
Thanks in advance.
|
|
|
|
|
The workbook object has a close method that has save changes parameter and a filename paramater.
Suspect you need to use that somehow.
|
|
|
|
|
Hi all...
I have created a client-server program in VB.Net to rum on LAN. Since I started my project in Windows Vista Ultimate, program run normally. Now the same program,without any changes, I tried to run on Windows XP SP2 (which have already installed VS2005), it gave an error while connecting to server. The error message is : "a socket operation was attempted to an unreachable host".
Suggest me how to solve this problem.
Thanks.
Gagan
|
|
|
|
|
Are the computers on the same networks?
Ignore your application for a moment, can you ping the target from the command prompt? If you cannot do that from windows, then your app won't see the target.
|
|
|
|
|
I'm trying to validate that all boxes are filled in by the user and not left blank.
'check for missing data
Try
Catch TitleException As Exception
If txtbxMovieTitle.Text = "" Then
MessageBox.Show("Please enter a movie title.", "Data Entry Error", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
With txtbxMovieTitle
.Focus()
.SelectAll()
End With
End Try
Try
Catch DataException As Exception
If radbtnDVD.Checked = False Or radbtnVHS.Checked = False Then
MessageBox.Show("Please select a movie type.", "Enter movie type", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Try
|
|
|
|
|
i think your a little confused. Within your try is where you put your logic that could cause the app to "blow up" the catch is where you want to control what happens if the blow up occurs.
Ex:
try
'do you test here
catch ex as exception
'tell the user what happened by showing the exception
msgbox(ex.tostring)
exit sub
end try
'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous
'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous
|
|
|
|
|
I've found that the simplest method for testing whether a textbox has been populated by the user is to test the length of the text.
If Textbox1.Text.Length <= 0 then
Msgbox("An entry is required in Textbox1")
Textbox1.Focus
Exit Sub
End If
If spaces are also an invalid entry, a simple addition handles that case as well.
If Textbox1.Text.Trim.Length <= 0 then
Msgbox("An entry is required in Textbox1")
Textbox1.Focus
Exit Sub
End If
|
|
|
|
|
Hi,
I have a tabcontrol with 2 tab pages. First tab page has a populated listview control and 2nd tabpage I have a button. My aim is to select the 3rd item in the listview when I clik a button in 2nd tabpage. However I also want to highlight the selected item in listview. Therefore when I go back to 1st tab page from 2nd, the listview should got focused and the 3rd item should be higlighted. How could you do that?
When I primitavely select the item in listview (in code) while I am in 2nd tabpage, the item is not getting selected and no highlight of course.
Thanks.
What a curious mind needs to discover knowledge is noting else than a pin-hole.
|
|
|
|
|
There should be no problem with selecting an item in a listview from another tab. What may be happening is that when the 1st tab is selected, another control on the tab (or the tab itself) is receiving the focus. If that's true and the listview HideSelection property is True, the selected item would not be highlighted until the listview receives the focus.
You might check the tab sequence for all of the controls in the form to ensure the focus is going where you want it when each tab is selected. If that doesn't work, in the 1st tab's GotFocus event you could set the focus to the listview control.
|
|
|
|
|
Thanks for responding to my post.
It is still not working even I set the focus for the listview in tabcontrols tabpage. The following statement is not setting the selected item somehow. When I set the item and then when I go back to first tab where the listview is, the listview still doesn't receive the focuse even I set the (listview1.focus()) in tabpage Got_Focuse event procedure. IF listview had received teh focuse, the selected item should be highlighted automatically.
Private Sub ToolStripComboBox2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripComboBox2.SelectedIndexChanged
Dim ICount As Int32 = 0
'**
ListView1.SelectedItems.Clear()
Me.Cursor = Cursors.WaitCursor
For Each LItem As ListViewItem In ListView1.Items
If LItem.SubItems(7).Text = ToolStripComboBox2.SelectedItem.ToString _
Or InStr(LItem.SubItems(7).Text, ToolStripComboBox2.SelectedItem.ToString) > 0 Then
'HERE IS HOW I SET THE X ITEM AS SELECTED IN LISTVIEW
ListView1.Items(ICount).Selected = True
'Here if user selects a song in the list it will be played while Media tab is focused
'
If TabControl2.SelectedTab Is TabPage7 Then '**
Call PlayMusic()
End If
Me.Cursor = Cursors.Default
Exit Sub
End If
ICount += 1
Next
Me.Cursor = Cursors.Default
End Sub
Private Sub TabPage5_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabPage5.GotFocus
Call PopulateSearchBox(ListView1, 7)
ListView1.Focus() 'HERE I SET THE FOCUSE FOR THE LISTVIEW
End Sub
What a curious mind needs to discover knowledge is noting else than a pin-hole.
|
|
|
|
|
Two items come to mind while reviewing the code.
1. The Listview1.SelectedItems.Clear statement in the ToolStripComboBox2_SelectedIndexChanged Sub appears to be unnecessary. If the listview MultiSelect property is set to False as it should be, the user's previous selection will remain selected until a new selection is made and the "ListView1.Items(ICount).Selected = True" statement is executed.
2. What is happening in the PopulateSearchBox procedure that is called in the TabPage5.GotFocus event? Is there something going on in that procedure that is affecting the listview item selection? Since the listview control is one of the parameters for that procedure, it looks suspect.
I think that exhausts my ideas on this issue. The only other suggestion I have is to set an appropriate Breakpoint and walk through the code to hopefully find where the listview selection is being lost.
|
|
|
|
|
Hi,
I want to read a cell-value from an Excel-worksheet in VB.NET 2005 with the following function:
Private Function strCellValue(ByVal strFilname As String, ByVal strWorksheet As String, ByVal strCell As String) As String
Dim dtDataTable As New DataTable
Dim dbConnection As OleDb.OleDbConnection
Dim dbDataAdapter As OleDb.OleDbDataAdapter
dbConnection = New OleDb.OleDbConnection
dbConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; " & "Data Source=" & strFilename & ";" & "Extended Properties='Excel 8.0;HDR=NO'"
dbConnection.Open()
dbDataAdapter = New OleDb.OleDbDataAdapter("Select * From " & "[" & strWorksheet & "$]", dbConnection)
dbDataAdapter.Fill(dtDataTable)
strCellValue = dtDataTable.Rows(intRow(strCell)).Item(intColumn(strCell)).ToString
dbConnection.Close()
End Function
with
Private Function intRow(ByVal strCell As String) As Integer
intRow = CInt(System.Text.RegularExpressions.Regex.Replace(strCell, "[A-Z]", "")) - 1
End Function
and
Private Function intColumn(ByVal strCell As String) As Integer
Dim intI As Integer
Dim strColumn As String
strColumn = System.Text.RegularExpressions.Regex.Replace(strCell, "[0-9]", "")
intColumn = 0
For intI = 0 To strColumn.Length - 1
intColumn += (Asc(Mid(strColumn, strColumn.Length - intI, 1)) - 64) * CInt(Math.Pow(26, intI))
Next
intColumn -= 1
End Function
The last 2 functions converts only the "cell-address" in Excel (e.g. "C4") to the column and row for the data-table (e.g. C==2 and 4==3).
Problem: If a cell-value in Excel is formated as date and the value in this cell is not a date (e.g. the string "Hello"), the value is assumed correct, means strCellValue="Hello" . If the value is a number and so, the cell-value is a valid date (e.g. "2009.03.21"), an empty string is assumed, means strCellValue=""
Any idea why? Thank you very much.
Christian
|
|
|
|
|
Hello,
I have solved the problem myself:
The ConnectionString must modified like this:
dbConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=" & strDateiname & ";" & "Extended Properties='Excel" & _
"8.0;HDR=NO;IMEX=1;'"
The "IMEX=1;" string tells the driver, that it should "convert" mixed formated cell-values (e.g. number, string, date,...) as text.
That's it.
Regards,
Christian
modified on Sunday, March 22, 2009 4:02 AM
|
|
|
|
|
how to make the form as MDI child in visual studio 2008?
|
|
|
|