The Easiest Way to Save and Share Code Snippets on the web

Quizy

csharp

posted: May, 24th 2010 | jump to bottom

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Diagnostics;
 
namespace Quizzy
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            WczytajQuizy();
        }
 
        private void OdswiezKreator()
        {
            webBrowser2.Url = new Uri("http://quizz.pl/addQuizzForm.php");
        }
 
        private void WczytajQuizy()
        {
            ZaladujListeQuizow();
        }
 
        int WybraneID = -1;
 
        private void lbListaQuizow_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            WybraneID = (int) lbListaQuizow.SelectedValue;
            tabControl1.SelectedIndex = 2;
        }
 
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 2)
            {
                webBrowser1.Url = new Uri("http://www.quizz.pl/o.php?i=" + WybraneID );
            }
 
            if (tabControl1.SelectedIndex == 1)
            {
                if (lbNajlepsze.DataSource == null)
                {
                    ZaladujNajlepszeQuizy();
                }
            }
 
            if (tabControl1.SelectedIndex == 3)
            {
                if (webBrowser2.Url == null)
                {
                    OdswiezKreator();
                }
            }
 
            if (tabControl1.SelectedIndex == 0)
            {
                lbListaQuizow.Focus();
            }
 
            if (tabControl1.SelectedIndex == 1)
            {
                lbNajlepsze.Focus();
            }
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            ZaladujListeQuizow();
        }
 
        DataTable dt = null;
        DataColumn dcNazwa = null;
        DataColumn dcId = null;
 
        private void ZaladujListeQuizow()
        {
            dt = new DataTable();
            dcNazwa = dt.Columns.Add("Nazwa", typeof(string));
            dcId = dt.Columns.Add("ID", typeof(int));
 
            DataRow dr;
 
            StreamReader reader = new StreamReader(WebRequest.Create("http://www.quizz.pl/api.php?method=listbyDatev1").GetResponse().GetResponseStream());
            string Quizy = reader.ReadToEnd();
            reader.Close();
 
            string[] Quiz = Quizy.Split(';');
 
            for (int i = 0; i < Quiz.Length; i++)
            {
                string[] quiz = Quiz[i].Split('/');
                if (quiz.Length == 3)
                {
                    dr = dt.NewRow();
                    dr[dcId] = quiz[0];
                    dr[dcNazwa] = quiz[1];
                    dt.Rows.Add(dr);
                }
            }
 
            lbListaQuizow.DataSource = dt;
            lbListaQuizow.DisplayMember = dcNazwa.ColumnName;
            lbListaQuizow.ValueMember = dcId.ColumnName;
 
            if (WybraneID < 0)
            {
                WybraneID = (int) dt.Rows[0][dcId];
            }
        }
 
        private void ZaladujNajlepszeQuizy()
        {
            DataTable dt = null;
            DataColumn dcNazwa = null;
            DataColumn dcId = null;
 
            dt = new DataTable();
            dcNazwa = dt.Columns.Add("Nazwa", typeof(string));
            dcId = dt.Columns.Add("ID", typeof(int));
 
            DataRow dr;
 
            StreamReader reader = new StreamReader(WebRequest.Create("http://www.quizz.pl/api.php?method=listallv1").GetResponse().GetResponseStream());
            string Quizy = reader.ReadToEnd();
            reader.Close();
 
            string[] Quiz = Quizy.Split(';');
 
            for (int i = 0; i < Quiz.Length; i++)
            {
                string[] quiz = Quiz[i].Split(',');
                if (quiz.Length == 2)
                {
                    dr = dt.NewRow();
                    dr[dcId] = quiz[0];
                    dr[dcNazwa] = quiz[1];
                    dt.Rows.Add(dr);
                }
            }
 
            this.lbNajlepsze.DataSource = dt;
            lbNajlepsze.DisplayMember = dcNazwa.ColumnName;
            lbNajlepsze.ValueMember = dcId.ColumnName;
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
 
 
        private string NazwaQuizu(int ID)
        {
            if (ID > 0)
            {
                return (string)(from DataRow quiz in dt.Rows
                                where (int)quiz[dcId] == ID
                                select quiz[dcNazwa]).First();
            }
            else
            {
                return "";
            }
        }
 
        private void button3_Click_1(object sender, EventArgs e)
        {
            Process p = new Process();
            p.StartInfo.FileName =
                String.Format(
                    "mailto: ?subject=Wypełnij quiz {0}&body=Cześć%0A%0APolecam Tobie quiz {1}, który udało mi się znaleźć dzisiaj%0A%0Az pozdrowieniami,",
                    NazwaQuizu(WybraneID),
                    NazwaQuizu(WybraneID)
                );
 
            p.Start();
        }
 
        private void Form1_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == this.WindowState)
            {
                this.notifyIcon1.Visible = true;
                notifyIcon1.ShowBalloonTip(500);
                this.Hide();
            }
            else if (FormWindowState.Normal == this.WindowState)
            {
                notifyIcon1.Visible = false;
            }
        }
 
        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.Show();
        }
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.notifyIcon1.Visible = true;
            notifyIcon1.ShowBalloonTip(500);
            this.Hide();
        }
 
        private void lbNajlepsze_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            WybraneID = (int)lbNajlepsze.SelectedValue;
            tabControl1.SelectedIndex = 2;
        }
 
        private void button4_Click(object sender, EventArgs e)
        {
            int numer = -1;
            if (int.TryParse(this.textBox1.Text, out numer))
            {
                StreamReader reader = new StreamReader(WebRequest.Create("http://quizz.pl/api.php?quizzId=" + WybraneID + "&cellNumber=" + numer.ToString() + "&method=sendsms").GetResponse().GetResponseStream());
                reader.ReadToEnd();
                reader.Close();
                MessageBox.Show("Quiz został wysłany pomyślnie!");
            }
        }
 
        int OstatnieID = 0;
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                ZaladujListeQuizow();
                int ObecneId = (int)dt.Rows[0][dcId];
 
                if (ObecneId != OstatnieID)
                {
                    notifyIcon1.ShowBalloonTip(500, "Nowy quiz dla Ciebie", "Wypełnij nowy quiz: " + dt.Rows[0][dcNazwa].ToString(), ToolTipIcon.Info);
                }
 
                OstatnieID = ObecneId;
            }
        }
 
        private void button5_Click(object sender, EventArgs e)
        {
            Process p = new Process();
            p.StartInfo.FileName = "http://www.facebook.com/sharer.php?u=http://quizz.pl/q.php?i=" + WybraneID + "&t=Polecam quiz " + NazwaQuizu(WybraneID);
            p.Start();
        }
 
        private void button6_Click(object sender, EventArgs e)
        {
            OdswiezKreator();
        }
 
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            odpalStrone();
        }
 
        private void odpalStrone()
        {
            Process p = new Process();
            p.StartInfo.FileName = "http://www.quizz.pl";
            p.Start();
        }
 
        private void label3_Click(object sender, EventArgs e)
        {
            odpalStrone();
        }
    }
}
 
210 views