Last active
July 3, 2018 16:02
-
-
Save d630/c760d9242063db9cffd18cddacce72c0 to your computer and use it in GitHub Desktop.
Building a simple tui app with https://github.com/migueldeicaza/gui.cs (https://asciinema.org/a/189910)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Test | |
{ | |
public class Auswahl | |
{ | |
public Auswahl(Produkt pro, int anzahl) | |
{ | |
this.Pro = pro; | |
this.Anzahl = anzahl; | |
this.Preis = pro.Preis * anzahl; | |
} | |
public Produkt Pro { get; set; } | |
public int Anzahl { get; set; } | |
public double Preis { get; set; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using Terminal.Gui; | |
namespace Test | |
{ | |
public class Produkt | |
{ | |
public Produkt() | |
{ | |
} | |
public Produkt (int nummer, string bezeichnung, double preis, int bestand) | |
{ | |
this.Nummer = nummer; | |
this.Bezeichnung = bezeichnung; | |
this.Preis = preis; | |
this.Bestand = bestand; | |
} | |
public int Nummer { get; set; } | |
public string Bezeichnung { get; set; } | |
public double Preis { get; set; } | |
public int Bestand { get; set; } | |
public override string ToString() | |
{ | |
return this.Nummer.ToString() + ";" + this.Bezeichnung + ";" + this.Preis.ToString() + ";" + this.Bestand.ToString(); | |
} | |
public List<Produkt> leseDatei(List<Produkt> produkte) | |
{ | |
FileStream fs = null; | |
StreamReader sr = null; | |
string[] fields; | |
try | |
{ | |
fs = new FileStream(Constants.FILENAME, FileMode.Open, FileAccess.Read); | |
sr = new StreamReader(fs); | |
while (sr.Peek() != -1) | |
{ | |
fields = sr.ReadLine().Split(';'); | |
produkte.Add(new Produkt(int.Parse(fields[0]), fields[1], double.Parse(fields[2]), int.Parse(fields[3]))); | |
} | |
} | |
catch (FileNotFoundException fnfeEx) | |
{ | |
MessageBox.ErrorQuery(50, 5, fnfeEx.Message, "ok"); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.ErrorQuery(50, 5, ex.Message, "ok"); | |
} | |
finally | |
{ | |
if (sr != null) | |
sr.Close(); | |
if (fs != null) | |
fs.Close(); | |
} | |
return produkte; | |
} | |
public void schreibeProdukt(Produkt p) | |
{ | |
FileStream fs = null; | |
StreamWriter sw = null; | |
try | |
{ | |
fs = new FileStream(Constants.FILENAME, FileMode.Append, FileAccess.Write); | |
sw = new StreamWriter(fs); | |
sw.WriteLine(p.ToString()); | |
} | |
catch (FileNotFoundException fnfeEx) | |
{ | |
MessageBox.ErrorQuery(50, 5, fnfeEx.Message, "ok"); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.ErrorQuery(50, 5, ex.Message, "ok"); | |
} | |
finally | |
{ | |
if (sw != null) | |
sw.Close(); | |
if (fs != null) | |
fs.Close(); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Terminal.Gui; | |
namespace Test | |
{ | |
static class Program | |
{ | |
static void Main() | |
{ | |
Application.Init(); | |
var tui1 = new Tui1(Application.Top); | |
Application.Run(tui1.Top); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Terminal.Gui; | |
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
namespace Test | |
{ | |
static class Constants | |
{ | |
public const string FILENAME = "/tmp/Produkte.dat"; | |
} | |
public class Tui1 | |
{ | |
List<Produkt> produkte = new List<Produkt>(); | |
List<Auswahl> auswahl = new List<Auswahl>(); | |
List<string> listView1List = new List<string>(); | |
List<string> listView2List = new List<string>(); | |
public Tui1(Toplevel top) | |
{ | |
this.Top = top; | |
this.Tframe = this.Top.Frame; | |
designer(); | |
var p = new Produkt(); | |
this.produkte = p.leseDatei(this.produkte); | |
aktualisiereListView1List(); | |
aktualisiereListView2List(); | |
} | |
public Toplevel Top { get; set;} | |
public Rect Tframe { get; set;} | |
private double Gesamtpreis { get; set; } | |
private Window win1 = null; | |
private Window win2 = null; | |
private ListView listView1 = null; | |
private ListView listView2 = null; | |
private Label label1 = null; | |
private Label label2 = null; | |
private Label label3 = null; | |
private Label label4 = null; | |
private Label label5 = null; | |
private Label label6 = null; | |
private Button button1 = null; | |
private Button button2 = null; | |
private Button button3 = null; | |
private Button button4 = null; | |
private Button button5 = null; | |
private Button button6 = null; | |
private Button button7 = null; | |
private TextField textField1 = null; | |
private TextField textField2 = null; | |
private TextField textField3 = null; | |
private TextField textField4 = null; | |
private TextField textField5 = null; | |
private void designer() | |
{ | |
this.win1 = new Window("Produkte") { | |
X = Pos.Percent(0), | |
Y = Pos.Percent(0), | |
Width = Dim.Fill(), | |
Height = Dim.Percent(50) | |
}; | |
this.win2 = new Window("Auswahl") { | |
X = Pos.Percent(0), | |
Y = Pos.Percent(50), | |
Width = Dim.Fill(), | |
Height = Dim.Percent(50) | |
}; | |
// win1 | |
this.listView1 = new ListView(new Rect (0, 0, 20, 10), listView1List) { | |
AllowsMarking = true | |
}; | |
this.listView1.SelectedChanged += new Action(listView1__SelectedChangedEvent); | |
this.label1 = new Label("Nummer") { | |
X = 22, | |
Y = 0, | |
Width = 20, | |
Height = 1 | |
}; | |
this.textField1 = new TextField(22, 1, 20, "int"); | |
this.label2 = new Label("Bezeichnung") { | |
X = 22, | |
Y = 2, | |
Width = 20, | |
Height = 1 | |
}; | |
this.textField2 = new TextField(22, 3, 20, "string"); | |
this.label3 = new Label("Preis") { | |
X = 22, | |
Y = 4, | |
Width = 20, | |
Height = 1 | |
}; | |
this.textField3 = new TextField(22, 5, 20, "double"); | |
this.label4 = new Label("Bestand") { | |
X = 22, | |
Y = 6, | |
Width = 20, | |
Height = 1 | |
}; | |
this.textField4 = new TextField(22, 7, 20, "int"); | |
this.button1 = new Button(44, 0, "new ") { | |
Clicked = () => button1__newAction() | |
}; | |
this.button2 = new Button(44, 1, "save") { | |
Clicked = () => button2__saveAction() | |
}; | |
this.button5 = new Button(44, 2, "Auswahl") { | |
Clicked = () => this.Top.SetFocus(win2) | |
}; | |
this.button7 = new Button(44, 3, "quit") { | |
Clicked = () => Application.RequestStop() | |
}; | |
this.win1.Add(listView1, label1, textField1, label2, textField2, label3, textField3, label4, textField4, button1, button2, button5, button7); | |
// win2 | |
this.listView2 = new ListView(new Rect(0, 0, 20, 10), listView2List) { | |
AllowsMarking = true | |
}; | |
this.listView2.SelectedChanged += new Action(listView2__SelectedChangedEvent); | |
this.label5 = new Label("Anzahl") { | |
X = 22, | |
Y = 0, | |
Width = 20, | |
Height = 1 | |
}; | |
this.textField5 = new TextField(22, 1, 20, "int"); | |
this.button3 = new Button(44, 0, "new") { | |
Clicked = () => button3__newAction() | |
}; | |
this.button4 = new Button(44, 1, "buy") { | |
Clicked = () => button4__buyAction() | |
}; | |
this.button6 = new Button(44, 2, "Produkte") { | |
Clicked = () => this.Top.SetFocus(win1) | |
}; | |
this.label6 = new Label("Gesamtsumme: xxx") { | |
X = 44, | |
Y = 5, | |
Width = 40, | |
Height = 1 | |
}; | |
this.win2.Add(listView2, label5, textField5, button3, button4, label6, button6); | |
// zack | |
this.Top.Add(win1, win2); | |
} | |
private void aktualisiereListView1List() | |
{ | |
this.listView1List.Clear(); | |
this.listView1List.Add("Nummer, Bezeichnung"); | |
foreach (Produkt p in this.produkte) | |
{ | |
this.listView1List.Add(p.Nummer + ", " + p.Bezeichnung); | |
} | |
} | |
private void button1__newAction() | |
{ | |
this.textField1.Text = string.Empty; | |
this.textField2.Text = string.Empty; | |
this.textField3.Text = string.Empty; | |
this.textField4.Text = string.Empty; | |
this.listView1.SelectedItem = 0; | |
} | |
private void button2__saveAction() | |
{ | |
if (this.listView1.SelectedItem != 0) | |
{ | |
MessageBox.ErrorQuery(80, 5, "Error", "Es darf kein Eintrag in der Listbox ausgewaehlt sein", "ok"); | |
return; | |
} | |
if (this.textField1.Text.IsEmpty || this.textField2.Text.IsEmpty || this.textField3.Text.IsEmpty || this.textField4.Text.IsEmpty) | |
{ | |
MessageBox.ErrorQuery(80, 5, "Error", "Alle Felder ausfuellen (int, string, double, int)", "ok"); | |
return; | |
} | |
Produkt p = null; | |
try | |
{ | |
p = new Produkt(Convert.ToInt32(this.textField1.Text.ToString()), this.textField2.Text.ToString(), Convert.ToDouble(this.textField3.Text.ToString()), Convert.ToInt32(this.textField4.Text.ToString())); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.ErrorQuery(80, 5, "Error", ex.Message, "ok"); | |
return; | |
} | |
p.schreibeProdukt(p); | |
this.produkte.Add(p); | |
aktualisiereListView1List(); | |
this.listView1.Redraw(new Rect(0, 0, 20, 10)); | |
this.listView1.SelectedItem = this.listView1List.Count - 1; | |
} | |
private void listView1__SelectedChangedEvent() | |
{ | |
if (this.listView1.SelectedItem == 0) | |
{ | |
this.textField1.Text = "int"; | |
this.textField2.Text = "string"; | |
this.textField3.Text = "double"; | |
this.textField4.Text = "int"; | |
} | |
else | |
{ | |
this.textField1.Text = produkte[this.listView1.SelectedItem - 1].Nummer.ToString(); | |
this.textField2.Text = produkte[this.listView1.SelectedItem - 1].Bezeichnung; | |
this.textField3.Text = produkte[this.listView1.SelectedItem - 1].Preis.ToString(); | |
this.textField4.Text = produkte[this.listView1.SelectedItem - 1].Bestand.ToString(); | |
} | |
} | |
private void aktualisiereListView2List() | |
{ | |
this.listView2List.Clear(); | |
this.listView2List.Add("Bezeichnung, Anzahl, Preis"); | |
foreach (Auswahl a in this.auswahl) | |
{ | |
this.listView2List.Add(a.Pro.Bezeichnung + ", " + a.Anzahl + "," + a.Preis); | |
} | |
} | |
private void button3__newAction() | |
{ | |
this.auswahl.Clear(); | |
aktualisiereListView2List(); | |
this.listView2.Redraw(new Rect(0, 0, 20, 10)); | |
this.Gesamtpreis = 0.0; | |
this.textField5.Text = "int"; | |
this.label6.Text = "Gesamtpreis: xxx"; | |
//this.listView2.SelectedItem = 0; | |
} | |
private void button4__buyAction() | |
{ | |
if (this.listView1.SelectedItem == 0) | |
{ | |
MessageBox.ErrorQuery(80, 5, "Error", "Es muss ein Produkt ausgewaehlt sein", "ok"); | |
return; | |
} | |
try | |
{ | |
if (Convert.ToInt32(this.textField5.Text.ToString()) == 0 || this.textField5.Text.IsEmpty) | |
{ | |
MessageBox.ErrorQuery(80, 5, "Error", "Auswahl muss korrekt sein (int). Und groesser 0", "ok"); | |
return; | |
} | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.ErrorQuery(80, 5, "Error", ex.Message, "ok"); | |
return; | |
} | |
if (Convert.ToInt32(this.textField5.Text) > this.produkte[listView1.SelectedItem - 1].Bestand) | |
{ | |
MessageBox.ErrorQuery(80, 5, "Error", "Lagerbestand zu klein", "ok"); | |
} | |
this.auswahl.Add(new Auswahl(this.produkte[listView1.SelectedItem - 1], Convert.ToInt32(this.textField5.Text))); | |
aktualisiereListView2List(); | |
this.listView2.Redraw(new Rect(0, 0, 20, 30)); | |
this.listView2.SelectedItem = this.listView2List.Count - 1; | |
this.Gesamtpreis = 0.0; | |
foreach (Auswahl a in auswahl) | |
this.Gesamtpreis += a.Preis; | |
this.label6.Text = "Gesamtpreis: " + this.Gesamtpreis.ToString(); | |
} | |
private void listView2__SelectedChangedEvent() | |
{ | |
if (this.listView2.SelectedItem == 0) | |
{ | |
this.textField5.Text = "int"; | |
} | |
else | |
{ | |
this.textField5.Text = this.auswahl[this.listView2.SelectedItem - 1].Anzahl.ToString(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO