Client/MainForm.cs
author sl
Mon, 22 Sep 2014 17:23:35 +0200
changeset 63 cd9924457275
parent 62 ac698f4e1b36
child 65 464486b81635
permissions -rw-r--r--
TableLayout now support row and column styles.
sl@18
     1
using System;
sl@18
     2
using System.Collections.Generic;
sl@18
     3
using System.ComponentModel;
sl@18
     4
using System.Data;
sl@18
     5
using System.Drawing;
sl@18
     6
using System.Linq;
sl@18
     7
using System.Text;
sl@18
     8
using System.Threading.Tasks;
sl@18
     9
using System.Windows.Forms;
sl@18
    10
using System.ServiceModel;
sl@18
    11
using System.ServiceModel.Channels;
sl@31
    12
using System.Diagnostics;
sl@55
    13
using SharpDisplay;
sl@20
    14
sl@18
    15
sl@18
    16
namespace SharpDisplayClient
sl@18
    17
{
sl@18
    18
    public partial class MainForm : Form
sl@18
    19
    {
sl@26
    20
        Client iClient;
sl@26
    21
        Callback iCallback;
sl@43
    22
        ContentAlignment Alignment;
sl@43
    23
        TextField iTextFieldTop;
sl@18
    24
sl@18
    25
        public MainForm()
sl@18
    26
        {
sl@18
    27
            InitializeComponent();
sl@43
    28
            Alignment = ContentAlignment.MiddleLeft;
sl@43
    29
            iTextFieldTop = new TextField(0);
sl@18
    30
        }
sl@18
    31
sl@18
    32
sl@18
    33
        private void MainForm_Load(object sender, EventArgs e)
sl@18
    34
        {
sl@31
    35
            iCallback = new Callback(this);
sl@57
    36
            iClient = new Client(iCallback);
sl@18
    37
sl@29
    38
            //Connect using unique name
sl@32
    39
            //string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt");
sl@32
    40
            string name = "Client-" + (iClient.ClientCount() - 1);
sl@32
    41
            iClient.SetName(name);
sl@30
    42
            //Text = Text + ": " + name;
sl@32
    43
            Text = "[[" + name + "]]  " + iClient.SessionId;
sl@18
    44
sl@33
    45
            //
sl@33
    46
            textBoxTop.Text = iClient.Name;
sl@33
    47
            textBoxBottom.Text = iClient.SessionId;
sl@33
    48
sl@18
    49
        }
sl@21
    50
sl@31
    51
sl@60
    52
sl@31
    53
        public delegate void CloseConnectionDelegate();
sl@31
    54
        public delegate void CloseDelegate();
sl@31
    55
sl@31
    56
        /// <summary>
sl@60
    57
        ///
sl@31
    58
        /// </summary>
sl@31
    59
        public void CloseConnectionThreadSafe()
sl@21
    60
        {
sl@31
    61
            if (this.InvokeRequired)
sl@29
    62
            {
sl@31
    63
                //Not in the proper thread, invoke ourselves
sl@31
    64
                CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
sl@31
    65
                this.Invoke(d, new object[] { });
sl@29
    66
            }
sl@31
    67
            else
sl@31
    68
            {
sl@31
    69
                //We are in the proper thread
sl@31
    70
                if (IsClientReady())
sl@31
    71
                {
sl@31
    72
                    Trace.TraceInformation("Closing client: " + iClient.SessionId);
sl@31
    73
                    iClient.Close();
sl@31
    74
                    Trace.TraceInformation("Closed client: " + iClient.SessionId);
sl@31
    75
                }
sl@29
    76
sl@31
    77
                iClient = null;
sl@31
    78
                iCallback = null;
sl@31
    79
            }
sl@26
    80
        }
sl@26
    81
sl@31
    82
        /// <summary>
sl@60
    83
        ///
sl@31
    84
        /// </summary>
sl@31
    85
        public void CloseThreadSafe()
sl@31
    86
        {
sl@31
    87
            if (this.InvokeRequired)
sl@31
    88
            {
sl@31
    89
                //Not in the proper thread, invoke ourselves
sl@31
    90
                CloseDelegate d = new CloseDelegate(CloseThreadSafe);
sl@31
    91
                this.Invoke(d, new object[] { });
sl@31
    92
            }
sl@31
    93
            else
sl@31
    94
            {
sl@31
    95
                //We are in the proper thread
sl@31
    96
                Close();
sl@31
    97
            }
sl@31
    98
        }
sl@31
    99
sl@31
   100
sl@26
   101
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
sl@26
   102
        {
sl@31
   103
            CloseConnectionThreadSafe();
sl@29
   104
        }
sl@29
   105
sl@29
   106
        public bool IsClientReady()
sl@29
   107
        {
sl@29
   108
            return (iClient != null && iClient.State == CommunicationState.Opened);
sl@21
   109
        }
sl@43
   110
sl@43
   111
        private void buttonAlignLeft_Click(object sender, EventArgs e)
sl@43
   112
        {
sl@43
   113
            Alignment = ContentAlignment.MiddleLeft;
sl@43
   114
            textBoxTop.TextAlign = HorizontalAlignment.Left;
sl@43
   115
            textBoxBottom.TextAlign = HorizontalAlignment.Left;
sl@43
   116
        }
sl@43
   117
sl@43
   118
        private void buttonAlignCenter_Click(object sender, EventArgs e)
sl@43
   119
        {
sl@43
   120
            Alignment = ContentAlignment.MiddleCenter;
sl@43
   121
            textBoxTop.TextAlign = HorizontalAlignment.Center;
sl@43
   122
            textBoxBottom.TextAlign = HorizontalAlignment.Center;
sl@43
   123
        }
sl@43
   124
sl@43
   125
        private void buttonAlignRight_Click(object sender, EventArgs e)
sl@43
   126
        {
sl@43
   127
            Alignment = ContentAlignment.MiddleRight;
sl@43
   128
            textBoxTop.TextAlign = HorizontalAlignment.Right;
sl@43
   129
            textBoxBottom.TextAlign = HorizontalAlignment.Right;
sl@43
   130
        }
sl@43
   131
sl@43
   132
        private void buttonSetTopText_Click(object sender, EventArgs e)
sl@43
   133
        {
sl@43
   134
            //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
sl@43
   135
            iTextFieldTop.Text = textBoxTop.Text;
sl@60
   136
            iTextFieldTop.Alignment = Alignment;
sl@43
   137
            iClient.SetText(iTextFieldTop);
sl@43
   138
        }
sl@43
   139
sl@43
   140
        private void buttonSetText_Click(object sender, EventArgs e)
sl@43
   141
        {
sl@43
   142
            //iClient.SetText(0,"Top");
sl@43
   143
            //iClient.SetText(1, "Bottom");
sl@43
   144
            //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
sl@43
   145
sl@43
   146
            iClient.SetTexts(new TextField[]
sl@60
   147
            {
sl@43
   148
                new TextField(0, textBoxTop.Text, Alignment),
sl@43
   149
                new TextField(1, textBoxBottom.Text, Alignment)
sl@43
   150
            });
sl@43
   151
        }
sl@62
   152
sl@62
   153
        private void buttonLayoutUpdate_Click(object sender, EventArgs e)
sl@62
   154
        {
sl@62
   155
            TableLayout layout = new TableLayout(2,2);
sl@63
   156
            layout.Columns[1].Width = 25F;
sl@62
   157
            iClient.SetLayout(layout);
sl@62
   158
        }
sl@18
   159
    }
sl@18
   160
}