Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Xml;
- using System.IO;
- using Word = Microsoft.Office.Interop.Word;
- using System.Runtime.InteropServices.ComTypes;
- using System.Reflection;
- using System.Data.SQLite;
- // !!! НАЗВАНИЕ документа должно совпадать с названием вершины и с названием столбцы в бд name_srs.
- // !!! Для того, чтобы лаб.работы открылась нужно чтобы в названии вершины дерева содержалось "Лаб"
- namespace Programm
- {
- public partial class TV : Form
- {
- SQLiteConnection sCon;
- public string name_srs = ""; //нужен для того, чтобы при переходе между срс варианты не менялись
- MainWindow _MainWindowForm; //для кнопки назад
- public TV(MainWindow MainWindowForm)
- {
- InitializeComponent();
- StartConnection();
- TreeNodeMouseClickEventArgs e = null;
- _MainWindowForm = MainWindowForm; //для кнопки назад
- string fileName = "example.xml";
- //XmlTextReader reader = new XmlTextReader(fileName);
- Add(sender,"example.xml",e);
- }
- private bool StartConnection() //Подключение к БД
- {
- String StringConnection = @"Data Source = DataBase.sqlite;Version=3;";
- sCon = new SQLiteConnection(StringConnection);
- try
- {
- sCon.Open();
- return true;
- }
- catch
- {
- MessageBox.Show("Not opened");
- return false;
- }
- }
- public void Add(object sender, String fileName, EventArgs e)
- {
- ClassesTreeView.Nodes.Clear(); //почистили treeview чтобы открыть другой
- XmlTextReader reader = new XmlTextReader(fileName);
- try
- {
- ClassesTreeView.BeginUpdate();
- TreeNode parentNode = null;
- while (reader.Read()) //пока идет считывание
- {
- if (reader.NodeType == XmlNodeType.Element)
- {
- if (reader.Name == "Вершина")
- {
- TreeNode newNode = new TreeNode();
- bool isEmptyElement = reader.IsEmptyElement;
- int attributeCount = reader.AttributeCount;
- if (attributeCount > 0)
- {
- for (int i = 0; i < attributeCount; i++)
- {
- if (i == 0)
- {
- reader.MoveToAttribute(i);
- newNode.Text = reader.Value;
- }
- else
- {
- reader.MoveToAttribute(i);
- newNode.Tag = reader.Value;
- }
- }
- }
- // add new node to Parent Node or TreeView
- if (parentNode != null) parentNode.Nodes.Add(newNode);
- else ClassesTreeView.Nodes.Add(newNode);
- // making current node 'ParentNode' if its not empty
- if (!isEmptyElement)
- {
- parentNode = newNode;
- }
- }
- }
- else if (reader.NodeType == XmlNodeType.EndElement)
- {
- if (reader.Name == "Вершина")
- {
- parentNode = parentNode.Parent;
- }
- }
- else if (reader.NodeType == XmlNodeType.XmlDeclaration)
- {
- //Ignore Xml Declaration
- }
- else if (reader.NodeType == XmlNodeType.None)
- {
- return;
- }
- else if (reader.NodeType == XmlNodeType.Text)
- {
- parentNode.Nodes.Add(reader.Value);
- }
- // moving up to in TreeView if end tag is encountered
- }
- }
- finally
- {
- // enabling redrawing of treeview after all nodes are added
- ClassesTreeView.EndUpdate();
- reader.Close();
- }
- }
- public void ClassesTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) // открытие ворда по нажатию на узел
- {
- TV_Word word_text = new TV_Word();
- TheoryText.Text = word_text.ShowWord(e.Node.Text); //возращает totaltext, где хранится весь текст ворда
- GetData(e);
- }
- public void GetData(TreeNodeMouseClickEventArgs e) //выводим данные из БД
- {
- string srs;
- try
- {
- srs = e.Node.Text; // запоминаем узел, который щелкнули
- }
- catch
- {
- MessageBox.Show("Кажется что-то пошло не так...");
- return;
- }
- if (srs.IndexOf("Лаб") != -1) //Если есть фраза "Лаб", то обращаемся к БД
- {
- // считаем количество вариантов, которые относятся в выбранной Срс
- string queryCount = @"select count(var) from CRC where name_srs= @name_srs_list";
- SQLiteCommand commandCount = new SQLiteCommand(queryCount, sCon);
- commandCount.Parameters.Add(new SQLiteParameter("@name_srs_list", srs));
- int Count = Convert.ToInt32(commandCount.ExecuteScalar());
- int x;
- if (Count != 0)
- {
- Random rand = new Random();
- int RandomCount = rand.Next(1, Count + 1); //выбираем случайный варинт для данной срс
- // Выводим случайны вариант, выбранной срс на экран
- string query = @"select text_srs FROM CRC WHERE name_srs= @name_srs_list and var=@number_var";
- SQLiteCommand command = new SQLiteCommand(query, sCon);
- command.Parameters.Add(new SQLiteParameter("@name_srs_list", srs));
- command.Parameters.Add(new SQLiteParameter("@number_var", RandomCount));
- SQLiteDataReader reader = command.ExecuteReader();
- //Считываем лабу из БД
- if (LabText.Text == "")
- {
- name_srs = srs;
- while (reader.Read())
- {
- LabText.Text += reader["text_srs"] + "\n";
- }
- }
- //Пересчитываем, только если название лабы сменилось.
- if (name_srs != srs)
- {
- name_srs = srs;
- LabText.Clear();
- while (reader.Read())
- {
- LabText.Text += reader["text_srs"] + "\n";
- }
- }
- }
- }
- }
- private void btnOpen_Click(object sender, EventArgs e)//выбор XML
- {
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- Add(sender, openFileDialog1.FileName, e);
- }
- }
- private void btnClose_Click(object sender, EventArgs e)//кнопка назад
- {
- //this.Hide();
- //_MainWindowForm.Show();
- }
- private void LoadSolution_Click(object sender, EventArgs e)
- {
- MessageBox.Show("Coming soon", "Oooops", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- private void button1_Click(object sender, EventArgs e)
- {
- MessageBox.Show("Coming soon", "Oooops", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- private void tabs_Selecting(object sender, TabControlCancelEventArgs e)
- {
- int temp_tab = tabs.SelectedIndex;
- // НАДО СДЕЛАТЬ В названии срс ОБЯЗАТЕЛЬНО должна содержаться фраза "Лаб" столбец name_srs, иначе к БД программа не будет обращаться
- if (tabs.SelectedIndex == 2) //тест
- {
- //уведомление о переходе
- const string message = "Вы уверены, что хотете перейти к тесту? Теория будет недоступна";
- const string caption = "Error Detected in Input";
- var result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
- if (result == DialogResult.No) e.Cancel=true;
- else
- {
- tabs.TabPages[0].Parent = null; //закрыли теорию
- tabs.TabPages[0].Parent = null; //закрыли лабу
- }
- }
- }
- private void pictureBox1_Click(object sender, EventArgs e)
- {
- this.Hide();
- _MainWindowForm.Show();
- }
- private void btnCopy_Click(object sender, EventArgs e)
- {
- MessageBox.Show("Coming soon", "Oooops", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement