Add Text In textbox at a certain position C#

Asked

Viewed 944 times

3

Hello, I am developing a dental software which one of the functions is to select a tooth on a button and add the name of this tooth in a textbox. Until then no problem, but the point is that this textbox refers to observations, and I would like the name of each selected tooth, be added in the textbox in different lines and not side by side. So as each button referring to each tooth is selected it adds the name of the teeth one in each row. For example, when I click button1 is added the text "Bot1" in the textbox and so on, the problem is that if I click more than one button the text should go to different lines and not in the same. That’s what I’d like but I can’t develop a logic to do that someone has some idea?

follow the click event I’m using on the Uttons:

private void button1_Click(object sender, EventArgs e)
    {
        string Texto = txtObservacao.Text;
        txtObservacao.Text += "Terceiro Molar Inf.Direito (48):";
        if (btnMolar48.BackColor == Color.FromArgb(150, Color.LightSeaGreen))
        {
            btnMolar48.BackColor = Color.Transparent;
            txtObservacao.Text = Texto.Replace("Terceiro Molar Inf.Direito (48):", "");
        }
        else
        {
            btnMolar48.BackColor = Color.FromArgb(150, Color.LightSeaGreen);
        }
    }

    private void btnMolar47_Click(object sender, EventArgs e)
    {
        string Texto = txtObservacao.Text;
        txtObservacao.Text += "\n" + "Segundo Molar Inf.Direito(47):";
        if (btnMolar47.BackColor == Color.FromArgb(150, Color.LightSeaGreen))
        {
            btnMolar47.BackColor = Color.Transparent;
            txtObservacao.Text = Texto.Replace("Segundo Molar Inf.Direito(47):", "");
        }
        else
        {
            btnMolar47.BackColor = Color.FromArgb(150, Color.LightSeaGreen);
        }
    }`

Thank you.

3 answers

6


There are basically two types of CR line breaks (Carriage Return - \n) and LF (line feed - \r).

You can read a little about it here at What is the difference between Carriage Return and line feed?

Possibly you are using the wrong type, to avoid this kind of stress and even to be completely sure that your code will work even on other machines is possible to use the Environment.NewLine.

Example:

textBox.Text = "Linha 1" + Environment.NewLine + "Linha 2";
  • There are also the two together, which is the \r\n. I just don’t know his name. And if I’m not mistaken, the Environment.NewLine uses this mentioned value.

  • It is the junction of the two (would be CRLF). The Environment.NewLine uses the appropriate character according to the platform being used (Windows uses both)

1

I managed to solve my problem using System.Environment.Newline.

string Texto = txtObservacao.Text; txtObservacao.Text += System.Environment.NewLine; txtObservacao.Text += "Segundo Molar Inf.Direito(47):"; if (btnMolar47.BackColor == Color.FromArgb(150, Color.LightSeaGreen)) { btnMolar47.BackColor = Color.Transparent; txtObservacao.Text = Texto.Replace("Segundo Molar Inf.Direito(47):", ""); } else { btnMolar47.BackColor = Color.FromArgb(150, Color.LightSeaGreen); }

I apologize if I was not clear in my question above, but who answered, my sincere thanks!

0

 private void button1_Click(object sender, EventArgs e)
 {
 int h =3;

Button newButton = new Button();
Button[] buttonArray = new Button[8];

for (int i = 0; i <= h-1; i++)
{
  buttonArray[i] = new Button();
 buttonArray[i].Size = new Size(20, 43);
  buttonArray[i].Name= ""+i+"";
  buttonArray[i].Click += button_Click;//function
  buttonArray[i].Location = new Point(40, 20 + (i * 20));
  panel1.Controls.Add(buttonArray[i]);

}  }
  • What does your code do? Does it solve the question problem? If so, it has how to explain to us how it does it?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.