W
W
White_Bambie2020-07-08 13:19:02
MySQL
White_Bambie, 2020-07-08 13:19:02

How to store and display records with text from a database with formatting support?

The form has the following objects: richTextBox, 2 Button and ListBox.
an entry is selected in the Listbox.

private void MyListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Patient_id = Convert.ToInt32(MyListBox.SelectedIndex);

            MySqlConnection con = new MySqlConnection(AppSetting.ConnectonString());
            MySqlCommand cmd;
            cmd = con.CreateCommand();
            cmd.CommandText = "SELECT * FROM patients WHERE id='" + MyListBox.SelectedValue + "';";

            try
            {
                con.Open();

                MySqlDataReader sdr = cmd.ExecuteReader();

                // Загружаем данные
                while (sdr.Read())
                {
                    string sNaz = sdr.GetString("Naz");
                    richTextBox1.Text = sNaz;
                }

                con.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Подключение к серверу отсутствует");
            }
        }

Text is entered into the richTextBox, and Button1 gives you the option to format the selected text.
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, FontStyle.Bold ^ richTextBox1.SelectionFont.Style);
richTextBox1.Select();
}

-
On pressing Button2, the text from richtextbox1 is written to the database.
private void button2_Click(object sender, EventArgs e)
        {
            if (Patient_id != -1) // Если выбран пациент из списка
            {
                MySqlConnection con = new MySqlConnection(AppSetting.ConnectonString());

                con.Open();

                MySqlCommand cmd;

                cmd = con.CreateCommand();
                cmd.CommandText = "UPDATE patients set [email protected] WHERE id='" + MyListBox.SelectedValue + "';";

                cmd.Parameters.AddWithValue("@Naz", richTextBox1.Text);

                cmd.ExecuteNonQuery();

                con.Close();

                MessageBox.Show("Пациент обновлен", "Сообщение");
            }
            else
            {
                MessageBox.Show("Выберите пациента", "Сообщение");
            }
        }


-
How to save formatted text and display it in the future from the database in richtextbox?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shitskov, 2020-07-08
@White_Bambie

RichTextBox1.Rtf stores text with all formatting . Save and load it to save formatting

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question