Programm_Wirl/UserControls/UCTourenplan.cs

139 lines
3.7 KiB
C#

using DatenDB;
using Deckungsbeitrag.AA_Klassen;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Deckungsbeitrag.UserControls
{
public partial class UCTourenplan : UserControl
{
private static string savePath = ConfigurationManager.AppSettings["DateiSavePfad"];
private Benutzer benutzer;
private string[] touren;
public (DateTime datum, string text, string[] touren) lieferDaten;
public UCTourenplan()
{
InitializeComponent();
}
public UCTourenplan(Benutzer benutzer) : this()
{
this.benutzer = benutzer;
}
private void UCTourenplan_Load(object sender, EventArgs e)
{
ReadCSV();
}
/// <summary>
/// Bereitgestellten Tourenplan in .CSV lesen und den Controls des TableLayoutPanels zuordnen
/// </summary>
private void ReadCSV()
{
string updatePath = Path.Combine(savePath, "Listen");
string dateiName = "Tourenplan_Aktuell.csv";
string row;
string[] rows;
try
{
if (File.Exists(Path.Combine(updatePath, dateiName)))
{
string filename = Path.Combine(updatePath, dateiName);
StreamReader sr = new StreamReader(filename, Encoding.GetEncoding("iso-8859-1"));
row = string.Empty;
rows = new string[0];
int idx = 0;
while (!sr.EndOfStream)
{
Array.Resize<string>(ref rows, ++idx);
row = sr.ReadLine();
rows[idx - 1] = row;
}
sr.Close();
int i = 0;
touren = new string[rows.Length - 2];
foreach (string line in rows)
{
if (i == 0) { i++; continue; }
string[] linedata = line.Split(';');
for (int j = 0; j < this.tableLPTourenplan.ColumnCount; j++)
{
var control = this.tableLPTourenplan.GetControlFromPosition(j, i - 1);
control.Text = linedata[j].Replace(",", "\n\n");
if (i >= 2 & j == 0) touren[i - 2] = linedata[0];
}
i++;
}
this.tableLPTourenplan = RemoveTLPanelRow(i - 1, tableLPTourenplan);
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Nicht benötigte Zeilen des TableLayoutPanels entfernen.
/// </summary>
/// <param name="lines"></param>
/// <param name="tlp"></param>
/// <returns>TableLayoutPanel ohne nicht benutzten Zeilen</returns>
private TableLayoutPanel RemoveTLPanelRow(int lines, TableLayoutPanel tlp)
{
for (int i = lines; i <= tlp.RowCount; i++)
{
for (int col = 0; col < tlp.ColumnCount; col++)
{
var ctl = tlp.GetControlFromPosition(col, i);
if (ctl != null)
tlp.Controls.Remove(ctl);
}
}
while (tlp.RowCount > lines)
{
tlp.RowStyles.RemoveAt(tlp.RowCount - 1);
tlp.RowCount--;
}
return tlp;
}
private void Button_Click(object sender, EventArgs e)
{
var btn = (Button)sender;
var tlp = (TableLayoutPanel)btn.Parent;
TableLayoutPanelCellPosition tlpCellPosition = tlp.GetCellPosition(btn);
int row = tlpCellPosition.Row;
int col = tlpCellPosition.Column;
DateTime liefertag = Funktionen.GetNextLiefertag((Liefertage)(1 << col));
string tour = touren[row - 1];
lieferDaten = (liefertag, tour, touren);
if (this.ParentForm != null)
{
this.ParentForm.DialogResult = DialogResult.OK;
this.ParentForm?.Close();
}
}
}
}