VB 2010: Console application prints some superscript number characters as normal - vb.net

I just have a small issue with a Visual Basic (2010 Express) .NET application I'm making. It's a console-based application and I want it to print superscript numbers.
Well, I've found out the superscript numbers from Charmap and my code is this:
Public Module MainModule
Public Sub Main()
Console.Write(GetExponentialNumAsStr(12345, 12345)) 'This must be printed as 12345¹²³⁴⁵
Console.ReadKey(True)
End Sub
Public Function GetExponentialNumAsStr$(Number As Double, Exponent%)
If Exponent = 1 Then Return Number : Exit Function
Return Number.ToString & Exponent.ToString.Replace("-", "⁻").Replace("0", "⁰").Replace("1", "¹").Replace("2", "²").Replace("3", "³").Replace("4", "⁴").Replace("5", "⁵").Replace("6", "⁶").Replace("7", "⁷").Replace("8", "⁸").Replace("9", "⁹")
End Function
End Module
Okay, the GetExponentialNumAsString$ function works well and gives 12345¹²³⁴⁵, which is okay (checked through Immediate window), but the only problem is that while printing the same in the Console view gives this:
123451²345
Why is it that only 2 is shown in superscript form and why not the other digits also?
If I try printing 0 to 9 as superscript, it comes like this:
1²345678?°
Well 9 is gone, but the other digits is what confusing me. Only 2 and 0 are shown in superscript form!
I also tried copy-pasting all the superscript numbers in a Command Prompt window... this is what came:
Command Prompt, in fact, when copy-pasted, shows correctly!
So what should I do? Convert those superscript numbers to CP-437 (DOS) encoding and then display it?
Please help me! Thank you everybody!
EDIT: I've tried saving that thing as a file in my Desktop using CP-437 encoding. What appears is like 1ý345678?ø. And when converted back manually to UTF-8 or one of the general encodings, it comes back as 1²345678?°.
Also tried using ECHO ¹²³⁴⁵⁶⁷⁸⁹⁰>Hello123.TXT and what came was the same thing as above. So... has no use.
And I am a bit sure that CP-437 has those superscript characters because Command Prompt can display them!!!

Thanks to Hans Passant's comment, the trick is to set the Console's output encoding to UTF-8, such as
Console.OutputEncoding = System.Text.Encoding.UTF8
And look! It comes!

Related

Implementing Unicode Characters into VB Console Application

I'm trying to create a Backgammon game board using Unicode graphics with an array back-end. For example; White Draughts Man: U+26C0 to represent checkers on a slot. What do I need to do to make this work ?
I've tried using ChrW() with the Unicode that is given from a Unicode table elsewhere
PS: This code is from a test application before adding to my actual assignment.
I just receive an error.
Sub Main()
Console.OutputEncoding = System.Text.Encoding.Unicode
Console.WriteLine(ChrW(&H26C0))
Console.ReadKey()
End Sub

Range accepts sometimes only semicolons instead of commas

