Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Form1.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.IO;
- using System.Net;
- using System.Text;
- using System.Windows.Forms;
- //using Excel = Microsoft.Office.Interop.Excel;
- namespace KerdoivKitolto
- {
- public partial class Form1 : Form
- {
- public string[] sorok;
- public int lastPos;
- public Kerdoiv kerdoiv;
- public Button keszButton;
- public string fajlnev;
- public List<int> IDLista;
- public int[] inttemp;
- public string[] temp;
- bool voltmar;
- public Form1()
- {
- InitializeComponent();
- listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;
- listBox1.MouseDoubleClick += listBox1_MouseDoubleClick;
- }
- void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
- {
- if (listBox1.SelectedIndex >= 0)
- {
- button1_Click(sender, e);
- }
- }
- void listBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (listBox1.SelectedIndex >= 0)
- button1.Enabled = true;
- }
- private void megnyitásToolStripMenuItem_Click(object sender, EventArgs e)
- {
- try
- {
- mezőkVisszaállításaToolStripMenuItem.Enabled = false;
- this.Enabled = false;
- if (voltmar)
- {
- Controls.Clear();
- InitializeComponent();
- Refresh();
- listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;
- listBox1.MouseDoubleClick += listBox1_MouseDoubleClick;
- }
- IDLista = new List<int>(); // dirContent!
- button1.Enabled = false;
- temp = FTP.GetDirectoryContent("");
- foreach (var x in temp)
- {
- if (x.Contains(".txt"))
- {
- IDLista.Add(int.Parse(x.Split('.')[0]));
- }
- }
- FTP.GetIDContent();
- temp = FTP.IDContent.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
- inttemp = new int[temp.Length];
- for (int i = 0; i < temp.Length; i++)
- {
- inttemp[i] = int.Parse(temp[i].Split('|')[0]);
- temp[i] = temp[i].Split('|')[1];
- }
- for (int i = 0; i < IDLista.Count; i++)
- {
- for (int j = 0; j < inttemp.Length; j++)
- {
- if (IDLista[i] == inttemp[j])
- {
- listBox1.Items.Add(temp[i]);
- }
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message + '\n' + ex.ToString().Substring(ex.ToString().LastIndexOf(':') + 1));
- }
- finally
- {
- this.Enabled = true;
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (listBox1.SelectedIndex < 0)
- MessageBox.Show("Válassz egy kérdőívet a listából!");
- else
- {
- try
- {
- label2.Enabled = false;
- label2.Visible = false;
- mezőkVisszaállításaToolStripMenuItem.Enabled = true;
- Refresh();
- fajlnev = IDLista[listBox1.SelectedIndex] + ".txt";
- //FTP.ID = IDLista[listBox1.SelectedIndex];
- sorok = FTP.DownloadFile("", fajlnev);
- KerdoivPeldanyositas();
- Kirajzolas();
- megnyitásToolStripMenuItem.Enabled = true;
- listBox1.Visible = false;
- listBox1.Enabled = false;
- button1.Enabled = false;
- button1.Visible = false;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message + '\n' + ex.ToString().Substring(ex.ToString().LastIndexOf(':')));
- }
- }
- }
- private void Kirajzolas()
- {
- int tabindex = 0;
- voltmar = true;
- lastPos = 35;
- ClientSize = new System.Drawing.Size(500, 530);
- Label cim = new Label();
- cim.TabIndex = tabindex;
- tabindex++;
- cim.Text = kerdoiv.cim;
- cim.AutoSize = true;
- cim.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- cim.Location = new System.Drawing.Point(48, lastPos);
- this.Controls.Add(cim);
- lastPos += 25;
- label2.Visible = false;
- label1.Visible = true;
- foreach (var k in kerdoiv.kerdesLista)
- {
- int _last = 20;
- GroupBox box = new GroupBox();
- box.TabStop = true;
- box.TabIndex = tabindex;
- tabindex++;
- box.Location = new System.Drawing.Point(15, lastPos);
- box.Size = new System.Drawing.Size(431, k.hanykerdes * 35);
- box.Text = k.kerdes;
- this.Controls.Add(box);
- lastPos += 5;
- if (k.tobblehetoseg)
- {
- for (int i = 0; i < k.hanykerdes; i++)
- {
- k.checks[i] = new CheckBox();
- k.checks[i].TabIndex = tabindex;
- tabindex++;
- k.checks[i].TabStop = true;
- k.checks[i].Text = k.valaszok[i];
- k.checks[i].Location = new System.Drawing.Point(10, _last);
- _last += 25;
- box.Controls.Add(k.checks[i]);
- }
- }
- else
- {
- for (int i = 0; i < k.hanykerdes; i++)
- {
- k.radios[i] = new RadioButton();
- k.radios[i].TabIndex = tabindex;
- tabindex++;
- k.radios[i].TabStop = true;
- k.radios[i].Text = k.valaszok[i];
- k.radios[i].Location = new System.Drawing.Point(10, _last);
- _last += 25;
- box.Controls.Add(k.radios[i]);
- }
- }
- lastPos += k.hanykerdes * 40;
- }
- keszButton = new Button();
- keszButton.Text = "Kész!";
- keszButton.Size = new System.Drawing.Size(150, 40);
- keszButton.Location = new System.Drawing.Point((this.ClientRectangle.Width / 2) - keszButton.Size.Width / 2 - 20, lastPos + 10);
- keszButton.AutoSize = true;
- keszButton.TabIndex = tabindex;
- tabindex++;
- this.Controls.Add(keszButton);
- keszButton.Click += keszButton_Click;
- }
- // private void ExcelMentes()
- //{
- // Excel.Application xlApp;
- // Excel.Workbook xlWorkBook;
- // Excel.Worksheet xlWorkSheet;
- // object misValue = System.Reflection.Missing.Value;
- // xlApp = new Excel.Application();
- // xlWorkBook = xlApp.Workbooks.Add(misValue);
- // xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
- // xlWorkSheet.Cells[1, 1] = kerdoiv.cim;
- // for (int i = 0; i < kerdoiv.kerdesLista.Count; i++)
- // {
- // xlWorkSheet.Cells[i + 2, 1] = kerdoiv.kerdesLista[i].kerdes;
- // if (kerdoiv.kerdesLista[i].tobblehetoseg)
- // {
- // int vissza = 0;
- // for (int j = 0; j < kerdoiv.kerdesLista[i].hanykerdes; j++)
- // {
- // if (kerdoiv.kerdesLista[i].tobbvalasz[j])
- // {
- // xlWorkSheet.Cells[i + 2, j + 2 - vissza] = kerdoiv.kerdesLista[i].valaszok[j];
- // }
- // else
- // {
- // vissza++;
- // continue;
- // }
- // }
- // }
- // else
- // {
- // xlWorkSheet.Cells[i + 2, 2] = kerdoiv.kerdesLista[i].valaszok[kerdoiv.kerdesLista[i].valasz];
- // }
- // }
- // try
- // {
- // fajlnev = fajlnev.Split('.')[0];
- // xlWorkBook.SaveAs(fajlnev + ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
- // MessageBox.Show("Az Excel fájl elkészült:\n\n " + fajlnev);
- // }
- // catch (Exception ex)
- // {
- // MessageBox.Show(ex.Message + '\n' + ex.ToString().Substring(ex.ToString().LastIndexOf(':') + 1));
- // }
- // finally
- // {
- // xlWorkBook.Close(true, misValue, misValue);
- // xlApp.Quit();
- // releaseObject(xlWorkSheet);
- // releaseObject(xlWorkBook);
- // releaseObject(xlApp);
- // }
- //}
- private void releaseObject(object obj)
- {
- try
- {
- System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
- obj = null;
- }
- catch (Exception ex)
- {
- obj = null;
- MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
- }
- finally
- {
- GC.Collect();
- }
- }
- public void KerdoivPeldanyositas()
- {
- kerdoiv.kerdesLista = new List<Kerdes>();
- kerdoiv.cim = sorok[0];
- Kerdes k = new Kerdes();
- string[] temp;
- for (int i = 2; i < sorok.Length; i++)
- {
- if (sorok[i] == "--")
- {
- k = new Kerdes();
- continue;
- }
- temp = sorok[i].Split(':');
- k.hanyadik = Convert.ToInt32(temp[0]);
- k.kerdes = temp[1];
- i++;
- k.valaszok[0] = sorok[i];
- i++;
- k.valaszok[1] = sorok[i];
- i++;
- k.valaszok[2] = sorok[i];
- i++;
- k.valaszok[3] = sorok[i];
- i++;
- k.hanykerdes = Convert.ToInt32(sorok[i]);
- i++;
- k.tobblehetoseg = Convert.ToBoolean(sorok[i]);
- kerdoiv.kerdesLista.Add(k);
- }
- }
- private void kilépésToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (MessageBox.Show("Biztosan szeretné zárni a kérdőív kitöltőt?", "Biztos?", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.OK)
- {
- Environment.Exit(-1);
- }
- }
- private void mezőkVisszaállításaToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Controls.Clear();
- this.Controls.Add(this.label2);
- this.Controls.Add(this.label1);
- this.Controls.Add(this.menuStrip1);
- this.ResumeLayout(false);
- Kirajzolas();
- }
- void keszButton_Click(object sender, EventArgs e)
- {
- if (!MindenholVaneValasz())
- {
- MessageBox.Show("Kötelező mindenhol megjelölni egy választ!", "Figyelem!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- return;
- }
- int index = 0;
- try
- {
- this.Enabled = false;
- ValaszokMentese(); // Kérdésekben eltárolva a válasz //
- List<string> eredmenyLista = new List<string>();
- eredmenyLista.Add(kerdoiv.cim);
- foreach (var k in kerdoiv.kerdesLista)
- {
- eredmenyLista.Add("--");
- eredmenyLista.Add(k.kerdes);
- if (!k.tobblehetoseg) // radio
- {
- for (int i = 0; i < k.radios.Length; i++)
- {
- if (i == k.valasz)
- eredmenyLista.Add("1");
- else eredmenyLista.Add("0");
- }
- }
- else // check
- {
- for (int i = 0; i < k.checks.Length; i++)
- {
- if (k.tobbvalasz[i]) eredmenyLista.Add("1");
- else eredmenyLista.Add("0");
- }
- }
- }
- string eredmeny;
- bool letezik = false;
- temp = FTP.GetDirectoryContent("stat");
- foreach (var x in temp)
- {
- if (x.Contains(fajlnev))
- {
- letezik = true;
- break;
- }
- }
- if (!letezik)
- {
- eredmeny = string.Join("\n", eredmenyLista);
- FTP.SaveFile(eredmeny, fajlnev, "stat");
- }
- else
- {
- string[] statfile = FTP.DownloadFile("stat", fajlnev);
- if (statfile[0] == eredmenyLista[0])
- {
- int szam = 0;
- for (int i = 1; i < statfile.Length; i++)
- {
- if (eredmenyLista[i] == "--")
- {
- i++;
- continue;
- }
- else
- {
- i++;
- index = i;
- szam = Convert.ToInt32(statfile[i]) + Convert.ToInt32(eredmenyLista[i]);
- eredmenyLista[i] = szam.ToString();
- }
- }
- eredmeny = string.Join("\n", eredmenyLista);
- FTP.SaveFile(eredmeny, fajlnev, "stat");
- }
- }
- MessageBox.Show("Sikeres mentés!", "Juhé!", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message + " -- INDEX IS " + index + "\n" + ex.ToString().Substring(ex.ToString().LastIndexOf(':') + 1));
- }
- finally
- {
- this.Enabled = true;
- }
- }
- public bool MindenholVaneValasz()
- {
- int count;
- foreach (var k in kerdoiv.kerdesLista)
- {
- count = 0;
- if (!k.tobblehetoseg)
- {
- for (int i = 0; i < k.hanykerdes; i++)
- {
- if (!k.radios[i].Checked) count++;
- }
- }
- else
- {
- for (int i = 0; i < k.hanykerdes; i++)
- {
- if (!k.checks[i].Checked) count++;
- }
- }
- if (count == k.hanykerdes) return false;
- }
- return true;
- }
- private void ValaszokMentese()
- {
- foreach (var k in kerdoiv.kerdesLista)
- {
- if (!k.tobblehetoseg) // radio
- {
- for (int i = 0; i < k.radios.Length; i++)
- {
- if (k.radios[i] != null)
- {
- if (k.radios[i].Checked)
- k.valasz = i;
- }
- }
- }
- else // checkbox
- {
- for (int i = 0; i < k.checks.Length; i++)
- {
- if (k.checks[i] != null)
- {
- if (k.checks[i].Checked) k.tobbvalasz[i] = true;
- }
- }
- }
- }
- }
- }
- }
- // FTP.cs:
- using System.IO;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- using System;
- using System.Windows.Forms;
- namespace KerdoivKitolto
- {
- public static class FTP
- {
- static StreamReader reader;
- static FtpWebResponse response;
- public static int ID;
- public static string IDContent;
- static string dirContent;
- public static string[] GetDirectoryContent(string directory)
- {
- var request = (FtpWebRequest)WebRequest.Create
- (Path.Combine("ftp://185.27.134.8/htdocs/data/", directory));
- request.Credentials = new NetworkCredential("b11_13574135", "csaki123");
- request.Method = WebRequestMethods.Ftp.ListDirectory;
- response = (FtpWebResponse)request.GetResponse();
- Stream responseStream = response.GetResponseStream();
- reader = new StreamReader(responseStream);
- dirContent = reader.ReadToEnd();
- reader.Close();
- response.Close();
- return dirContent.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
- }
- public static void SaveFile(string Content, string Filename, string directory)
- {
- try
- {
- Uri requestUri = new Uri(Path.Combine("ftp://185.27.134.8/htdocs/data/", directory, Filename));
- FtpWebRequest request = (FtpWebRequest)WebRequest.Create(requestUri);
- request.Credentials = new NetworkCredential("b11_13574135", "csaki123");
- request.KeepAlive = false;
- request.Method = "STOR";
- request.UseBinary = true;
- request.ContentLength = Content.Length;
- request.Proxy = null;
- using (Stream stream = request.GetRequestStream())
- {
- byte[] bytes = new UTF8Encoding().GetBytes(Content);
- //stream.Write(bytes, 0, bytes.Length);
- for (int i = 0; i < bytes.Length; i++)
- {
- stream.WriteByte(bytes[i]);
- }
- }
- request.GetResponse();
- }
- catch (Exception ex)
- {
- MessageBox.Show("Hiba történt a fájl mentése során!\n\n" + ex.Message + '\n' + ex.ToString().Substring(ex.ToString().LastIndexOf(':') + 1), "Hiba", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- public static void SelectID()
- {
- CheckIDFileExists();
- GetIDContent();
- if (IDContent == null) throw new FileNotFoundException();
- if (IDContent == String.Empty || IDContent == "")
- {
- ID = -1;
- }
- else
- {
- string[] tmp = IDContent.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
- ID = Convert.ToInt32(tmp[tmp.Length - 1].Split('|')[0]);
- }
- ID++;
- }
- public static void GetIDContent()
- {
- var request = (FtpWebRequest)WebRequest.Create
- ("ftp://185.27.134.8/htdocs/data/ID/_ID.txt");
- request.Credentials = new NetworkCredential("b11_13574135", "csaki123");
- request.Method = WebRequestMethods.Ftp.DownloadFile;
- response = (FtpWebResponse)request.GetResponse();
- Stream responseStream = response.GetResponseStream();
- reader = new StreamReader(responseStream);
- IDContent = reader.ReadToEnd();
- }
- public static bool CheckFileExists(string directory, string filenameWithExtension)
- {
- var request = (FtpWebRequest)WebRequest.Create(Path.Combine("ftp://185.27.134.8/htdocs/data/", directory, filenameWithExtension));
- request.Credentials = new NetworkCredential("b11_13574135", "csaki123");
- request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
- try
- {
- response = (FtpWebResponse)request.GetResponse();
- }
- catch (WebException ex)
- {
- response = (FtpWebResponse)ex.Response;
- if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
- {
- return false;
- }
- }
- return true;
- }
- public static void CheckIDFileExists()
- {
- if (!CheckFileExists("ID", "_ID.txt"))
- {
- CreateIDFile();
- }
- }
- private static void CreateIDFile()
- {
- var request = (FtpWebRequest)WebRequest.Create
- ("ftp://185.27.134.8/htdocs/data/ID/_ID.txt");
- request.Credentials = new NetworkCredential("b11_13574135", "csaki123");
- request.KeepAlive = false;
- request.Method = "STOR";
- request.UseBinary = true;
- request.ContentLength = 0;
- request.Proxy = null;
- using (Stream stream = request.GetRequestStream())
- {
- byte[] bytes = new UnicodeEncoding().GetBytes("");
- stream.Write(bytes, 0, bytes.Length);
- }
- }
- public static void CreateFile(string uri)
- {
- try
- {
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message + '\n' + ex.ToString().Substring(ex.ToString().LastIndexOf(':') + 1));
- }
- }
- public static string[] DownloadFile(string directory, string filenameWithExtension)
- {
- var request = (FtpWebRequest)WebRequest.Create
- (Path.Combine("ftp://185.27.134.8/htdocs/data/", directory, filenameWithExtension));
- request.Credentials = new NetworkCredential("b11_13574135", "csaki123");
- request.Method = WebRequestMethods.Ftp.DownloadFile;
- response = (FtpWebResponse)request.GetResponse();
- Stream responseStream = response.GetResponseStream();
- reader = new StreamReader(responseStream);
- string temp = reader.ReadToEnd();
- return temp.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
- }
- }
- }
- // Kerdes.cs:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading.Tasks;
- namespace KerdoivKitolto
- {
- public class Kerdes
- {
- public string kerdes;
- public string[] valaszok = new string[4];
- public bool tobblehetoseg;
- public int hanykerdes;
- public int hanyadik;
- public int valasz;
- public bool[] tobbvalasz = new bool[4];
- public RadioButton[] radios = new RadioButton[4];
- public CheckBox[] checks = new CheckBox[4];
- public Kerdes(string kerdes, string elso, string masodik, string harmadik, string negyedik, bool tobblehetoseg, int hanykerdes)
- {
- this.kerdes = kerdes;
- this.valaszok[0] = elso;
- this.valaszok[1] = masodik;
- this.valaszok[2] = harmadik;
- this.valaszok[3] = negyedik;
- this.tobblehetoseg = tobblehetoseg;
- this.hanykerdes = hanykerdes;
- }
- public Kerdes()
- {
- }
- }
- }
- // Kerdoiv.cs:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace KerdoivKitolto
- {
- public struct Kerdoiv
- {
- public List<Kerdes> kerdesLista;
- public string cim;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement