Работа с richTextbox, listBox и файлами - C#

Узнай цену своей работы

Формулировка задачи:

Народ, очень срочно. Пишу курсач на С#. Нужно прописать две кнопки. Курсовой - электронный учебник. Задача такая: на форме есть 2 ListBoxa, один из них список файлов тем - в номерном порядке, другой практик. И файлы и практики в формате RTF. при нажатии на нужный номер темы файл загружается в TextRichBox. Есть две кнопки, предыдушая и следующая (тема, практика), как сделать так, чтобы кнопки при нажатии например на кнопку "следующая", загружался файл в TextRichBox, который стоит в очереди после файла который загружался через ListBox.

Решение задачи: «Работа с richTextbox, listBox и файлами»

textual
Листинг программы
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        
         public Form1()
            
        {
            InitializeComponent();
        }
       
 
        private void Form1_Load(object sender, EventArgs e)
        {
            button10.Visible = false;
            button11.Visible = false;
            button12.Visible = false;
            richTextBox1.Visible = false;
        }
 
        private void button6_Click(object sender, EventArgs e)
        {
          DialogResult res = MessageBox.Show("Так быстро покидаете учебник по БД?","",MessageBoxButtons.YesNo);
          switch(res)
          {
              case DialogResult.Yes:
                  { this.Close(); } break;
              case DialogResult.No:
                  {return;}
          }
            
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Visible = true;
            listBox2.Visible = false;
            button7.Visible = false;
            button8.Visible = false;
            button9.Visible = false;
            label2.Visible = false;
           
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            richTextBox1.LoadFile("1-2.rtf", RichTextBoxStreamType.PlainText);
            listBox1.Visible = false;
            listBox2.Visible = false;
            button1.Visible = false;
            button2.Visible = false;
            button3.Visible = false;
            button4.Visible = false;
            button5.Visible = false;
            button6.Visible = false;
            button7.Visible = false;
            button8.Visible = false;
            button9.Visible = false;
            pictureBox2.Visible = false;
            pictureBox3.Visible = false;
            label2.Visible = false;
            label1.Visible = false;
            menuStrip1.Visible = false;
            button10.Visible = true;
            button11.Visible = true;
            button12.Visible = true;
            richTextBox1.Visible = true;
            
 
        }
 
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
           string curItem = listBox1.SelectedItem.ToString();
           curItem = listBox1.SelectedIndex.ToString();
           if (listBox1.SelectedIndex == 0 )
           richTextBox1.LoadFile("1-2.rtf", RichTextBoxStreamType.PlainText);
           if (listBox1.SelectedIndex == 2 )
           richTextBox1.LoadFile("3.rtf", RichTextBoxStreamType.PlainText);
           if (listBox1.SelectedIndex == 3)
           richTextBox1.LoadFile("4.rtf", RichTextBoxStreamType.PlainText);
           if (listBox1.SelectedIndex == 4)
           richTextBox1.LoadFile("5.rtf", RichTextBoxStreamType.PlainText);
           if (listBox1.SelectedIndex == 5)
           richTextBox1.LoadFile("6.rtf", RichTextBoxStreamType.PlainText);
           if (listBox1.SelectedIndex == 6)
           richTextBox1.LoadFile("7.rtf", RichTextBoxStreamType.PlainText);
           if (listBox1.SelectedIndex == 7)
           richTextBox1.LoadFile("8.rtf", RichTextBoxStreamType.PlainText);
           if (listBox1.SelectedIndex == 8)
           richTextBox1.LoadFile("9.rtf", RichTextBoxStreamType.PlainText);
           if (listBox1.SelectedIndex == 9)
           richTextBox1.LoadFile("10.rtf", RichTextBoxStreamType.PlainText);
           if (listBox1.SelectedIndex == 10)
           richTextBox1.LoadFile("11.rtf", RichTextBoxStreamType.PlainText);
           if (listBox1.SelectedIndex == 11)
           richTextBox1.LoadFile("12.rtf", RichTextBoxStreamType.PlainText);
           if (listBox1.SelectedIndex == 12)
           richTextBox1.LoadFile("13.rtf", RichTextBoxStreamType.PlainText);
           if (listBox1.SelectedIndex == 13)
           richTextBox1.LoadFile("14.rtf", RichTextBoxStreamType.PlainText);
           if (listBox1.SelectedIndex == 14)
           richTextBox1.LoadFile("15.rtf", RichTextBoxStreamType.PlainText);
           if (listBox1.SelectedIndex == 15)
           richTextBox1.LoadFile("16.rtf", RichTextBoxStreamType.PlainText);
           if (listBox1.SelectedIndex == 16)
           richTextBox1.LoadFile("17.rtf", RichTextBoxStreamType.PlainText);
           if (listBox1.SelectedIndex == 17)
           richTextBox1.LoadFile("18.rtf", RichTextBoxStreamType.PlainText);
           listBox1.Visible = false;
       listBox2.Visible = false;
       button1.Visible = false;
       button2.Visible = false;
       button3.Visible = false;
       button4.Visible = false;
       button5.Visible = false;
       button6.Visible = false;
       button7.Visible = false;
       button8.Visible = false;
       button9.Visible = false;
       pictureBox2.Visible = false;
       pictureBox3.Visible = false;
       label2.Visible = false;
       label1.Visible = false;
       menuStrip1.Visible = false;
       button10.Visible = true;
       button11.Visible = true;
       button12.Visible = true;
       richTextBox1.Visible = true;
  
        }            
        
        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
 
        private void оПрограммеToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form3
            Form3 = new Form3();
            Form3.Show();
            Form3.Location = this.Location;
                    }
 
        private void button5_Click(object sender, EventArgs e)
        {
            listBox1.Visible = false;
            listBox2.Visible = false;
            button7.Visible = true;
            button8.Visible = true;
            button9.Visible = true;
            label2.Visible = false;
           
 
        }
 
        private void наДеньгиToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(" Для пользования этой функцией требуется 100%-е изучение дисциплины 'БД и СУБД' ");
 
        }
 
        private void button4_Click(object sender, EventArgs e)
        {
            Form4
            Form4 = new Form4();
            Form4.Show();
            Form4.Location = this.Location;
            this.Hide();
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            listBox2.Visible = true;
            listBox1.Visible = false;
            button7.Visible = false;
            button8.Visible = false;
            button9.Visible = false;
            label2.Visible = false;
           
        }
 
        private void button7_Click(object sender, EventArgs e)
        {
            Form5
            Form5 = new Form5();
            Form5.Show();
            Form5.Location = this.Location;
            this.Hide();
        }
 
        private void обычныеToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form5
            Form5 = new Form5();
            Form5.Show();
            Form5.Location = this.Location;
            this.Hide();
        }
 
        private void button8_Click(object sender, EventArgs e)
        {
            Form6
            Form6 = new Form6();
            Form6.Show();
            Form6.Location = this.Location;
            this.Hide();
        }
 
        private void button9_Click(object sender, EventArgs e)
        {
            Form7
            Form7 = new Form7();
            Form7.Show();
            Form7.Location = this.Location;
            this.Hide();
        }
 
        private void наВремяToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form6
            Form6 = new Form6();
            Form6.Show();
            Form6.Location = this.Location;
            this.Hide();
        }
 
        private void контрольныеToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form7
            Form7 = new Form7();
            Form7.Show();
            Form7.Location = this.Location;
            this.Hide();
        }
 
        private void button10_Click(object sender, EventArgs e)
        {
            
            button1.Visible = true;
            button2.Visible = true;
            button3.Visible = true;
            button4.Visible = true;
            button5.Visible = true;
            button6.Visible = true;
            pictureBox2.Visible = true;
            pictureBox3.Visible = true;
            label2.Visible = true;
            label1.Visible = true;
            menuStrip1.Visible = true;
            button10.Visible = false;
            button11.Visible = false;
            button12.Visible = false;
            richTextBox1.Visible = false;
        }
 
        private void йСеместрToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listBox1.Visible = true;
            listBox2.Visible = false;
            button7.Visible = false;
            button8.Visible = false;
            button9.Visible = false;
            label2.Visible = false;
        }
 
        private void практическаяРаботаToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listBox2.Visible = true;
            listBox1.Visible = false;
            button7.Visible = false;
            button8.Visible = false;
            button9.Visible = false;
            label2.Visible = false;
        }
 
        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string curItem = listBox2.SelectedItem.ToString();
            curItem = listBox2.SelectedIndex.ToString();
            if (listBox2.SelectedIndex == 0)
                richTextBox1.LoadFile("1.1.rtf", RichTextBoxStreamType.PlainText);
            if (listBox2.SelectedIndex == 1)
                richTextBox1.LoadFile("2.1.rtf", RichTextBoxStreamType.PlainText);
            if (listBox2.SelectedIndex == 2)
                richTextBox1.LoadFile("3.1.rtf", RichTextBoxStreamType.PlainText);
            if (listBox2.SelectedIndex == 3)
                richTextBox1.LoadFile("4.1.rtf", RichTextBoxStreamType.PlainText);
            if (listBox2.SelectedIndex == 4)
                richTextBox1.LoadFile("5.1.rtf", RichTextBoxStreamType.PlainText);
            if (listBox2.SelectedIndex == 5)
                richTextBox1.LoadFile("6.1.rtf", RichTextBoxStreamType.PlainText);
            if (listBox2.SelectedIndex == 6)
                richTextBox1.LoadFile("7.1.rtf", RichTextBoxStreamType.PlainText);
            if (listBox2.SelectedIndex == 7)
                richTextBox1.LoadFile("8.1.rtf", RichTextBoxStreamType.PlainText);
            if (listBox2.SelectedIndex == 8)
                richTextBox1.LoadFile("9.1.rtf", RichTextBoxStreamType.PlainText);
            if (listBox2.SelectedIndex == 9)
                richTextBox1.LoadFile("10.1.rtf", RichTextBoxStreamType.PlainText);
            if (listBox2.SelectedIndex == 10)
                richTextBox1.LoadFile("11.1.rtf", RichTextBoxStreamType.PlainText);
            if (listBox2.SelectedIndex == 11)
                richTextBox1.LoadFile("12.1.rtf", RichTextBoxStreamType.PlainText);
            if (listBox2.SelectedIndex == 12)
                richTextBox1.LoadFile("13.1.rtf", RichTextBoxStreamType.PlainText);
            if (listBox2.SelectedIndex == 13)
                richTextBox1.LoadFile("14.1.rtf", RichTextBoxStreamType.PlainText);
            if (listBox2.SelectedIndex == 14)
                richTextBox1.LoadFile("15.1.rtf", RichTextBoxStreamType.PlainText);
            if (listBox2.SelectedIndex == 15)
                richTextBox1.LoadFile("16.1.rtf", RichTextBoxStreamType.PlainText);
            if (listBox2.SelectedIndex == 16)
                richTextBox1.LoadFile("17.1.rtf", RichTextBoxStreamType.PlainText);
            if (listBox2.SelectedIndex == 17)
                richTextBox1.LoadFile("18.1.rtf", RichTextBoxStreamType.PlainText);
            listBox1.Visible = false;
            listBox2.Visible = false;
            button1.Visible = false;
            button2.Visible = false;
            button3.Visible = false;
            button4.Visible = false;
            button5.Visible = false;
            button6.Visible = false;
            button7.Visible = false;
            button8.Visible = false;
            button9.Visible = false;
            pictureBox2.Visible = false;
            pictureBox3.Visible = false;
            label2.Visible = false;
            label1.Visible = false;
            menuStrip1.Visible = false;
            button10.Visible = true;
            button11.Visible = true;
            button12.Visible = true;
            richTextBox1.Visible = true;
        }
 
        private void button11_Click(object sender, EventArgs e)
        {
 
            int num;
            num = listBox1.SelectedValue;
            num = num - 1;
            listBox1.SelectedItem = num;
                        ;
            
        }
 
        private void button12_Click(object sender, EventArgs e)
        {
 
            int num;
            num = listBox1.SelectedValue;
            num = num + 1;
            listBox1.SelectedItem = num;
          
        }
 
       
 
 
    }
}

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 4.455 из 5
Похожие ответы