How to keep only the first character in a sting - vb.net

I am looking for a way to store the first character in a string, I have used substring, but it did not work for me, since I want to keep only the first character in string regradless of the number of characters in the string.
This is the code I currently have:
If RadioButton2.Checked Then
RichTextBox1.Rtf = "{\rtf1\ansi\deff0 {\fonttbl{\f0 Times New Roman;}} " &
vbNewLine &
"- " & TextBox1.Text.Substring(2) & "," &
TextBox2.Text & ", " &
"\i " & TextBox5.Text & ". " & "\i0 " &
TextBox9.Text & ", " &
TextBox10.Text & ". "
End If
I am specifically looking for the first character in the string contained in textbox1.

To get the first character, specify the start index, and count.
TextBox1.Text.Substring(0, 1)

Related

Converting formula to string

I have the following formula that I am inputting in a function.
dim minfee, feetier3, feetier4, feetier5, bpspread1, bpsread2, bpspread3 as double
call insformulaincell("=IF(K2 = 100, ""#NA"", IF(K2 <" & minfee & "," & feetier3 & ",IF(K2<" & feetier4 & ",K2+ " & bpspread1 & ",IF(K2<" & feetier5 & ",K2+ " & bpspread2 & ",K2+ " & bpspread3 & "))))")
'all the function does is paste the formula into a cell
'How would I format the formula so that it can be stored as a single string?
'Ex:
dim sFormula as string
sformula = ""=IF(K2 = 100, ""#NA"", IF(K2 <" & minfee & "," & feetier3 & ",IF(K2<" & feetier4 & ",K2+ " & bpspread1 & ",IF(K2<" & feetier5 & ",K2+ " & bpspread2 & ",K2+ " & bpspread3 & "))))""
call insformulaincell(sFormula)
The main issue is that the variables such as minfee would not reference its actual values but instead have the actual string variable name appear.
Ex: "... If(K2 <" & minfee & "," ... )
as opposed to
"... If(K2 < 1) ..." ' assuming that minfee = 1
In VBA " serves as a delimiter for string literals, like this:
Dim foo As String
foo = "some string"
If your string literal needs to contain " double quotes, you need to escape them, by doubling them up between the string delimiters:
foo = """some string"""
Printing the above would yield "some string", including the double quotes.
So you do need to remove the leading & trailing double quotes.
sformula = "=IF(K2 = 100, ""#NA"", IF(K2 <" & minfee & "," & feetier3 & ",IF(K2<" & feetier4 & ",K2+ " & bpspread1 & ",IF(K2<" & feetier5 & ",K2+ " & bpspread2 & ",K2+ " & bpspread3 & "))))"
Breaking this down, it's a concatenation of the following literals:
"=IF(K2 = 100, ""#NA"", IF(K2 <" (note, "#NA" is a bad idea IMO. Use the NA() function to yield an actual worksheet error instead of a string value that looks like one)
","
",IF(K2<"
",K2+ "
",IF(K2<"
",K2+ "
",K2+ "
"))))"
Which looks right to me.
Arguably, such concatenation is annoyingly confusing. A custom StringFormat function can help mitigate this, by abstracting away the concatenations:
sFormula = StringFormat("=IF(K2=100, ""#NA"", IF(K2<{0},{1},IF(K2<{2},K2+{3},IF(K2<{4},K2+{5},K2+{6}", _
minfee, feetier3, feetier4, bpspread1, feetier5, bpspread2, bpspread3)

Microsoft.Office.Interop - ReplaceText (with) *NewLine* in Word.Table

