0

I'm trying to make my program display text into a text box but when I paste the text it causes errors

richTextBox1.Text = "/give @p sign 1 0 {    BlockEntityTag: {        Text1: "{\"text\":\"BUY FOR\",\"bold\":true,\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/playsound ui.button.click master @a ~ ~ ~ 10 2\"}}",        Text3: "{\"text\":\"2           LVL\",\"color\":\"white\",\"bold\":true,\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/scoreboard players tag @p add SHOP2\"}}"    },    display: { Name: "XP SIGN"    }}";

Error CS1002 ; expected MSC C:\Users\Aprute\Desktop\Minecraft Shop Creator\MSC\MSC\Form1.cs

Error CS1056 Unexpected character '\' MSC C:\Users\Aprute\Desktop\Minecraft Shop Creator\MSC\MSC\Form1.cs

Error CS1002 ; expected MSC C:\Users\Aprute\Desktop\Minecraft Shop Creator\MSC\MSC\Form1.cs

Error CS1513 } expected MSC C:\Users\Aprute\Desktop\Minecraft Shop Creator\MSC\MSC\Form1.cs

Error CS1002 ; expected MSC C:\Users\Aprute\Desktop\Minecraft Shop Creator\MSC\MSC\Form1.cs

Error CS1513 } expected MSC C:\Users\Aprute\Desktop\Minecraft Shop Creator\MSC\MSC\Form1.cs

Error CS1002 ; expected MSC C:\Users\Aprute\Desktop\Minecraft Shop Creator\MSC\MSC\Form1.cs

Error CS1010 Newline in constant MSC C:\Users\Aprute\Desktop\Minecraft Shop Creator\MSC\MSC\Form1.cs

Error CS1519 Invalid token '";' in class, struct, or interface member declaration MSC C:\Users\Aprute\Desktop\Minecraft Shop Creator\MSC\MSC\Form1.cs

Error CS1022 Type or namespace definition, or end-of-file expected MSC C:\Users\Aprute\Desktop\Minecraft Shop Creator\MSC\MSC\Form1.cs

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Aprute
  • 1

2 Answers2

0

You need to escape your text as such:

richTextBox1.Text = @"/give @p sign 1 0 {    BlockEntityTag: {        Text1: ""{\""text\"":\""BUY FOR\"",\""bold\"":true,\""clickEvent\"":{\""action\"":\""run_command\"",\""value\"":\""/playsound ui.button.click master @a ~ ~ ~ 10 2\""}}"",        Text3: ""{\""text\"":\""2           LVL\"",\""color\"":\""white\"",\""bold\"":true,\""clickEvent\"":{\""action\"":\""run_command\"",\""value\"":\""/scoreboard players tag @p add SHOP2\""}}""    },    display: { Name: ""XP SIGN""    }}"
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
  • I'm just trying to understand what you changed for it to work. – Aprute May 30 '19 at 17:31
  • The usage of the verbatim string literal and escaping " (using "" instead) did the trick. Here's more info and a link to Jon Skeet's explanation: https://stackoverflow.com/a/556142/10713658 – Mariano Luis Villa May 30 '19 at 18:28
  • @Aprute Sorry for the late reply. There are two characters that you need to escape in your original text, `\` and `"`, which should be escaped `\\` and `\"`. However, I used a verbatim string( notice the `@`) which only requires you to escape the `"` to `""`. – Xiaoy312 May 30 '19 at 19:45
0

As sticky _bit* has pointed out, you need to escape any quotes within your string.

i.e.

richTextBox1.Text =  "/give @p sign 1 0 {    BlockEntityTag: {        Text1: \"{\"text\":\"BUY FOR\",\"bold\":true,\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/playsound ui.button.click master @a ~ ~ ~ 10 2\"}}\",        Text3: \"{\"text\":\"2           LVL\",\"color\":\"white\",\"bold\":true,\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/scoreboard players tag @p add SHOP2\"}}\"    },    display: { Name: \"XP SIGN\"    }}";
PKCS12
  • 407
  • 15
  • 41