|
you can use datediff also but timespan is better option depends on the your desired results
try to look at cheat sheets also just for a quick reference , u can also Google cheat sheets
here is one of the reference
http://www.addedbytes.com/cheat-sheets/sql-server-cheat-sheet[^]
T = DateDiff("n", (T1), (T2))
T = TimeSpan.Compare(t1, t2)
Best Of Regards,
SOFTDEV
Sad like books with torn pages, sad like unfinished stories ...
|
|
|
|
|
I have to design, write and test a vb program to help the user solve the "Numbrix" puzzle.
The user should have the capability to "undo" a number of steps taken in order to take another path it he current path is unworkable
so far I had this, I am able to crate a dynamic array of text boxes and load a from
Imports System.IO
Imports System.Math
Public Class Form1
Structure rect
Dim rec(,) As TextBox, val(,) As Byte, yy(,,) As Boolean
Sub disp(ByVal a As Integer, ByVal b As Integer)
Dim st As String = ""
For i As Integer = 1 To 9
If yy(a, b, i) Then st &= i.ToString
Next
rec(a, b).Text = st
End Sub
Sub comp(ByVal a As Integer, ByVal b As Integer)
Dim i, j, cnt As Byte
For i = 1 To 9
If yy(a, b, i) Then
cnt = CByte(cnt + 1)
j = i
End If
Next
If cnt = 1 Then val(a, b) = j
End Sub
End Structure
Dim xx(3, 3) As rect, done As Boolean, diff As Byte
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim pt1, pt2 As Point, rec As Size
rec.Width = 47 : rec.Height = 29
pt1.X = 25 : pt1.Y = 25
For r As Integer = 1 To 3
For c As Integer = 1 To 3
ReDim xx(r, c).rec(3, 3), xx(r, c).val(3, 3), xx(r, c).yy(3, 3, 9)
pt2 = pt1
For rr As Integer = 1 To 3
For cc As Integer = 1 To 3
Dim newtb As New TextBox : Me.Controls.Add(newtb)
newtb.Size = rec : newtb.Multiline = True
newtb.TextAlign = HorizontalAlignment.Center
newtb.Location = pt2 : pt2.X += 46
If c = 2 And (r = 1 Or r = 3) Or r = 2 And (c = 1 Or c = 3) _
Then newtb.BackColor = Color.LightGray
xx(r, c).rec(rr, cc) = newtb
Next
pt2.X = pt1.X : pt2.Y += 28
Next
pt1.X += 140
Next
pt1.X = 25 : pt1.Y += 86
Next
End Sub
Private Sub LoadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadButton.Click
Dim lin(8) As String, filRead As StreamReader, i, x, t, d As Integer
Dim openFile As New OpenFileDialog()
openFile.Filter = "txt.Files(*.txt)|*.txt" openFile.InitialDirectory = "\C:\Fall09\numbrix\numbrix\TextFile1.txt"
If openFile.ShowDialog() = DialogResult.OK Then
MessageBox.Show(openFile.FileName)
End If
filRead = New StreamReader(openFile.FileName)
diff = CByte(filRead.ReadLine())
For r As Integer = 1 To 3
For rr As Integer = 1 To 3
lin = filRead.ReadLine.Split() : i = 0
For c As Integer = 1 To 3
For rc As Integer = 1 To 3
If lin(i) <> "00" Then
x = Integer.Parse(lin(i))
If x > 0 Then xx(r, c).rec(rr, rc).ForeColor = Color.Red
x = Abs(x)
xx(r, c).rec(rr, rc).Text = x.ToString
For j As Integer = CInt(Int(Log10(x))) To 0 Step -1
If j = 0 Then xx(r, c).val(rr, rc) = CByte(x)
t = CInt(10 ^ j)
d = x \ t : x = x Mod t
xx(r, c).yy(rr, rc, d) = True
Next
End If
i += 1
Next
Next
Next
Next
filRead.Close()
End Sub
End Class
|
|
|
|
|
Please use PRE tags (as the forum guidelines dictate) so formatting gets preserved, lines get indented, and code becomes readable.
BTW: you can edit the existing message to fix this.
|
|
|
|
|
I don't see a question here.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
help the user solve the "Numbrix" puzzle is the question
thanks
|
|
|
|
|
If this is a reply to me, why did you reply to yourself ?
You seem to be asking us to do your homework. I'm not even sure what point you're stuck on, just that you posted a lot of code. If you have a specific question, you can ask it, posting relevant code. We are not going to do your homework for you tho, you will not learn that way
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
can u xplain what you're running into a problem with ?
Best Of Regards,
SOFTDEV
Sad like books with torn pages, sad like unfinished stories ...
|
|
|
|
|
when ever I click the next button it has to look for possible answers to slove the puzzle. I don't now how to search for the next numder
Thanks
Lakshmi
|
|
|
|
|
Hi,
I'm fairly new to vb and have managed quite a lot so far but the part I'm stuck on is adding a line number to my text file.
On my form the user submits information entered into a textbox - this line is appended to a text file log . I would like to be able to add line numbers to it as each time text is sent from my textbox, so when printed it would show how many events were recorded.
I appreciate any help in this I have searched for months now with no real help found.
thanks for any help you may provide.
Regards
CJ
modified on Monday, September 14, 2009 6:26 AM
|
|
|
|
|
cjdc wrote: I have searched for months now
Really ?
What is the number you want to append exactly ? The number of lines of text in the file ? The line count would be a line itself, right ? you can use file.ReadAllLines to read the file into an array and check it's length to see how many lines it has
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
the line number would have to be incremental as each time an event from my textbox is logged into my text file I would like a line number added to the log, this is where I'm stuck and probably why its taking me months to do what may be a simple task, but I'm still learning.
|
|
|
|
|
OK, well, then, put the line count at the end. Use File.ReadAllLines to read all the lines. Then write a new text file that contains all the lines but the last one. You can use int.TryParse to parse the number, or just work it out and write it again.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Thanks for that but now confused as to why create a new file to transfer existing data?
here's what I have so far:
' This text is always added, making the file longer over time
sw = IO.File.AppendText(path)
sw.WriteLine(DateTime.Now.ToString() + "," + "Reason," + (RichTextBox2.Text)
so where I'm using writeline i would need it to add a incremental line number first, so if the log already had two entries it would need to put a number 3 before datetime.now
Sorry if I'm not explaining it very well.
thanks for replying
|
|
|
|
|
cjdc wrote: Thanks for that but now confused as to why create a new file to transfer existing data?
Because there's no way to remove the old line otherwise. In fact 'AppendText' is loading the entire file and rewriting it, it's just hiding it from you.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Hi,
assuming your textbox is initially empty and lines a are going to be added one by one, this simple method would add one line including a line number:
Private Dim lineNumber As Integer = 0
Private Sub AddString(s As String)
lineNumber=lineNumber+1
myTextBox.Text=myTextBox.Text & lineNumber.ToString() & " : " & s & Environment.NewLine
End Sub
|
|
|
|
|
Hi Luc,
Thanks for that you seem to understand part of this problem well.
Here's what I have so far:
' This text is always added, making the file longer over time
sw = IO.File.AppendText(path)
sw.WriteLine(DateTime.Now.ToString() + "," + "Reason," + (RichTextBox2.Text)
so where I'm using writeline I would need it to add a incremental line number first, so if the log already had two entries it would need to put a number 3 before datetime.now
If i use your code it writes a line number but does not increment the number.
The form already creates the text file if it does not exist, and on each use will write a new line entry.
The user will use this form to report technical issues, so there could be many issues per day, I would like each issue logged with an incremental line number, so would it have to count existing entries to establish the correct line number to write? if so how would I get it to write incremental numbers and check to see how many entries already existed.
I hope I'm explaining this right or it might be easier to send you my project so you can see what goes on.
Thanks again for helping and taking time to reply.
Regards
CJ
|
|
|
|
|
Hi,
I missed the fact the file could preexist. And yes my code increments the lines generated, however it starts at 1.
if you need to append to an existing file, you have to figure the number of lines present; there are two rather safe ways to do that:
1. read through the entire file, line by line, and count them.
2. read through the entire file, keep the last line, isolate the line number and parse it.
Other ways would include:
3. keep a copy of the last line number somewhere (in registry, in another file, ...)
4. not to use line numbers, use something else than increases all the time (but not by 1), example:
the number yyyyMMddHHmmss containing numeric parts of date and time.
Whatever you do with files, chances are it will fail if and when your app is running more than once at a time on a single computer. And also when it runs on several machines.
The solution, if relevant, is to use a database, where each app would add records to a table, with one of the fields made a primary key, unique and auto-incrementing.
|
|
|
|
|
You have to append a text-line (that includes for each line an incremental number) to a pre-existing file, not to append a line number to a text file!
Is it correct?
Do you need to add a new line to the file, each time you input a new line, or before you want to input several lines and then to append them at the end of work?
How many lines do you preview to write in the log file (maximum), and how many characters have each line (maximum)?
modified on Thursday, September 10, 2009 2:30 PM
|
|
|
|
|
Hi Hurricane3000,
Thanks for the input, I'm so confused probably why it's taking me months to try to resolve this problem.
I have already managed to get the text file made and written to from my text box. Each time an entry will goto a newline.
For example :the log file is created on first entry if it does not exist...
A report of one line is submitted from my textbox to the text file and is added to and a new file will be created monthly.
Each entry in my text box is submitted one at a time into the same text file but on a new line.
Every entry is written to a newline I have got this part working.
The form is shared between 2 technicians reporting various faults but writing to the same file within a network, I have got this part working too, without any issues or access problems.
My issue is I would like to add line numbers as the file is written to for each logged entry
I'm grateful for any assistance in resolving this battle, my vb is limited but I some how manage.
regards
CJ
|
|
|
|
|
You not answered to my questions yet ,
You have to append a text-line (that includes for each line an incremental number) to a pre-existing file, not to append a line number to a text file!
Is it correct?
Do you need to add a new line to the file, each time you input a new line, or before you want to input several lines and then to append them at the end of work?
How many lines do you preview to write in the log file (maximum), and how many characters have each line (maximum)?
Can you send me a sample of a text-line that you need to append?
|
|
|
|
|
HI Hurricane3000,
Perhaps I mean to add an inc remental number as the lines are sent to the text file so that the line numbers are counted and added to as the file grows.
There is no limit to the max characters typed per line but usually is just a brief explanation of the issue
here's what I have so far:
' This text is always added, making the file longer over time
sw = IO.File.AppendText(path)
sw.WriteLine(DateTime.Now.ToString() + "," + "Reason," + (RichTextBox2.Text)
so where I'm using writeline i would need it to add a incremental line number first, so if the log already had two entries it would need to put a number 3 before datetime.now, so I think it would somehow have to count any existing lines in my text file.
My text file at present is as follows:
IT Report Log File Started,10/09/2009 02:03:53
**********************************************
10/09/2009 18:32:27,Reason,test,Approx Time,Duration
10/09/2009 18:32:33,Reason,pc would not power on,Approx Time,Duration
So as each line is made I would like it to be numbered before the date entry.
to continue as follows:
IT Report Log File Started,10/09/2009 02:03:53
**********************************************
1. 10/09/2009 18:32:27,Reason,test,Approx Time,Duration
2. 10/09/2009 18:32:33,Reason,pc would not power on,Approx Time,Duration
Then when a new entry is submitted from my textbox it will write line number 3 and so on
If this doesn't help I could sent you my project
Regards
CJ
modified on Thursday, September 10, 2009 4:26 PM
|
|
|
|
|
1)How many lines will contains the file?
2)Normally, how many lines do you want to add (in the file) at same time? 1 or more than 1?
|
|
|
|
|
Hi Hurricane3000,
1. The file can contain upto 200 or 300 lines max dependant on issues reported that month
2. Only 1 line at a time will be added to the file
The file will take about a month too reach 300 lines some files only reach 160 or so.
regards
CJ
|
|
|
|
|
Hi,
Assuming that your problem is ONLY that to add a progressive number for each new line added in the file,your problem is more easy to resolve than you think.
You simply must go in the optical vision of what you want obtain(you have not much fantasy)!
Like already told you by Luc Pattyn, there are several way to obtain that you want.
For your case I suggest you this:
1) You have to read the entire file and count the lines in it contained.
Following code can do this work. At the end, CountLines will contains the number of lines already contained in the file.
Note: If the head of file will contains not numbered lines, you'll have to subtract them from CountLines.
Using sr As StreamReader = New StreamReader(myStream) 'mystream = Path of File
Dim line As String
Dim CountLines As Integer = 0
Do
line = sr.ReadLine()
If line = Nothing Then Exit Do
CountLines = CountLines + 1
Loop Until line Is Nothing
sr.Close()
CountLines = CountLines - 4 'The number 4 indicate eventually first lines to subtract.
End Using
2) To append a new line in the file, you simply have to change your code-line:
sw.WriteLine(DateTime.Now.ToString() + "," + "Reason," + (RichTextBox2.Text) to
CountLines = CountLines + 1
sw.WriteLine(CountLines.ToString + " , " + DateTime.Now.ToString() + "," + "Reason," + (RichTextBox2.Text) Note that code I sent you, close the file after read it. Then you have to re-open it.
And also note that the code will have no effect on lines already written, but only on newest.
May be that you'll need to modify the "scope-visibility" of CountLines.
Hope this help you
Ignazio
modified on Friday, September 11, 2009 3:42 PM
|
|
|
|
|
Hi Ignazio,
Many, many thanks for the brain refresh. I had grown tired took the weekend of to refresh.
Well thank sfor the code and a few mods and it works swell, here's what I have now and it probably could be shorter than what I have but I'm still learning. See what you think:
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
If ComboBox1.SelectedItem = "" Then
Me.Hide()
Dialog1.ShowDialog()
Return
Else
End If
Dim fso
Dim fol As String
fol = "\\Sv1\technician\IT report"
fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(fol) Then
fso.CreateFolder(fol)
MsgBox(" Folder created", MsgBoxStyle.OkOnly, "Folder Created")
Else
MsgBox(" Log File Updated", MsgBoxStyle.OkOnly, "Log Updated")
End If
Dim path As String = "\\Sv1\technician\IT report\IT Report Log.txt"
Dim sw As IO.StreamWriter
If IO.File.Exists(path) = False Then
sw = IO.File.CreateText(path)
sw.WriteLine(Date.Now.ToString() + "," + ("IT Report Log File Started"))
sw.Flush()
sw.Close()
IO.File.SetCreationTime(path, Date.Now())
End If
Using sv As IO.StreamReader = New IO.StreamReader(path)
Dim CountLines As Integer = 0
Dim line As String
Do
line = sv.ReadLine()
If line = Nothing Then Exit Do
CountLines = CountLines + 1
Loop Until line Is Nothing
sv.Close()
CountLines = CountLines - 1 'The number 1 indicate eventually first lines to subtract.
sw = IO.File.AppendText(path)
CountLines = CountLines + 1
sw.WriteLine(CountLines.ToString + ". " + DateTime.Now.ToString() + " " + (ComboBox1.Text) + "," + (TextBox1.Text) + "," + (ComboBox2.Text) + "," + "Reported by," + (ComboBox3.Text) + "," + (DateTimePicker1.Text))
End Using
sw.Flush()
sw.Close()
Dim sr As IO.StreamReader = IO.File.OpenText(path)
Dim s As String
Do While sr.Peek() >= 0
s = sr.ReadLine()
Console.WriteLine(s)
Loop
ComboBox1.SelectedIndex = -1
ComboBox2.SelectedIndex = -1
ComboBox3.SelectedIndex = -1
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
sr.Close()
End Sub
Once again thanks to all that help with this one.
Best regards
CJ
|
|
|
|
|