I am having an application that uses Microsoft Word 2016 / the Interop interface to call the replace function. This way the application replaces some texts like 'Customername' to the actual name of the customer. Surely this is not the best way to do so, but thats how the application was build back in the days. Now I have to do some adjustments to the formatting facing a problem with the named function.
One of the Texts getting replaced is "[TELNR]" , beeing replaced with all known numbers of the customer, one number per line. Inserting New Lines as vbCr works great on a blank page as you can see top of the screenshot. The same called function will not put a new lines into a table (second page on the left) and won't even touch a textbox (second page on the right).
I have tried passing Line breaks as ^p, \n\r, \n, vbClRf / vbNewLine, vbCr. None of these will work.
I haven't found anything usefull on google or SO. Do you have ideas or a reason what might cause or even solve the problem?
#Region "Sample Strings"
Dim sample1 As String = "t:022220000000" & vbNewLine & "h:022221111111" & vbNewLine & ":0222222222222" & vbNewLine & "f:022223333333"
Dim sample2 As String = "t:022220000000" & vbCr & "h:022221111111" & vbCr & ":0222222222222" & vbCr & "f:022223333333"
Dim sample3 As String = "t:022220000000" & "^p" & "h:022221111111" & "^p" & ":0222222222222" & "^p" & "f:022223333333"
Dim sample4 As String = "t:022220000000" & "\n" & "h:022221111111" & "\n" & ":0222222222222" & "\n" & "f:022223333333"
Dim sample5 As String = "t:022220000000" & "\n\r" & "h:022221111111" & "\n\r" & ":0222222222222" & "\n\r" & "f:022223333333"
Dim sample6 As String = "t:022220000000" & vbCrLf & "h:022221111111" & vbCrLf & ":0222222222222" & vbCrLf & "f:022223333333"
#End Region
Private Sub replaceText(file As Microsoft.Office.Interop.Word.Document, find As String, replaceWith As String)
// First line was commented out for the ^p try
If replaceWith.Contains("^") Then replaceWith = replaceWith.Replace("^", "")
file.Content.Find.Execute(FindText:=find, ReplaceWith:=replaceWith, Replace:=Word.WdReplace.wdReplaceAll)
End Sub
The following was function was used to insert the textbox.

String sets its Value to "False"

So, I'm currently working on a program that should create Lua-Scripts for the Unitale-Engine. I added a "New Project" button which should create a new project file when you click it. I want this file to contain the default project-file-text. So i did this:
Dim File1 As String = "monster{" & vbNewLine & "COMMENTS=Ribbit, ribbit.;Croak, croak.;Hop, hop.;Meow." & vbNewLine & "COMMANDS=Compliment;Threaten" & vbNewLine & "DEFAULT_SPRITE=None" & vbNewLine & "NAME=Froggit" & vbNewLine & "HP=10" & vbNewLine & "ATK=1" & vbNewLine = "DEF=0" & vbNewLine & "CHECK_MESSAGE=Life is difficult for this enemy." & vbNewLine & "HandleAttack=" & vbNewLine & "OnDeath=" & vbNewLine & "HandleCustomCommand=" & vbNewLine & "}"
Dim File2 As String = vbNewLine & "encounter{" & vbNewLine & "DEFAULT_MUSIC=Fight" & vbNewLine & "ENCOUNTER_TEXT=Froggit hops close." & vbNewLine & "NEXTWAVES=1" & vbNewLine & "WAVETIMER=6" & vbNewLine & "ARENASIZE=155;130" & vbNewLine & "ENEMIES=Froggit" & vbNewLine & "ENEMYPOSITIONS=0;0" & "EncounterStarting=" & vbNewLine & "EnemyDialogueStarting=" & vbNewLine & "EnemyDialogueEnding=" & vbNewLine & "DefenseEnding=" & vbNewLine & "HandleItem=" & vbNewLine & "HandleSpare=" & vbNewLine & "EnteringState=" & vbNewLine & "Update=" & vbNewLine & "}"
Dim File3 As String = vbNewLine & "waves{" & vbNewLine & "1=Do Nothing" & vbNewLine & "}" & vbNewLine & "end"
My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\UnitaleMaker\Projects\" & PJNBox.Text & ".umproj", File1 & File2 & File3, False)
Yes, that is a lot of text. The problem is, that File1 always sets its value to "False" when it is declared. I thought that is because the string is too long, so I split it to 3 parts, but it doesn't work.
Any help would be apprechiated!
~ Mika // OneByte_
Starting with VB 14 (Visual Studio 2015), you can span String literals across multiple lines. VB 14 also adds String interpolation, which makes for much cleaner code. You could write your code like the following which would have kept you from having made your mistake of & vbNewLine = "DEF=0" &and would overall make the code easier to read.
I am also assuming you forgot a newline in "ENEMYPOSITIONS=0;0" & "EncounterStarting=" and I have indented the parts between the {}. I combined File1, File2, and File3 back into a single string since you stated you broke them up in the question, I assumed you'd rather have it all in one string.
Dim File1 As String = "monster{
COMMENTS = Ribbit, ribbit.;Croak, croak.;Hop, hop.;Meow.
COMMANDS=Compliment;Threaten
DEFAULT_SPRITE=None
NAME=Froggit
HP=10
ATK=1
DEF=0
CHECK_MESSAGE=Life Is difficult For this enemy.
HandleAttack=
OnDeath=
HandleCustomCommand=
}
encounter{
DEFAULT_MUSIC = Fight
ENCOUNTER_TEXT =Froggit hops close.
NEXTWAVES=1
WAVETIMER=6
ARENASIZE=155;130
ENEMIES=Froggit
ENEMYPOSITIONS=0;0
EncounterStarting=
EnemyDialogueStarting=
EnemyDialogueEnding=
DefenseEnding=
HandleItem=
HandleSpare=
EnteringState=
Update=
}
waves{
1 =Do Nothing
}
End"
My.Computer.FileSystem.WriteAllText($"{My.Computer.FileSystem.SpecialDirectories.MyDocuments}\UnitaleMaker\Projects\{PJNBox.Text}.umproj", File1, False)
Please learn to use string.format.
Also I changed & to new line in file2:
"ENEMYPOSITIONS=0;0" & "EncounterStarting="
I just assumed you forgot to add new line there.
Alss Environment.NewLine is the best option for new lines as it is universal.
Dim File1 As String = String.Format("monster{{{0}COMMENTS=Ribbit, ribbit.;Croak, croak.;Hop, hop.;Meow.{0}COMMANDS=Compliment;Threaten{0}DEFAULT_SPRITE=None{0}NAME=Froggit{0}HP=10{0}ATK=1{0}DEF=0{0}CHECK_MESSAGE=Life is difficult for this enemy.{0}HandleAttack={0}OnDeath={0}HandleCustomCommand={0}}}", Environment.NewLine)
Dim File2 As String = String.Format("{0}encounter{{{0}DEFAULT_MUSIC=Fight{0}ENCOUNTER_TEXT=Froggit hops close.{0}NEXTWAVES=1{0}WAVETIMER=6{0}ARENASIZE=155;130{0}ENEMIES=Froggit{0}ENEMYPOSITIONS=0;0{0}EncounterStarting={0}EnemyDialogueStarting={0}EnemyDialogueEnding={0}DefenseEnding={0}HandleItem={0}HandleSpare={0}EnteringState={0}Update={0}}}", Environment.NewLine)
Dim File3 As String = String.Format("{0}waves{{{0}1=Do Nothing{0}}}{0}end", Environment.NewLine)
My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\UnitaleMaker\Projects\" & PJNBox.Text & ".umproj", File1 & File2 & File3, False)