I have reduced my problem to the following code example. I am using a German Excel version in which separators in normal Excel formulas are semicolons ";" instead of "," (e.g. =SUMME(A1;A3) instead of =SUM(A1,A3)).
Now the code which works different from time to time:
Sub CommasDoNotWorkAnymore()
Dim a()
Dim i%
a = Array("A1,A3,A5", "B1", "B2")
i = 0
Debug.Print Sheets(1).Range(a(i)).Address
End Sub
Normally, when starting Excel, this code works. But sometimes Excel seem to switch the accepted separators used in the Range() to semicolons untill I restart Excel. This occurs most times when rerunning the code after a runtime error.
Is this a general Excel bug? Does anybody know what is behind this behaviour? Is there some Excel-wide "local option" for the Range class?
EDIT: I just tried to convert the a(i) with CStr(a(i) but this does also not work. So no ByRef kind of problem...
If you want to control it, check first what separator is currently in use. What I guess is that you want to know the list separator:
Application.International(xlListSeparator)
Check other separators here:
https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-international-property-excel
The other time I had a problem with identifying decimal separator in VBA. Finnally I was able to get it in this way:
Function GetVBAdecimalSep()
Dim a(0) As Variant
a(0) = 1 / 2
GetVBAdecimalSep = Mid(a(0), 2, 1)
End Function
Changing separator not always works. Please see this: Changing decimal separator in VBA (not only in Excel)
The best solution is to check/change locale, even temporary.
Application.LanguageSettings.LanguageID(msoLanguageIDUI)
gives the LCID which would be 1033 for English (US)

VB.Net corrupted strings - IDE Incorrectly interpreting a string as code

Today I opened a visual basic project which has several lines that declare some strings, which contain comma separated data. I amended one of the strings, rebuilt and closed the project. When I came to test it, there was an error. When I looked at the code, I discovered that the strings on lines after the one I had amended had also changed - they had spaces inserted after the commas.
Has anyone seen normal strings incorrectly having formatting applied by the IDE in visual studio 2015?
It seems like the string is being formatted as if it were code. I would suspect this has something to do with String Interpolation, though these are legacy projects that do not use it.
Edit, FWIW the code is similar to this, I've renamed the fields and headings to try keep it short.
fullColumnMappingsBuilder.Append(",field1,field2,field3,field4,field5,field6,field7,field8,field9,field10")
fullColumnHeadingsBuilder.Append(",heading1,heading2,heading3,heading4,heading5,heading6,heading7,heading8,heading9,heading10")
If clientCode = "ML" Then
fullColumnMappingsBuilder.Append(",mlField")
fullColumnHeadingsBuilder.Append(",mlHeading")
End If
defaultColumnMappingsBuilder.Append(",morefields1,morefields2,morefields3,morefields4")
defaultColumnHeadingsBuilder.Append(",moreHeadings1,moreHeadings2,moreHeadings3,moreHeadings4")
My edit was to simply add in field11 and heading11, and save. But the code ended up looking like this:
fullColumnMappingsBuilder.Append(",field1,field2,field3,field4,field5,field6,field7,field8,field9,field10,field11")
fullColumnHeadingsBuilder.Append(",heading1,heading2,heading3,heading4,heading5,heading6,heading7,heading8,heading9,heading10,heading11")
If clientCode = "ML" Then
fullColumnMappingsBuilder.Append(", mlField")
fullColumnHeadingsBuilder.Append(", mlHeading")
End If
defaultColumnMappingsBuilder.Append(", morefields1, morefields2, morefields3, morefields4")
defaultColumnHeadingsBuilder.Append(", moreHeadings1, moreHeadings2, moreHeadings3, moreHeadings4")
The problem is that the IDE has inserted spaces after the commas on lines that I did not edit.
Well, I think I know how I did it. If you start with something like this:
Public Class Class1
Sub New()
Dim string1 As String = "Hello there world asdfj asldfkja sad "
Dim string2 As String = "some code like words dim private as object sub"
End Sub
End Class
I think I must have taken the first double quotes character off the declaration of string1, and then moved the position of the text cursor to a different line. In previous versions of visual studio, the IDE would have marked the first line as being in error.
Now we have multi-line strings, it thinks you have a multi-line string running from the quotes at the end of the first line up to string2 As String", and that the following text is code, and so it gets changed (in this case the keywords are capitalized).
Dim string2 As String = "some code Like words Dim Private As Object Sub"
I'll just have to be more careful when editing strings.
Might not have been your fault.
I've seen VS 2015 automatically make changes very similar to the above example:
Dim string2 As String = "some code like words dim private as object sub"
... changing to:
Dim string2 As String = "some code Like words Dim Private As Object Sub"
This was on both multi-line and single-line strings, in procedures I had not touched at all, in a .vb file where I was changing strings in other procedures. Only reason I caught VS doing this was because I did a Compare before check-in.
In Visual Studio formatting Strings as VB.NET code it suggests un-checking Tools > Options > Text Editor > Basic > Advanced > Pretty listing (reformatting) of code, but am reluctant to turn off all the good things that does.
Feels like a bug in VS 2015 to me. Have never seen this in 16 years of using prior versions of VS, before 2015.

VB seems to lose newlines when called over COM

I have a VB method
Public Sub append_text(ByVal s As String)
f1.TextBox1.AppendText(s)
End Sub
which is called over COM from C++
_bstr_t b(L"test\nnew\nlines\n");
ATLENSURE_SUCCEEDED(t->append_text(b));
But the text box ends up saying
testnewlines
Without the aforementioned new lines.
Why is that then?
For the sake of completeness, posting my comment as an answer (now that I know it's correct...):
Different operating systems consider different character combinations as new lines. The *nixes, for instance, use a single \n, as in your code. Windows, on the other hand, uses the \r\n combination. Therefore, the single \n in your string just isn't enough to be considered a new line marker. Using \r\n will do the trick.
Eran is right.
To fix it on the VB side, try this
Dim s2 As String = s.Replace(vbLf, vbCrLf)
f1.TextBox1.AppendText(s2)
EDIT Sideshow Bob has compiled and tested this.

Is there any way to align text in message box in vb or vba?

Is there any way to align text into the center in msgbox in VB or VBA? Does VB have any functionality to do the same?
No. The MsgBox() function is simply a wrapper for the Windows MessageBox() function and as such has no stylistic control over the dialog beyond the icon.
If you want to change it any further than this, you will need to create your own window and show that instead.
On Windows Vista+ you can use TaskDialogs that allow a lot more control.
no, but you can cheat by using spaces.
msgbox(" your message")
VBA
Some notes: http://access.mvps.org/access/bugs/bugs0035.htm AND http://www.tek-tips.com/viewthread.cfm?qid=435428 However, it is not so difficult to build your own message box, which solves all your problems.
When you are building your strings you could pad them at the beginning and end with spaces to achieve a target length. If you're using excel the worksheet function rept is handy for this.
function pad_n_center(byval mystring as string, lenmax as integer) as string
dim pad_by as integer
dim pad as string
pad_by = (lenmax - len(mystring))/2
'some more code to finesse that?
pad = worksheetfunction.rept(" ",pad_by)
pad_n_center = pad & mystring & pad
end function
As mentioned before if the msgbox still doesn't look good you can use textbox shape object (or other objects) to get the desired effect.