|
Here's a better solution for when all files have headers.
Dim theDir As DirectoryInfo = new DirectoryInfo(iFile);
Dim datFiles As FileInfo() = theDir.GetFiles("*.txt");
Using sw As StreamWriter = New StreamWriter(oFile, True, System.Text.Encoding.Default)
'Read and write first file including headers
Dim fi As FileInfo = datFiles(0)
Using sa As StreamReader = New StreamReader(fi.FullName, System.Text.Encoding.Default)
'read input line
'write output line
End Using
' Read and write subsequent files excluding headers
For i As Integer = 1 To datFiles.Length()-1
fi = datFiles(i)
Using sa As StreamReader = New StreamReader(fi.FullName, System.Text.Encoding.Default)
'read input line
If 'header line' Then
' skip header line - do nothing
Else
'write output line
End If
End Using
Next i
End Using
Much neater and gets rid of awful skipHeaders flag.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
How is it possible to do a read befor declaring the streamreader???
Using sw As StreamWriter = New StreamWriter(oFile, True, System.Text.Encoding.Default)
'Read and write first file including headers
Dim fi As FileInfo = datFiles(0)
Using sa As StreamReader = New StreamReader(fi.FullName, System.Text.Encoding.Default)
'read input line
'write output line
End Using
|
|
|
|
|
'Read and write first file including headers
is a comment - if you read the code you'll see that it does declare sa at the appropriate place.
The comments in the using blocks do need to be replaced by code.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
|
Here's an annotated version of your code.
You need to think about the logic of the code rather than just hacking.
Roughly it is something like this NOTE this is not VB it's just looks a bit like VB:
Fill the pf array
Using streamreader for first file
read and write two header lines
Loop while data lines
read and write data line
End Loop
End Using 'Closes streamreader
For second and subsequent files in folder
Using streamreader for first file
Loop while input lines
read input line
If header line Then
'do nothing
else
'write data line
End If
End Loop
End Using 'Closes streamreader
Next
Unless you get the logic clear you'll keep stumbling around wondering why things don't work or give the output you expect.
'-------------------------------------------------------------
'Change the parameter name iFile to something more meaningful
'It's not the file name but the folder name
'--------------------------------------------------------------
Private Sub AppendFiles(ByVal iFile As String, ByVal oFile As String)
Dim temp As String
Dim name As String
Dim comp As String
Dim desF As String
Dim desAF As String
Dim desA As String
Dim desAA As String
Dim ecn As String
Dim dnf As String
Dim repDNF As String
Dim repASM As String
Dim pF(10) As Integer
Dim NbOfSpaces As Integer = 0
Dim theDir As DirectoryInfo = New DirectoryInfo(iFile)
Dim datFiles As FileInfo() = theDir.GetFiles("*.txt")
Using sw As StreamWriter = New StreamWriter(oFile, True, System.Text.Encoding.Default)
'--------------------------------------------------------------------------------------------
' Call FillPF sub here, or move the code marked XXXX to here (I've done that). You don't need any files to do what it does
'--------------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------------
'XXXX Why is this being done 0 to 10 times? All it does is assign entries to the pf Array
'that's why I did it as a sub called FillPF() and got rid of the loop and it can be done above
For i = 0 To 10
pF(0) = temp.IndexOf("Nom du Modèle")
pF(1) = temp.IndexOf("AEC_COMPATIBILITY")
pF(2) = temp.IndexOf("AEC_STANDARD_DESCRIPTION_FRENCH")
pF(3) = temp.IndexOf("AEC_FREE_DESCRIPTION_FRENCH")
pF(4) = temp.IndexOf("AWW_STANDARD_DESCRIPTION")
pF(5) = temp.IndexOf("AEC_FREE_DESCRIPTION_ENGLISH")
pF(6) = temp.IndexOf("AEC_ECN")
pF(7) = temp.IndexOf("DNF")
pF(8) = temp.IndexOf("REP_DNF")
pF(9) = temp.IndexOf("REP_ASM")
Next
'---------------------------------------------------------------------------------------------
'Read and write first file including headers
'*** iFile is the path, we've been through this before; StreamReader reads a file it can't read a folder
'*** Change the name to something like inputPath and it will be less misleading.
Using sa As StreamReader = New StreamReader(iFile, System.Text.Encoding.Default)
'-----------------------------------------------------------------------
'This block reads and writes the two header lines in the first file
'-----------------------------------------------------------------------
temp = sa.ReadLine()
name = temp.Substring(0, pF(1))
comp = temp.Substring(pF(1), pF(2) - pF(1))
desF = temp.Substring(pF(2), pF(3) - pF(2))
desAF = temp.Substring(pF(3), pF(4) - pF(3))
desA = temp.Substring(pF(4), pF(5) - pF(4))
desAA = temp.Substring(pF(5), pF(6) - pF(5))
ecn = temp.Substring(pF(6), pF(7) - pF(6))
dnf = temp.Substring(pF(7), pF(8) - pF(7))
repDNF = temp.Substring(pF(8), pF(9) - pF(8))
repASM = temp.Substring(pF(9), temp.Length - pF(9))
sw.WriteLine(name & comp & desF & desAF & desA & desAA & ecn & dnf & repDNF & repASM & "CREATED_DATE")
sw.WriteLine(sa.ReadLine())
'-----------------------------------------------------------------------
End Using
'------------------------------------------------------------------------------------------
' This opens the first file for reading, you can't write the headers until you've done this
' and you have to take into account the two lines you attempt to read above
'i.e. you must read and write them before the loop reads the data lines
'So you need to move these two lines to before the block that reads the header lines.
'Also get rid of the Using and End Using above.
'------------------------------------------------------------------------------------------
Dim fi1 As FileInfo = datFiles(0)
Using sa As StreamReader = New StreamReader(fi1.FullName, System.Text.Encoding.Default)
'------------------------------------------------------------------------------------------
While sa.Peek() >= 0
temp = sa.ReadLine()
name = temp.Substring(0, pF(1))
comp = temp.Substring(pF(1), pF(2) - pF(1))
desF = temp.Substring(pF(2), pF(3) - pF(2))
desAF = temp.Substring(pF(3), pF(4) - pF(3))
desA = temp.Substring(pF(4), pF(5) - pF(4))
desAA = temp.Substring(pF(5), pF(6) - pF(5))
ecn = temp.Substring(pF(6), pF(7) - pF(6))
dnf = temp.Substring(pF(7), pF(8) - pF(7))
repDNF = temp.Substring(pF(8), pF(9) - pF(8))
repASM = temp.Substring(pF(9), temp.Length - pF(9))
sw.WriteLine(name & comp & desF & desAF & desA & desAA & ecn & dnf & repDNF & repASM & Created)
End While
End Using
' Read and write subsequent files excluding headers
For i As Integer = 1 To datFiles.Length() - 1
fi1 = datFiles(i)
Using sa As StreamReader = New StreamReader(fi1.FullName, System.Text.Encoding.Default)
'read input line
While sa.Peek() >= 0
temp = sa.ReadLine()
name = temp.Substring(0, pF(1))
comp = temp.Substring(pF(1), pF(2) - pF(1))
desF = temp.Substring(pF(2), pF(3) - pF(2))
desAF = temp.Substring(pF(3), pF(4) - pF(3))
desA = temp.Substring(pF(4), pF(5) - pF(4))
desAA = temp.Substring(pF(5), pF(6) - pF(5))
ecn = temp.Substring(pF(6), pF(7) - pF(6))
dnf = temp.Substring(pF(7), pF(8) - pF(7))
repDNF = temp.Substring(pF(8), pF(9) - pF(8))
repASM = temp.Substring(pF(9), temp.Length - pF(9))
'-------------------------------------------------------------------------
' Is this test right? It means the second line in the file will be written
' out. But will only be ------------------------------------ line.
'-------------------------------------------------------------------------
If name.Contains("Nom du Modélé") Then
' skip header line - do nothing
Else
'write output line
sw.WriteLine(name & comp & desF & desAF & desA & desAA & ecn & dnf & repDNF & repASM & created)
End If
End While
End Using
Next i
End Using
'------------------------------------------------------------
'Where did the End Sub go to? Is there more code after this?
'------------------------------------------------------------
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Thanks for the explanations of the code.
Will try and let u know...
Thanks once again...
|
|
|
|
|
Good Morning David,
My question is how to call the pF() without reading the file.
Private Sub FillPF(ByVal pF() As Integer, ByVal temp As String)
pF(0) = temp.IndexOf("Nom du Modèle")
pF(1) = temp.IndexOf("AEC_COMPATIBILITY")
pF(2) = temp.IndexOf("AEC_STANDARD_DESCRIPTION_FRENCH")
pF(3) = temp.IndexOf("AEC_FREE_DESCRIPTION_FRENCH")
pF(4) = temp.IndexOf("AWW_STANDARD_DESCRIPTION")
pF(5) = temp.IndexOf("AEC_FREE_DESCRIPTION_ENGLISH")
pF(6) = temp.IndexOf("AEC_ECN")
pF(7) = temp.IndexOf("DNF")
pF(8) = temp.IndexOf("REP_DNF")
pF(9) = temp.IndexOf("REP_ASM")
End Sub
Call pF(pF,temp)
Before calling i should read the line using temp variable like "temp=sa.ReadLine()"
Since streamreader comes after this line,its not possible to call pF()
Modified version of the Code:
Private Sub AppendFiles(ByVal iFile As String, ByVal oFile As String)
Dim temp As String
Dim name As String
Dim comp As String
Dim desF As String
Dim desAF As String
Dim desA As String
Dim desAA As String
Dim ecn As String
Dim dnf As String
Dim repDNF As String
Dim repASM As String
Dim pF(10) As Integer
Dim NbOfSpaces As Integer = 0
Dim inputline As String
Dim theDir As DirectoryInfo = New DirectoryInfo(iFile)
Dim datFiles As FileInfo() = theDir.GetFiles("*.txt")
Using sw As StreamWriter = New StreamWriter(oFile, True, System.Text.Encoding.Default)
'--------------------------------------------------------------------------------------------'
'Call FillPF sub here, or move the code marked XXXX to here (I've done that). You don't need any files to do what it does
'inputline = sa.readline()
Call FillPF(pF, inputline)
'--------------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------------
'Read and write first file including headers
'*** iFile is the path, we've been through this before; StreamReader reads a file it can't read a folder
'*** Change the name to something like inputPath and it will be less misleading.
'Using sa As StreamReader = New StreamReader(inputfile, System.Text.Encoding.Default)
Dim fi1 As FileInfo = datFiles(0)
Using sa As StreamReader = New StreamReader(fi1.FullName, System.Text.Encoding.Default)
'-----------------------------------------------------------------------
'This block reads and writes the two header lines in the first file
'-----------------------------------------------------------------------
temp = sa.ReadLine()
name = temp.Substring(0, pF(1))
comp = temp.Substring(pF(1), pF(2) - pF(1))
desF = temp.Substring(pF(2), pF(3) - pF(2))
desAF = temp.Substring(pF(3), pF(4) - pF(3))
desA = temp.Substring(pF(4), pF(5) - pF(4))
desAA = temp.Substring(pF(5), pF(6) - pF(5))
ecn = temp.Substring(pF(6), pF(7) - pF(6))
dnf = temp.Substring(pF(7), pF(8) - pF(7))
repDNF = temp.Substring(pF(8), pF(9) - pF(8))
repASM = temp.Substring(pF(9), temp.Length - pF(9))
sw.WriteLine(name & comp & desF & desAF & desA & desAA & ecn & dnf & repDNF & repASM & "CREATED_DATE")
sw.WriteLine(sa.ReadLine())
'-----------------------------------------------------------------------
'End Using
'------------------------------------------------------------------------------------------
' This opens the first file for reading, you can't write the headers until you've done this
' and you have to take into account the two lines you attempt to read above
'i.e. you must read and write them before the loop reads the data lines
'So you need to move these two lines to before the block that reads the header lines.
'Also get rid of the Using and End Using above.
'------------------------------------------------------------------------------------------
'Dim fi1 As FileInfo = datFiles(0)
'Using sa As StreamReader = New StreamReader(fi1.FullName, System.Text.Encoding.Default)
'------------------------------------------------------------------------------------------
While sa.Peek() >= 0
temp = sa.ReadLine()
name = temp.Substring(0, pF(1))
comp = temp.Substring(pF(1), pF(2) - pF(1))
desF = temp.Substring(pF(2), pF(3) - pF(2))
desAF = temp.Substring(pF(3), pF(4) - pF(3))
desA = temp.Substring(pF(4), pF(5) - pF(4))
desAA = temp.Substring(pF(5), pF(6) - pF(5))
ecn = temp.Substring(pF(6), pF(7) - pF(6))
dnf = temp.Substring(pF(7), pF(8) - pF(7))
repDNF = temp.Substring(pF(8), pF(9) - pF(8))
repASM = temp.Substring(pF(9), temp.Length - pF(9))
sw.WriteLine(name & comp & desF & desAF & desA & desAA & ecn & dnf & repDNF & repASM & Created)
End While
End Using
' Read and write subsequent files excluding headers
For i As Integer = 1 To datFiles.Length() - 1
fi1 = datFiles(i)
Using sa As StreamReader = New StreamReader(fi1.FullName, System.Text.Encoding.Default)
'read input line
While sa.Peek() >= 0
temp = sa.ReadLine()
name = temp.Substring(0, pF(1))
comp = temp.Substring(pF(1), pF(2) - pF(1))
desF = temp.Substring(pF(2), pF(3) - pF(2))
desAF = temp.Substring(pF(3), pF(4) - pF(3))
desA = temp.Substring(pF(4), pF(5) - pF(4))
desAA = temp.Substring(pF(5), pF(6) - pF(5))
ecn = temp.Substring(pF(6), pF(7) - pF(6))
dnf = temp.Substring(pF(7), pF(8) - pF(7))
repDNF = temp.Substring(pF(8), pF(9) - pF(8))
repASM = temp.Substring(pF(9), temp.Length - pF(9))
'-------------------------------------------------------------------------
' Is this test right? It means the second line in the file will be written
' out. But will only be ------------------------------------ line.
'-------------------------------------------------------------------------
If name.Contains("Nom du Modélé") Then
' skip header line - do nothing
ElseIf name.Contains("------") Then
Else
'write output line
sw.WriteLine(name & comp & desF & desAF & desA & desAA & ecn & dnf & repDNF & repASM & Created)
End If
End While
End Using
Next i
End Using
'------------------------------------------------------------
'Where did the End Sub go to? Is there more code after this?
End Sub
modified on Wednesday, May 6, 2009 3:35 AM
|
|
|
|
|
That was an aberration on my part - it struck me that it was wrong late last night.
I've had a think about it and there's a problem if AppendFiles is called twice in same program or the program is run when the output file already exists. You'll get the headers again on second call/run.
However, I awoke early this morning and produced this code which handles the problem.
I've not tested it but think the logic is correct. It's done as a console app and this is the complete program.
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Try
Call AppendFiles("C:\thePathToFiles\", "C:\PlayPen\AppendedJunk.txt")
Call AppendFiles("C:\WayToOtherFiles\", "C:\PlayPen\AppendedJunk.txt")
Catch ex As Exception
MsgBox("ERROR: " & ex.Message(), CType(MsgBoxStyle.OkOnly, MsgBoxStyle), "APPEND FILES FAILED")
End Try
End Sub
Private Sub AppendFiles(ByVal inputPath As String, ByVal outputFile As String)
Dim columnPositions(10) As Integer
Dim theDir As DirectoryInfo = New DirectoryInfo(inputPath)
Dim theFiles As FileInfo() = theDir.GetFiles("*.txt")
'If the output file exists it already has headers, so we only need to do headers if it does not exist
Dim doHeaders As Boolean = Not File.Exists(outputFile)
Using sw As StreamWriter = New StreamWriter(outputFile, True, System.Text.Encoding.Default)
For fileCount As Integer = 0 To theFiles.Length() - 1
Dim inputFile As String = theFiles(fileCount).FullName
Dim createdAt As Date = theFiles(fileCount).CreationTime()
Dim inputLine As String
Dim outputLine As String
Using sr As StreamReader = New StreamReader(inputFile, System.Text.Encoding.Default)
If (fileCount = 0) Then
'Read the first line of first file to set up column positions array
inputLine = sr.ReadLine()
Call GetColumnPostions(columnPositions, inputLine)
If doHeaders Then
Call WriteHeaders(sr, sw, columnPositions)
Else
'Read and throw away second header line
inputLine = sr.ReadLine()
End If
Else
' Read the header lines and throw them away
For i As Integer = 1 To 2 'Assumes 2 header lines, as does WriteHeaders
inputLine = sr.ReadLine()
Next
End If
'Now read and write the data lines
While sr.Peek() >= 0
inputLine = sr.ReadLine()
outputLine = GetOutputLine(inputLine, createdAt, columnPositions)
sw.WriteLine(outputLine)
End While
End Using
Next fileCount
End Using
End Sub
Private Sub GetColumnPostions(ByVal colPos As Integer(), ByVal s As String)
colPos(0) = s.IndexOf("Nom du Modèle")
colPos(1) = s.IndexOf("AEC_COMPATIBILITY")
colPos(2) = s.IndexOf("AEC_STANDARD_DESCRIPTION_FRENCH")
colPos(3) = s.IndexOf("AEC_FREE_DESCRIPTION_FRENCH")
colPos(4) = s.IndexOf("AWW_STANDARD_DESCRIPTION")
colPos(5) = s.IndexOf("AEC_FREE_DESCRIPTION_ENGLISH")
colPos(6) = s.IndexOf("AEC_ECN")
colPos(7) = s.IndexOf("DNF")
colPos(8) = s.IndexOf("REP_DNF")
colPos(9) = s.IndexOf("REP_ASM")
End Sub
Private Sub WriteHeaders(ByVal sa As StreamReader, ByVal sw As StreamWriter, ByVal p As Integer())
Dim temp As String = sa.ReadLine()
Dim sb As StringBuilder = BuildLine(temp, p)
sb.Append("CREATED_DATE")
sw.WriteLine(sb.ToString()) 'First header line
sw.WriteLine(sa.ReadLine()) 'Second header line
End Sub
Private Function GetOutputLine(ByVal s As String, ByVal cd As Date, ByVal p As Integer()) As String
Dim sb As StringBuilder = BuildLine(s, p)
sb.Append(cd.ToString()) 'creation date
GetOutputLine = sb.ToString()
End Function
Private Function BuildLine(ByVal s As String, ByVal p As Integer()) As StringBuilder
Dim sb As StringBuilder = New StringBuilder("")
sb.Append(s.Substring(0, p(1))) 'name
sb.Append(s.Substring(p(1), p(2) - p(1))) 'comp
sb.Append(s.Substring(p(2), p(3) - p(2))) 'desF
sb.Append(s.Substring(p(3), p(4) - p(3))) 'desAF
sb.Append(s.Substring(p(4), p(5) - p(4))) 'desA
sb.Append(s.Substring(p(5), p(6) - p(5))) 'desAA
sb.Append(s.Substring(p(6), p(7) - p(6))) 'ecn
sb.Append(s.Substring(p(7), p(8) - p(7))) 'dnf
sb.Append(s.Substring(p(8), p(9) - p(8))) 'repDNF
sb.Append(s.Substring(p(9), s.Length - p(9))) 'repASM
BuildLine = sb
End Function
End Module
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
Thanks a lot for supporting me so much...
You have spent ur precious time in helping me.
I have never seen a person who helps somebody who is not know well before.
The below is the sample of the output i got from ur previous version of the code:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------CREATED_DATE
01530501.ASM -- UNITE CARROSSERIE CARROSSERIE SRT2 MF HOOD HOOD SRT2 MF F9000507
01530501_SKEL.PRT -- -- 530 UNIT SKEL UNIT SKEL -- F999999 -- -- -- 06/05/2009 10:24:16
4281484.PRT M1 SUPPORT CARROSSERIE SRT2 SUPPORT INF GBA15 & CVT-ML160 SRT2 HOOD SUPPORT LOWER SUPPORT GBA15 & CVT-ML160 F8546130 12289 A -- 06/05/2009 10:24:16
3009508X1.PRT -- -- VIS H, M12x40, 8.8 VIS A TETE HEXAGONALE -- -- 12289 B -- 06/05/2009 10:24:16
.
.
.
And on more question:
If the field spacing ie., GetColumnPosition() varies for each file how to handle it?
Is it possible to rearange the position, if the second files position of the fields are higher than the previous one?
Can the rearranging of the file be done based on the field position with more spacing file?
Thanks once again for helping me...
|
|
|
|
|
I have modified the code to write the first header line as follows and it works fine.
If (fileCount = 0) Then
'Read the first line of first file to set up column positions array
inputLine = sr.ReadLine()
sw.WriteLine(inputLine) ' first header line
Call GetColumnPostions(columnPositions, inputLine)
If doHeaders Then
Call WriteHeaders(sr, sw, columnPositions)
Else
'Read and throw away second header line
inputLine = sr.ReadLine()
End If
Else
.
.
Private Sub WriteHeaders(ByVal sa As StreamReader, ByVal sw As StreamWriter, ByVal p As Integer())
Dim temp As String = sa.ReadLine()
Dim sb As StringBuilder = BuildLine(temp, p)
'sb.Append("CREATED_DATE")
sw.WriteLine(sb.ToString())
'Second header line
'First header line
'sw.WriteLine(sa.ReadLine())
End Sub
Thanks for the code.
|
|
|
|
|
Here's my version of how to write headers and change column spacing.
I realised there was a flaw in WriteHeaders() since the code that calls it had already read the first line.
Using sr As StreamReader = New StreamReader(inputFile, System.Text.Encoding.Default)
If (fileCount = 0) Then
'Read the first line of first file to set up column positions array
inputLine = sr.ReadLine()
Call GetColumnPostions(columnPositions, inputLine)
If doHeaders Then
Call WriteHeaders(inputLine, sr, sw, columnPositions)
Else
'Read and throw away second header line
inputLine = sr.ReadLine()
End If
Else
'Read the first line of file to set up column positions array
'NOTE: This could produce columns that don't line up properly
inputLine = sr.ReadLine()
Call GetColumnPostions(columnPositions, inputLine)
' Read the remaining header lines and throw them away
For i As Integer = 2 To 2 'Assumes 2 header lines, as does WriteHeaders
inputLine = sr.ReadLine()
Next
End If
'Now read and write the data lines
While sr.Peek() >= 0
inputLine = sr.ReadLine()
outputLine = GetOutputLine(inputLine, createdAt, columnPositions)
sw.WriteLine(outputLine)
End While
End Using
Private Sub WriteHeaders(ByVal temp As String, ByVal sa As StreamReader, ByVal sw As StreamWriter, ByVal p As Integer())
Dim sb As StringBuilder = BuildLine(temp, p)
sb.Append("CREATED_DATE")
sw.WriteLine(sb.ToString()) 'First header line
sw.WriteLine(sa.ReadLine()) 'Second header line
End Sub
Not sure if this is what you meant by changing column positions. And it could produce output where columns don't line up properly.
The problem is that if, for example, the column positions in second file are greater than in first file you can't go back and re-write the output that's already been done. If they are less than in first file then the columns won't line up.
One way to make sure columns line up would be to change GetColumnPositions to use hard coded values (or pick them up from a file that is not one of the data files). That would mean you would not have to do a readline before calling GetColumnPositions, which would allow you to use the original WriteHeaders() that reads the first line.
How does your code write "CREATED_DATE" as column head?
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
thanks for the next varsion.
I will try ur code and let u know.
this is how i wrote "CREATED_DATE" in the header line:
inputLine = sr.ReadLine()
sw.WriteLine(inputLine & Space(5) & "CREATED_DATE") ' first header line
|
|
|
|
|
It works as you said...
The header lines are written only for the first file.
Its difficult to determine the column position,because there are n number of files and hardcoding will not help in that case.
Anyways thanks for all the support and patience u had to help me.
|
|
|
|
|
Using VBScript in .NET code?? I seriously have to question your friend's abilities.
|
|
|
|
|
Can i ask what must i enter to check if my port is open? Cause port.open = true is not able to use in this case. thanks.
|
|
|
|
|
what about
serialport.isopen = true
sorry i see someone just beet me to it
modified on Monday, May 4, 2009 4:30 AM
|
|
|
|
|
in .net its
SerialPort1.IsOpen
Use the IsOpen property of the serialPort object
Hope this helps
-Regards
Bharat Jain
bharat.jain.nagpur@gmail.com
|
|
|
|
|
Thanks a lot ppl.. It helps..
|
|
|
|
|
Problem in opening the visual data manager in visual basic project. I recieves the message "looking for object with ProgID: Visdata.visdataclass"
|
|
|
|
|
I would suggest that you reinstall.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
hello i am new to code project and new to visual basic so please be patient with what im sure is a pretty tedious question
I have searched high and low to find an answer to this but figuring out what to type into MSDN or Google to get relevant answers is tying my head in knots
I am trying to write a graphing component but the grid never lines up with the picture box i am trying to draw it in, and i have distilled the problem down to this
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.BorderStyle = BorderStyle.FixedSingle
PictureBox1.BackColor = Color.White
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim line As System.Drawing.Graphics
Dim pen As System.Drawing.Pen = Pens.Black
line = PictureBox1.CreateGraphics
line.DrawLine(pen, PictureBox1.Left, PictureBox1.Bottom, PictureBox1.Right, PictureBox1.Top)
End Sub
End Class
but when i run this code the line does not start in the bottom left and stop at the top right of the picture box
so how do i fix this and how do i make the solution resolution and maximize\minimize safe
thanks
modified on Monday, May 4, 2009 3:41 AM
|
|
|
|
|
Wankel Maggot wrote: line = PictureBox1.CreateGraphics
At this point, your code is utterly broken. Ditch this. NEVER use CreateGraphics again, except for rubber bands. The picture box is a waste of time, and you're not even using it as a picture box, so, the whole thing is useless.
Handle your paint event, and draw your graph directly onto your form, or into a panel if you prefer.
Christian Graus
Driven to the arms of OSX by Vista.
"I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )
|
|
|
|
|
ok well that doesn't really tell me what the issue is
if i do the same thing in a form will the top be the top,and the left be the left ,or am i over looking something else ,are controls in forms true to the dimensions they report
whats wrong with the create graphics ? why do they have it if it's so bad i should NEVER use it
handle my paint event?
I do appreciate the help please don't take all the questions as me tearing your advice down ,i am just very new to all this
|
|
|
|
|
Wankel Maggot wrote: ok well that doesn't really tell me what the issue is
I am not sure why you have an issue with alignment, I suspect that the issue is that you're not correctly working out where the top and left of the picture box is, and so drawing directly on the form will fix that.
Wankel Maggot wrote: whats wrong with the create graphics
It creates a graphics object out of thin air. It is not called by the WM_PAINT event. Therefore, if you draw your form with it, and then your form gets hidden ( such as being minimised, or another form being dragged over it ), when your form reappears, what you drew will be gone.
Wankel Maggot wrote: why do they have it if it's so bad i should NEVER use it
It's useful for drawing things you WANT to disappear, such as a rubber band, which is why I said that's where you could use it.
Wankel Maggot wrote: handle my paint event?
Your form has events, they are listed in the designer, if you click on the little thunderbolt icon. The paint event is where windows tells you form to draw itself, when it is first shown, when it becomes visible after being hidden, etc.
Christian Graus
Driven to the arms of OSX by Vista.
"I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )
|
|
|
|
|
Hi,
1. Christian is absolutely right. Take his advice.
2.
Wankel Maggot wrote: line = PictureBox1.CreateGraphics
line.DrawLine(pen, PictureBox1.Left, PictureBox1.Bottom, PictureBox1.Right, PictureBox1.Top)
"line" (what a silly name) is a Graphics that has a coordinate system relative to its top left corner
so PictureBox1.Left and Bottom are irrelevant inside of it.
3.
A Graphics object is expensive (i.e. large memory footprint, and slow to create).
If you create it, you must dispose of it.
That too will be handled automatically inside a Paint handler, when you don't use CreateGraphics any more!
|
|
|
|
|