|
What version of visual basic are you using VB6, VB.net or another?
The code will be slightly different in the way you access the listbox depending on the version.
|
|
|
|
|
Thanks for your reply
Iam using MS VB 2008 EXPRESS EDITION
jaakinye
|
|
|
|
|
Re: Compare data of the last day with the past 4 day’s
I use VB 2008 EXPRESS EDITION
I dont have formular as you can see from this
that is why I need VB eihter throuh array or index to get this
Id A B C D E
15 2 4 6 7 8
14 2 6 8 7 2
for example as above,data A in day 15 repeats its value of day 14 and data D in da 15 also repeats its value of day 14. These are 2 data that repeat their values of day 14.Data A and D are 2 data therefore P1 = 2.
Id A B C D E
15 2 4 6 7 8
13 1 4 1 5 8
The same thing above
P2 (day 15 compared with day 13. Data B in day 15 repeats its value of day 13 and data E at day 15 also repeats its value of day 13.This gives 2 data and therefore P2 = 2
Id A B C D E
15 2 4 6 7 8
12 3 3 3 7 2
As above only 1 data (D) at day 15 repeats its value on day 12.Therefore P3 = 1
Id A B C D E
15 2 4 6 7 8
11 5 6 8 4 1
as above no data at day 15 repeats its value at day 11.Therefore
P4 = 0. Day 15 is the last data and is compared with day 14,13,12and 11.The next day will be day 16 and it will be copared with days 15,14,13and 12 while day 17 will be compared with days 16,15,14 and 13 etc.
How do I use codes to get these?
Thanks for your help
|
|
|
|
|
I assume that your are quite new to VB.net but I may be wrong. I have therfore written the attached code using an array as you asked. I would not say it is the best method but if you are learning they I thought it better.
Create a new project with a form1 and two listboxes (ListBox1 and ListBox2)
Replace the code from your form with the attached code.
It automatically generates test data to show you it working.
It may not be exactly what you want but its a start.
Public Class Form1
Const NumberOfDaystocountBack = 4 'This is a constant for the number of dats you will count back
Dim OriginalDataArray(15, 5) As String 'This is a sample array with 15 records each with 5 pieces of data
Dim ResultsArray(1, 1) As String 'This is a sample array that will store the data, it is resized with the code
Private Sub Main()
'Generate test data
Dim X As Integer
Dim Y As Integer
Dim TempString As String
For X = 15 To 0 Step -1
TempString = ""
For Y = 0 To 4
OriginalDataArray(X, Y) = Int(Rnd() * 10)
TempString = TempString & OriginalDataArray(X, Y).ToString & ", "
Next
ListBox1.Items.Add(TempString)
Next
GenerateResults()
For X = 15 To 0 Step -1
TempString = ""
For Y = 0 To 3
TempString = TempString & ResultsArray(X, Y).ToString & ", "
Next
ListBox2.Items.Add(TempString)
Next
End Sub
Private Sub GenerateResults()
Dim MaxDayCount As Long
Dim LoopCounter As Integer
Dim RecordLoopCounter As Long
Dim DataLoopCounter As Long
Dim DataCount As Integer 'count of the number of data elements
Dim TempArray1() As Integer
Dim TempArray2() As Integer
DataCount = OriginalDataArray.GetUpperBound(1) 'This will be '5' for the sample array given
MaxDayCount = OriginalDataArray.GetUpperBound(0) 'This would be 50 for the sample array given
ReDim ResultsArray(MaxDayCount, NumberOfDaystocountBack) 'Resize the results array to accept data
For RecordLoopCounter = MaxDayCount To 0 Step -1 'Steps back through records of data
'Pass the two days data to the comparison function
For DataLoopCounter = 1 To NumberOfDaystocountBack
ReDim TempArray1(DataCount) 'Resize array to the number of data elements and clears data
ReDim TempArray2(DataCount) 'Resize array to the number of data elements and clears data
If RecordLoopCounter - DataLoopCounter >= 0 Then
For LoopCounter = 0 To DataCount - 1
'Creates two temporary arrays that hold the data for the two days in question
TempArray1(LoopCounter) = OriginalDataArray(RecordLoopCounter, LoopCounter)
TempArray2(LoopCounter) = OriginalDataArray(RecordLoopCounter - DataLoopCounter, LoopCounter)
Next
ResultsArray(RecordLoopCounter, DataLoopCounter - 1) = CompareTwoDays(TempArray1, TempArray2)
Else
ResultsArray(RecordLoopCounter, DataLoopCounter - 1) = "-"
End If
Next
Next
End Sub
Private Function CompareTwoDays(ByVal TodayArray() As Integer, ByVal CompareDayArray() As Integer) As Integer
Dim TodayArraySize As Integer
Dim LoopCounter As Integer
Dim TempCount As Integer
If TodayArray.GetUpperBound(0) <> CompareDayArray.GetUpperBound(0) Then
'Put your error handling here
MsgBox("Cannot compare arrays of different sizes.", MsgBoxStyle.Critical, "Error")
End
End If
TodayArraySize = TodayArray.GetUpperBound(0)
'Loop through array to compare values
For LoopCounter = 0 To TodayArraySize - 1
If TodayArray(LoopCounter) = CompareDayArray(LoopCounter) Then TempCount = TempCount + 1 'or you can use TempCount+=
Next
Return TempCount
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Main()
End Sub
End Class
Good luck
|
|
|
|
|
Hello everybody,
during my first steps with VB2008 I've been facing quite a few challenges... and here I am asking for your support again...
One of my forms holds a "New Folder" button, which simply opens an InputBox to let the user set the new folders name. But if the user doesn't enter any text (or clicks the inputboxes "cancel" button), the underlying form acts as if its own cancel-button had been clicked - it receives the "form_closed" event and closes, too. All I want is to close the inputbox only and leave the major form open!
How can I change this behaviour?
Thanks for any hints
Michael
|
|
|
|
|
Show us the relevant code (using the "code block" button!) so people can help you.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Here it is, containing some dummy stuff. The three routines are part of "frmFolderBrowser", which is basically a form using a tree view control to simulate the windows explorer:
When clicking the "New Folder" button, the InputBox shows:
Private Sub btnNewFolder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNewFolder.Click
If Not SelectedPath Is Nothing Then
Dim NewPath As String = InputBox("New Folder:", "Create Folder")
If NewPath <> "" Then
'Dummy
MsgBox(String.Format("Folder {0} has been created", NewPath))
End If
Else
MsgBox("A path has to be marked!", MsgBoxStyle.Exclamation, "Error")
End If
End Sub
An empty InputBox (OR its cancel button) not only closes the InputBox but also frmFolderBrowser. Maybe there's some mess coming up from "frmFolderBrowser" having its own Cancel-Button, of course:
Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
DlgResult = False
Me.Close() 'supposed to close frmFolderBrowser
End Sub
Clicking the Cancel-Button of the InputBox seems to trigger the Cancel-Button of frmFolderBrowser, too... which I can follow in step mode:
Private Sub frmFolderBrowser_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
frmCenter.Refresh()
End Sub
Hope I could make it any clearer...
|
|
|
|
|
Hi Michael,
I ran a test containing your code, and it behaves the way it should, i.e. whatever way the InputBox gets closed (OK, Cancel, CloseBox) the form remains alive.
I googled a bit but came up empty handed, your problem is not a popular one.
Furthermore I cannot imagine a way to achieve the symptoms you describe, since InputBox is a very simple and completely closed Form, one cannot manipulate the way it behaves or tell it to manipulate anything outside itself.
Is there anything really special you did? wild guesses: installing a native hook, handling your own wndproc or message pump, ...
If you create a new Windows project, single form, and give it basically the above code, does it still go wrong?
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Hi Luc,
thank you for taking the time! And in my beginner's shoes there's nothing like native hooks hidden - or anything of the other tricky stuff you guessed. But it's even good to know that InputBox is a closed form and that it SHOULD behave different even the way I coded it.
I'll follow your advice when I find the time again, try the code in a new project and see if I can find the root of evil in this little bastard
Thank you again!
Michael
|
|
|
|
|
You're welcome.
And good luck with it.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Michael Schäuble wrote: How can I change this behaviour?
I'm wondering what you did to CAUSE that behavior! That is not normal and I suspect something in your code is not written correctly to cause something like this to happen.
|
|
|
|
|
Hi Dave, I'm afraid you're right but have no clue WHAT exactly I did wrong. Just posted some code hoping to show what I did ... maybe you have some minutes for examining? Thank you in advance!
|
|
|
|
|
I have written a tcp server to handle requests, most of which access a database for some sort of information. My question is how do I structure the error handling. Some errors I want to propagate up to the user level and others I want to just handle where they are. For example. If the database in unreachable - I want to wait and then try again but if there is a problem with the retrieved data, I want to send it up to the calling code so I can Send a tcp message back to the client. I am not sure if I should be putting try catches in all my routines (which i dont think is right) or just in specific spots. I am just having trouble figuring out the correct way to do this.
Does anyone know of some good error handling tutorials I could take a look at?? Thank You
modified on Thursday, March 12, 2009 7:38 AM
|
|
|
|
|
captainmogo wrote: If the database in unreachable - I want to wait and then try again but if there is a problem with the retrieved data, I want to send it up to the calling code
This is where you'd throw your exception. The calling code would be running in a Try/Catch block to catch this error. It's the calling code that has to worry about what the problem is and how to deal with it, not the database code.
|
|
|
|
|
ok well my calling code is in a backgroundworker
Class1
Private Sub DoSomething(ByVal sender as Object, ByVal e as DoWorkEventArgs) Handles worker.DoWork
SetTriggerStatus(nam, id, "Started")
Query(start_time, end_time)
....Wait
Query(start_time, end_time)
End Sub
End Class
Module1
Sub SetTriggerStatus(Byval nam as String, ByVal id as Integer, ByVal status as String)
Dim connection as New Client.OracleConnection(My.Settings.OracConnectionString.ToString)
Dim cmd as Client.OracleCommand = Nothing
Try
cmd = New Clinet.OracleCommand("INSERT INTO StatusLog (Name, RuleID, Status) VALUES(:Name,
:RuleID,:Status)", connection)
....set oracleparameters
....execute script
connection.close
Finally
..dispose
End Try
End Sub
End Module
Basically if I get an error connecting to the database in here, I want it to try again. So I would have to put the try/catch around the SetTriggerStatus call from within DoWork? Same with both the query subs?) I also read somewhere that it is not good practice to be putting try/catch blocks in background workers (of course I cant find a good explanation for it now)
|
|
|
|
|
Hi,
here are some guide lines, they should enable you to make good decisions about error handling:
1. only try-catch exceptions your catch can deal with; don't just swallow exceptions to make them go away; always leave some trace of an exception, say in a log file. it makes debugging much harder if you don't.
2. when you want retries, you can but the try-catch inside the loop and swallow exceptions in all but the last iteration. Don't retry forever.
3. background threads typically need to report back to their parent thread, whether success or failure. You shouldn't have a background thread retry forever, or just fail or give up without telling.
4. when your app needs to report failure back to the user, make sure to have both a message the user can understand (and knows what we'd do best then), and the technical information behind it (that could be the low-level exception) should be somewhere, again in a log file.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
I am going to write the errors to a log file because there is not user interface for this app. It is actually going execute on a machine with no monitor. The requirements I was given was to have the the db queries fail on any error but I want to try and handle what I can before saying its failed. say somethign like the db is not available.
Luc Pattyn wrote: . when you want retries, you can but the try-catch inside the loop and swallow exceptions in all but the last iteration. Don't retry forever.
3. background threads typically need to report back to their parent thread, whether success or failure. You shouldn't have a background thread retry forever, or just fail or give up without telling.
This makes sense
so could I use recursion to do a retry. I dont want it to retry it forever... just wait a sec and then try to connect again. If it fails again then exit.
|
|
|
|
|
Hi,
I see no problem in retrying a couple of times, and only report failure if all tries have failed.
I would not actually use recursion for that though, recursion gets abused all too often.
Basically what I'd do is (in C# syntax, VB.NET can do similar things):
for (int tryCount=1; ; tryCount++) {
try {
...
} catch(Exception exc) (
if (tryCount< maxTry) continue;
log(exc.ToString());
throw new ApplicationException("Operation XXX failed after "+maxTry+" tries", exc);
}
}
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Thanks for your help. I used your example and it works well. If it cannot see/find the database after 2 tries, it throws an exception which is caught by the run worker completed event and the rest of the DoWork method is not executed (which is fine since the whole point is to access the database anyway)
Thanks again
|
|
|
|
|
Hello I am trying to create a windows form application with VB 2008 and i am intending to read info from any textbox or whatever and export the data to word I do not know how to do it, I did some research and this is what I found :
Dim oWord As Word._Application()
oWord = New Word.Application()
But is giving me errors the word.application declaration seems to work with older version but not 2008. Please help.
I also found this code but it does not work:
Public Module DriveWord
Public Sub Main()
'Start Word in the background
Dim App As New Word.Application()
App.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone
'Create a new document
Dim Doc As Word._Doucment = App.Documents.Add()
Console.WriteLine()
Console.Writeline("Creating New Document")
Console.Writeline()
'Add a heading and two lines of text
Dim Range As Word.Range = Doc.Paragraphs.Add().Range
Range.InsertBefore ("Test Document")
Range.Style = "Heading 1"
Range = Doc.Paragraphs.Add().Range
Range.InsertBefore("Line one." &vbCrLf & "Line two.")
Range.Font.Bold = True
'Save and close the current document.
Doc.SaveAs(App.StartupPath & "\test.doc")
Doc.Close()
Doc=Nothing
End Sub
regards,
|
|
|
|
|
Hi,
FWIW: I recently decided to create RTF files from C#, foregoing all .NET-to-various-Office-versions troubles. Once an RTF file exists, you can launch it using Process.Start(filename) which launches the default RTF application, i.e. WordPad if no Office is present, Office otherwise. And BTW: RTF is capable of much more than you can edit in WordPad, e.g. I did some table stuff which is visible in WordPad, and works fine (viewing and editing) in Word, any version.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
hai,
i have a xml doc i need the nodes in the xml to be converted to a database table records for example access or oracle or sql. any one know the vb.net code to do this??
|
|
|
|
|
Tons of ways and you'll need to read up on it, because there are entirely to many details to list here and each DB has diff. methods for doing this.
1. You could do an XML reader and handle the import yourself.
2. SQL - you can use SSIS to import the data.
3. You could do an XML transform for the import.
Any suggestions, ideas, or 'constructive criticism' are always welcome.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
|
|
|
|
|
Hi
I am using visual studio 2003. I need to ping every computers in my LAN for my application. For this purpose I have used the "System.Net" namespace's TCP class. Is there any better solution to this problem.
Thanks.
reman
|
|
|
|
|
I'm not certain of the method you're talking about, but chances are the System.Net.NetworkInformation.Ping class will be more verbose and robust
|
|
|
|
|