Client/MainForm.cs
author sl
Fri, 22 Aug 2014 22:48:30 +0200
changeset 43 86aad774b532
parent 33 1363bda20171
child 55 b5ed2e29be23
permissions -rw-r--r--
Client now supports text alignment through our new TextField.
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@43
    13
using SharpDisplayInterface;
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@25
    36
            //Instance context is then managed by our client class
sl@26
    37
            InstanceContext instanceContext = new InstanceContext(iCallback);
sl@26
    38
            iClient = new Client(instanceContext);
sl@18
    39
sl@29
    40
            //Connect using unique name
sl@32
    41
            //string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt");
sl@32
    42
            string name = "Client-" + (iClient.ClientCount() - 1);
sl@32
    43
            iClient.SetName(name);
sl@30
    44
            //Text = Text + ": " + name;
sl@32
    45
            Text = "[[" + name + "]]  " + iClient.SessionId;
sl@18
    46
sl@33
    47
            //
sl@33
    48
            textBoxTop.Text = iClient.Name;
sl@33
    49
            textBoxBottom.Text = iClient.SessionId;
sl@33
    50
sl@18
    51
        }
sl@21
    52
sl@31
    53
sl@31
    54
       
sl@31
    55
        public delegate void CloseConnectionDelegate();
sl@31
    56
        public delegate void CloseDelegate();
sl@31
    57
sl@31
    58
        /// <summary>
sl@31
    59
        /// 
sl@31
    60
        /// </summary>
sl@31
    61
        public void CloseConnectionThreadSafe()
sl@21
    62
        {
sl@31
    63
            if (this.InvokeRequired)
sl@29
    64
            {
sl@31
    65
                //Not in the proper thread, invoke ourselves
sl@31
    66
                CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
sl@31
    67
                this.Invoke(d, new object[] { });
sl@29
    68
            }
sl@31
    69
            else
sl@31
    70
            {
sl@31
    71
                //We are in the proper thread
sl@31
    72
                if (IsClientReady())
sl@31
    73
                {
sl@31
    74
                    Trace.TraceInformation("Closing client: " + iClient.SessionId);
sl@31
    75
                    iClient.Close();
sl@31
    76
                    Trace.TraceInformation("Closed client: " + iClient.SessionId);
sl@31
    77
                }
sl@29
    78
sl@31
    79
                iClient = null;
sl@31
    80
                iCallback = null;
sl@31
    81
            }
sl@26
    82
        }
sl@26
    83
sl@31
    84
        /// <summary>
sl@31
    85
        /// 
sl@31
    86
        /// </summary>
sl@31
    87
        public void CloseThreadSafe()
sl@31
    88
        {
sl@31
    89
            if (this.InvokeRequired)
sl@31
    90
            {
sl@31
    91
                //Not in the proper thread, invoke ourselves
sl@31
    92
                CloseDelegate d = new CloseDelegate(CloseThreadSafe);
sl@31
    93
                this.Invoke(d, new object[] { });
sl@31
    94
            }
sl@31
    95
            else
sl@31
    96
            {
sl@31
    97
                //We are in the proper thread
sl@31
    98
                Close();
sl@31
    99
            }
sl@31
   100
        }
sl@31
   101
sl@31
   102
sl@26
   103
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
sl@26
   104
        {
sl@31
   105
            CloseConnectionThreadSafe();
sl@29
   106
        }
sl@29
   107
sl@29
   108
        public bool IsClientReady()
sl@29
   109
        {
sl@29
   110
            return (iClient != null && iClient.State == CommunicationState.Opened);
sl@21
   111
        }
sl@43
   112
sl@43
   113
        private void buttonAlignLeft_Click(object sender, EventArgs e)
sl@43
   114
        {
sl@43
   115
            Alignment = ContentAlignment.MiddleLeft;
sl@43
   116
            textBoxTop.TextAlign = HorizontalAlignment.Left;
sl@43
   117
            textBoxBottom.TextAlign = HorizontalAlignment.Left;
sl@43
   118
        }
sl@43
   119
sl@43
   120
        private void buttonAlignCenter_Click(object sender, EventArgs e)
sl@43
   121
        {
sl@43
   122
            Alignment = ContentAlignment.MiddleCenter;
sl@43
   123
            textBoxTop.TextAlign = HorizontalAlignment.Center;
sl@43
   124
            textBoxBottom.TextAlign = HorizontalAlignment.Center;
sl@43
   125
        }
sl@43
   126
sl@43
   127
        private void buttonAlignRight_Click(object sender, EventArgs e)
sl@43
   128
        {
sl@43
   129
            Alignment = ContentAlignment.MiddleRight;
sl@43
   130
            textBoxTop.TextAlign = HorizontalAlignment.Right;
sl@43
   131
            textBoxBottom.TextAlign = HorizontalAlignment.Right;
sl@43
   132
        }
sl@43
   133
sl@43
   134
        private void buttonSetTopText_Click(object sender, EventArgs e)
sl@43
   135
        {
sl@43
   136
            //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
sl@43
   137
            iTextFieldTop.Text = textBoxTop.Text;
sl@43
   138
            iClient.SetText(iTextFieldTop);
sl@43
   139
        }
sl@43
   140
sl@43
   141
        private void buttonSetText_Click(object sender, EventArgs e)
sl@43
   142
        {
sl@43
   143
            //iClient.SetText(0,"Top");
sl@43
   144
            //iClient.SetText(1, "Bottom");
sl@43
   145
            //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
sl@43
   146
sl@43
   147
            iClient.SetTexts(new TextField[]
sl@43
   148
            { 
sl@43
   149
                new TextField(0, textBoxTop.Text, Alignment),
sl@43
   150
                new TextField(1, textBoxBottom.Text, Alignment)
sl@43
   151
            });
sl@43
   152
        }
sl@18
   153
    }
sl@18
   154
}