Text File Formatting how to add white space in each row using vb.net - vb.net

How can I add white space in each rows to the exported file. I want to achieve is in each row is it has a total 44 character length. The content + white space to complete the 44 characters in every rows. I have a working code but I don't know how to apply the adding of white space see the last image in the question to give you an idea of what i'm trying to achieve thanks in advance.
CODE
RESULT
RESULT I WANT TO ACHIEVE
In the goal image i have shown the result with white space in every row total of 44 character including white space. How to do that? Thanks in advance

You should give writing the code a crack, but here is a basic outline of what you need to be doing:
count the characters in your string
subtract them from 44
add whitespace equivalent to the result of the previous
Example code:
Dim wsNeeded As Integer = 44 - dgvBilled.Rows(irow).Cells(col).Value.ToString().Length
Dim wsString As String = dgvBilled.Rows(irow).Cells(col).Value.ToString()
wsString.PadRight(wsNeeded," ") ' Maybe just "" instead of " "
Writer.Write(wsString)

Related

Add text box at specific location in report base on value

I'm working on a cut list generator but I'm having difficulty on the final report. I'd like to display a rectangle that represents the factory length piece with lines indicating cut points. In each segment I'd like to have the length of the piece shown. Using Report.line I've created the rectangles needed but I'm not sure how to get text in each box. Here is a sample output so far As an example I want the three rectangles for Piece #1 to have 48" in them, probably all the way to the left. Any suggestions? I thought createReportControl might work but I'm not sure that is the correct approach. I'm also thinking about one text box with a monospace font so I can scale the input across the entire width. Any suggestions are appreciated.
Thanks,
Dave
I played around with the monospace font idea. It isn't as pretty as I would like but I'm getting closer.
The issue is that I cannot keep the text in the same spot in each box. There is one line lower on the page that pushes the number almost out the right side of the box.
Sample Output
This code is functional but I'm looking at the cosmetics. I'm inserting spaces between my values using the following function:
Private Function InsertSpaces(CutLen, PieceLen) As String
MaxChar = 50 ' 6 inch 14pt Courier text box
cutchar = Int(CutLen / PieceLen * MaxChar)
Cutcharcount = Len(Str(CutLen))
cutchar = cutchar - Cutcharcount + 1
For i = 1 To cutchar
InsertSpaces = InsertSpaces + " "
Next
End Function
I'm just trying to clean it up. CreateReportControl was giving me a error because I wasn't in design mode. I'm guessing that is because it ran as part of OnFormat of the Detail section.

How to get last word in a cell using Excel Formula

I am trying to get the last word in a cell however, it does not seem to be appearing getting the last word of the cell. It seems to get the partial match and not working correctly. I am using =IFERROR(RIGHT(AP2,SEARCH(" ",AP2)-1),AP2). This works great for getting the first word in a cell even where the first word is the only word in a cell. Any ideas on why this is not getting the last word correctly?
You may try this UDF which can be used in another sub routine or on the worksheet as well.
Function GetLastWord(ByVal Str As String) As String
Dim arrStr
arrStr = Split(Str, " ")
GetLastWord = arrStr(UBound(arrStr))
End Function
If you need to use it on the worksheet, assuming your string is in A1 then try this...
=GetLastWord(A1)
Your formula is based on the first occurrence of a space in the string.
For example with "Man City V Stoke" in the formula SEARCH(" ",AP11) is looking for the first occurrence of a space which occurs at position 4. You are then subtracting 1 from this and using this as the number of characters to return from the right of the original string. So you get Right("Man City V Stoke",3) which is "oke".
A better way is a formula such as
=TRIM(RIGHT(SUBSTITUTE(AP11," ",REPT(" ",LEN(AP11))),LEN(AP11)))
find text after last space
The part written: =SUBSTITUTE(AP1," ",REPT(" ",LEN(AP1)))
This inserts spaces the length of the string (16) in place of every occurrence of a space i.e.
Man City V Stoke
When you then do RIGHT(SUBSTITUTE(AP11," ",REPT(" ",LEN(AP11))),LEN(AP11))
you are guaranteed to only get whitespace followed by the last word. You then use TRIM to get rid of this white space.
As far as I can tell the below worked for me.
=TRIM(RIGHT(SUBSTITUTE(AP40," ",REPT(" ",100)),100))
I can only assume my other method is unreliable at getting the last word.

Comparing specific data from string within cells by locating via unique parameters

I have a string that often looks like this:
X-POTATO-2D-AB3F-N
It will always be in that format, and I want to compare "AB3F"to another string of four characters. Here's the catch, certain values in this string may change in length, so I cannot compare simply "the first four of the last 6 characters"
What will always stay the same is the number of dashes. Always at least 4 in total (on rare occasions 5), and the string that I want to compare will always be between the last two. Is it possible to use these to isolate the 4 or 5 characters that I want to compare?
I know Stack overflow doesn't like vague questions. Sorry! :D
Here you go:
http://excel.tips.net/T003324_Finding_the_Nth_Occurrence_of_a_Character.html
Essentially, this formula:
=FIND("¬",SUBSTITUTE(A1,"-","¬",4))
Where A1 is your string. This will return the position of the 4th "-". (Because substitute replaces the 4th "-" with a "¬", which is unlikely to appear in the code, and find then finds the "¬")
Edit, just realised that the 4th group may also have more than 4 digits, so you could retrieve it with the overcomplicated:
=MID(A1,FIND("¬",SUBSTITUTE(A1,"-","¬",3))+1,FIND("¬",SUBSTITUTE(A1,"-","¬",4))-FIND("¬",SUBSTITUTE(A1,"-","¬",3))-1)
Hope this helps!
Since you mentioned there could be four or five hyphens, and that your desired value will always be between the last two, I suggest the following:
=TRIM(LEFT(RIGHT(SUBSTITUTE(A1,"-",REPT(" ",99)),198),99))
Replace each hyphen with 99 spaces (much longer than the longest substring)
The Right most 198 characters will surely have the last two substrings
Left (...,99) will surely return the first of those two
Trim to get rid of the extra spaces
This approach will work with four or five hyphens.
If you are going to use this in a Macro, the following code will return the "Next To Last" hyphen-delineated substring within a string:
Dim V As Variant
V = Split(S, "-")
NextToLast = V(UBound(V) - 1)

VB notepad spliter

So I just started a new program and in this program I need a feature that will basically take a text file or just a richtextbox, and then equally split it. So lets say I have a file with 200 lines of text. Then I want to split it up into 10 files of 20 lines. Please help I need some type of direction! -thanks
Count the textbox length by (textbox1.text.length)i.e.=200 character and divide it by 2 = 100 then make a loop which goes to 200 then keep condition in loop body if it equal to 100 put it to string variable and write it to file and so on and so forth...
First of all create a string array
Dim S() As String = TextBox1.Text.Split(vbNewLine)
this will allow you to put every line in an item of the array named S
to get the lines count use
S.Length
OR
S.Count()
now you got the lines and the lines' count all you have to do to divide them and go from there...

How to append text in a richtextbox in VB.NET on a specified line

How can I append text in a rich text box? Say, for example, that I need to append it on line 40 at column 30.
Take your string, count the lines, then use string manipulation to manually insert your string in the correct position.
You could count lines by counting occurrences of '\n' and for column you just add column number to the position that you find your line at.
If this is too little info, ask me and I can paste you a c# code for it.