Simple vb function crashes SSIS and give "unexpeced end tag error"

I am at a loss to what the issue is on this one, it is a very simple function and when I close the editor in SSIS I get the "Unexpected end tag, line 95, postions 87, (mscorlib)" error. once I get this error I am no longer able to open the script task to edit the code. I have tried this on several machines. and it either crashes SSIS or gives me an error.
Function XMLWrapper(ByVal name As String, ByVal value As String, ByVal cdata As Boolean) As String
Dim Result As String
If value.Length > 0 Then
If cdata = True Then
Result = vbTab & "<field name=" & name & "><![CDATA[" & value & "]]></field>"
Else '<-- Line 95 where i get the error
Result = vbTab & "<field name=" & name & ">" & value & "</field>"
End If
Else
Result = ""
End If
Return Result
End Function
This error is actually being thrown by saving the code into the XML SSIS package!
You can fool it by splitting your strings like so
XMLResult = Replace(XMLResult, "URL>", "URL" & ">" & "<" & "![CDATA")
XMLResult = Replace(XMLResult, "<" & "/" & "JobDetailURL" & ">", "]]" & ">" & "<" & "/" & "JobDetailURL" & ">")
XMLResult = Replace(XMLResult, "<" & "/" & "CompanyURL" & ">", "]]" & ">" & "<" & "/" & "CompanyURL" & ">")
XMLResult = Replace(XMLResult, "<" & "/" & "CompanyImageURL" & ">", "]]" & ">" & "<" & "/" & "CompanyImageURL" & ">")

How to edit cell value in VB.net - Using .Interop.Excel

This is a simple question. I have this code:
CurrentRow = 3
MyColumn = 2
CurrentCell = (CurrentRow & "," & MyColumn)
WorkingSheet.Cells(CurrentCell).value = (ClientName & " " & "(" & ClientLocation & ")" & " " & ExtraDefinition)
I thought that this would place the data on the 'WorkingSheet' in the position "B3" but it places the data in the cell "AF1".
Why is this?
Thanks,
Josh
Cell is not expected to be used as you are using it; you have to input the row and column indices (as integers) separated by a comma. Thus the right code is:
WorkingSheet.Cells(CurrentRow, MyColumn).Value = (ClientName & " " & "(" & ClientLocation & ")" & " " & ExtraDefinition)
Another alternative you have is using Range. Example:
WorkingSheet.Range("B3").Value = (ClientName & " " & "(" & ClientLocation & ")" & " " & ExtraDefinition)