-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
144 lines (128 loc) · 4.99 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace MedSQL_Reader
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
dataGridView.ReadOnly = true;
KeyPreview = true;
KeyDown += MainForm_KeyDown;
}
private void btnOpenFile_Click(object sender, EventArgs e)
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog.FileName;
try
{
using (SQLiteConnection connection = new SQLiteConnection($"Data Source={filePath};Version=3;"))
{
connection.Open();
string tableName = "ZKARTEIEINTRAG";
string query = $"SELECT [ZIDENT], [ZADDITIONALTEXT], [ZTEXT] FROM [{tableName}] ORDER BY [ZIDENT] ASC";
SQLiteDataAdapter adapter = new SQLiteDataAdapter(query, connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
dataGridView.DataSource = dataTable;
dataGridView.Columns["ZIDENT"].Width = 135;
dataGridView.Columns["ZTEXT"].Width = 271;
dataGridView.Columns["ZADDITIONALTEXT"].Width = 271;
}
}
catch (SQLiteException ex)
{
MessageBox.Show($"SQLite Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
DisplayContent(e.RowIndex, e.ColumnIndex);
}
}
private void dataGridView_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
int rowIndex = dataGridView.SelectedCells[0].RowIndex;
int colIndex = dataGridView.SelectedCells[0].ColumnIndex;
DisplayContent(rowIndex, colIndex);
}
}
private void DisplayContent(int rowIndex, int colIndex)
{
string columnName = dataGridView.Columns[colIndex].Name;
string content = dataGridView.Rows[rowIndex].Cells[columnName].Value.ToString();
panelTextFile.Controls.Clear();
if (IsRtf(content))
{
RichTextBox richTextBox = new RichTextBox();
richTextBox.Dock = DockStyle.Fill;
richTextBox.Rtf = content;
panelTextFile.Controls.Add(richTextBox);
}
else if (IsCsv(content))
{
TextBox textBox = new TextBox();
textBox.Dock = DockStyle.Fill;
textBox.Multiline = true;
textBox.ScrollBars = ScrollBars.Vertical;
textBox.ReadOnly = true;
// Check if the content is in ZTX format (the ZTX-Format is normally a Unity thing, but I couldn't think of a different name.)
ZTXFormat ztxFormat = new ZTXFormat();
if (ztxFormat.IsValidZTX(content))
{
List<Dictionary<string, string>> entries = ztxFormat.ParseLines(content);
string formattedContent = ztxFormat.FormatContent(entries);
textBox.Text = formattedContent;
}
else
{
textBox.Text = content;
}
panelTextFile.Controls.Add(textBox);
}
else
{
TextBox textBox = new TextBox();
textBox.Dock = DockStyle.Fill;
textBox.Multiline = true;
textBox.ScrollBars = ScrollBars.Vertical;
textBox.ReadOnly = true;
textBox.Text = content;
panelTextFile.Controls.Add(textBox);
}
}
private bool IsRtf(string text)
{
return text.StartsWith("{\\rtf");
}
private bool IsCsv(string text)
{
return text.Contains(",") || text.Contains("\t");
}
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
int rowIndex = dataGridView.SelectedCells[0].RowIndex;
int colIndex = dataGridView.SelectedCells[0].ColumnIndex;
DisplayContent(rowIndex, colIndex);
}
}
}
}