Simple app: comma delimiter with input and output text boxes - visual-studio-2010

All,
I'm looking to create a simple app that takes text entered by the user and splits it up, based on what line the text is entered on (it's a multiline textbox) using a comma. It inserts this new comma-delimited value into an output textbox for the user.
I know this may be an embarrassingly easy question, but my VB is very rusty:
Is there a built-in VB function that can do this without the use of txt files?
Thanks for your help.

This works for me:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.TextBox2.Text = TextBox1.Text.Replace(vbNewLine, ",")
End Sub

Related

Need to strip out invalid characters in CSV file

I am generating a CSV file from a Microsoft SQL database that was provided to me, but somehow there are invalid characters in about two dozen places throughout the text (there are many thousands of lines of data). When I open the CSV in my text editor, they display as red, upside-down question marks (there are two of them in the attached screenshot).
When I copy the character and view the "find/replace" dialog in my text editor, I see this:
\x{0D}
...but I have no idea what that means. I need to modify my script that generates the CSV so it strips these characters out, but I don't know how to identify them. My script is written in Classic ASP.
You can also use RegEx to remove unwanted characters:
Set objRegEx = CreateObject(“VBScript.RegExp”)
objRegEx.Global = True
objRegEx.Pattern = “[^A-Za-z]”
strCSV = objRegEx.Replace(strCSV, “”)
This code is from the following article which explains in details what it does:
How Can I Remove All the Non-Alphabetic Characters in a String?
In your case you will want to add some characters to the Pattern:
^[a-zA-Z0-9!##$&()\\-`.+,/\"]*$
You can simply use the Replace function and specify Chr(191) (or "¿" directly):
Replace(yourCSV, Chr(191), "")
or
Replace(yourCSV, "¿", "")
This will remove the character. If you need to replace it with something else, change the last parameter from "" to a different value ("-" for example).
In general, you can use charmap.exe (Character Map) from Run menu, select Arial, find a symbol and copy it to the clipboard. You can then check its value using Asc("¿"), this will return the ASCII code to use with Chr().

Populating an email with data from IBM i (AS400) screen

I'm trying to grab data from an AS400 screen & populate an email using that data but seem to have bumped into something I'm struggling to overcome. Here's a slice of what I have so far:
Dim polNo
polNo = GetText(10,18,10)
Dim wsh
Set wsh=CreateObject("WScript.Shell")
subSub1_()
sub subSub1_()
// Just doing this to check the text I have
SendKeys(polNo)
// Sent the eMail with the text
wsh.Run "mailto:testing#somemailbox.com?Subject=" & polNo
end sub
With the above, the resulting email subject line takes only the first word upto the first space. From what I've found, this is a parsing issue & have discovered the following line that should help.
polNo = Chr(34) + Replace(polNo,chr(34),chr(34)&chr(34))
The above line places all of the text in quotes (I know this because my SendKeys line now shows the GetText result with a " at the start.
The issue is when I reach the mailto line as Outlook pops up a window saying:
"The command line argument is not valid. Verify the switch you are using."
My end result will be an email that has a subject & a body with text taken from various parts of the screen.
Solved: Thanks to dmc below, he started me on the right line.
However, the solution was not to use Chr(34) but to use something as simple as:
polNo = Replace(polNo," ","20%")
Although it might not look like it, you're constructing a URL. As such, the contents of that URL must be URL Encoded. Certain characters can't be included in a URL, including a space. Those characters are represented with a percent sign followed by the ASCII code of the character in hexadecimal. For example, a space is changed to %20.
See the link below for a VBScript routine that will URL encode and decode strings.
http://www.justskins.com/forums/wsh-equivalent-of-server-38778.html
Edit: Although this is commonly known as URL encoding, the thing you're constructing is technically a URI. Wikipedia has a good page that explains further.

Certain characters make moveItemAtURL:toURL:error: crash. how do I avoid them?

First, I'm using Swift. Second this line works fine in my code:
let didIt = fileManager.moveItemAtURL(originalFilePath, toURL: newFilePath, error: nil)
...as long as there are no special characters in the newFilePath. if the newFilePath has a dollar sign or an ampersand ($, & ) in it, the line fails. My issue is that the newFilePath comes from a text field in a window where the user can type any old thing. How do I escape special characters, or encode them so they will pass the test and be included in the new filename?
thanks in advance for any pointers.
My issue is that the newFilePath comes from a text field in a window where the user can type any old thing.
Right there is your problem. Why are you not using an NSSavePanel for letting the user select a name under which to save a file?
If you insist on taking input from a text field, the docs for -URLByAppendingPathComponent: specifically say that the path component string should be "in its original form (not URL encoded)" (emphasis mine).
How did you originally create newFilePath, before appending the path component? For example, you should have used one of the methods with "[fF]ileURL" in the name.

Long line breaking input structure in VB6

I have a file that is comma delimited
Text File:
"some_key_1", "Translation 1"
"some_key_2", "Translation 2"
"some_key_3", "Translation 3"
"some_key_4", "Translation 4"
"some_key_5", "I am a very long line of text that has decided to cause an issue for the programmer, I have thus far laughed at his futile attempts to fix me."
Private Sub ImportFile()
Dim strEmpFileName As String
Dim intEmpFileNbr As Integer
Dim strTranslationKey As String
Dim strTranslation As String
Dim error As String
strEmpFileName = "C:\Files\test_file_1.asp"
intEmpFileNbr = FreeFile
Open strEmpFileName For Input As #intEmpFileNbr
Do Until EOF(intEmpFileNbr)
Input #intEmpFileNbr, strTranslationKey, strTranslation
Loop
End Sub
The code assigns the lines of text just fine until it gets to some_key_5, even tho there it thinks the text on a new line even tho it is a new line because of word wrap and not me hitting enter.
Is there any way around this? Shortening the line is not really an option.
I think the likely problem is the comma in the translation element. I'd suggest your best bet is probably to read the file with the
Line Input #my_file, my_string
statement and pick it apart by hand. See here for more.
You can use regular expression to match all strings inside "", and then put them into pairs.
As for line breaks, you can simply replace all of them to empty string before doing a regexp match.
Most likely you have a line termination issue, and you actully have a CR where it stops reading. I would suggest recreating the file using notepad, or looking at it with a text editor with a hex mode (that might tell you if you have a really long line as as well, if your real file has a line that exceeds a few K it might actually be a problem).
I ran your code against your supplied data, with a msgbox giving the length and right 10 characters of the string, and it worked. It also worked when I increased the field length of the second part to 721 chars.
I opted to use the file system object and it works just fine with the same data. It couldn't have been the comma, as it would attach the data from the file to the variable just fine, and include information past the comma. It would then add the item on the next line as a new key. I couldn't get past this, tried even removing the commas from the offending item and it would still break. But after I switched it to the file system object, it works just fine with the same data, i just have to manually split it.

Index was outside the bounds of the array - RichTextBox lines setvalue

I've searched online and on StackOverflow, but I can't seem to find the answer to my question, although some of them came very close.
I am programming for .Net in Delphi Prism. I have a RichTextBox on a WinForm and I need to insert a line of text at the top every time program does insert. So, I am doing the following and it runs upto the line and raises the following exception.
offending code:
RichTextBox1.Lines.SetValue(str,0);
Exception:
Index was outside the bounds of the array
I think, I think I know why it is raising the exception. It's because there are no lines inserted into RichTextBox. So, my program really can't insert any line of text. I need to really insert line of text at the top everytime my program inserts a new line of text.
If I do call RichTextBox1.AppendText(str);, then it works and inserts the str text without newline, but it appends at the end. I want it inserting text at the top every time.
How do you insert line of text into RichTextBox?
Thanks.
The Lines property of the textbox is simply an array of string. So you need to add one element, move all elements one index down and insert your new text at the first index.
Also the Text property of the textbox is a string. Strings in .NET are immutable, so you need to fully replace the value.
One approach would be like this:
RichTextBox1.Text := "YourNewText" + Environment.NewLine + RichTextBox1.Text;

Resources