Exemple #1
0
        private void Initialize()
        {
            RedisDataAccess.URL  = "localhost";
            RedisDataAccess.Port = 6379;
            _portVal             = txtPortRedisServer.Text;

            txtUrlRedisServer.TextChanged += (object sender, EventArgs e) =>
            {
                RedisDataAccess.URL = (sender as TextBox).Text;
            };

            txtPortRedisServer.TextChanged += (object sender, EventArgs e) =>
            {
                if (String.IsNullOrEmpty((sender as TextBox).Text))
                {
                    (sender as TextBox).Text = _portVal;
                }

                if (Int32.TryParse((sender as TextBox).Text, out int port))
                {
                    RedisDataAccess.Port = port;
                    _portVal             = port.ToString();
                }
                else
                {
                    string strVal = "";
                    foreach (char s in txtPortRedisServer.Text)
                    {
                        if (int.TryParse(s.ToString(), out int digit))
                        {
                            strVal += s;
                        }
                    }
                    txtPortRedisServer.Text = strVal;
                    _portVal = strVal;
                }
            };

            lblInfo.Padding   = new Padding(5);
            lblInfo.BackColor = Color.FromArgb(245, 229, 228);
            lblInfo.drawBorder(3, Color.DarkBlue);
            lblInfo.Text = string.Empty;

            Label lblpnlLnkKeysBackground = new Label();

            lblpnlLnkKeysBackground.AutoSize = false;
            lblpnlLnkKeysBackground.Location = new Point(lblInfo.Location.X, lblInfo.Location.Y + lblInfo.Height + 10);
            lblpnlLnkKeysBackground.Size     = new Size(357, 355);
            lblpnlLnkKeysBackground.drawBorder(3, Color.DarkBlue);
            this.Controls.Add(lblpnlLnkKeysBackground);
            this.Controls.SetChildIndex(lblpnlLnkKeysBackground, -1);

            pnlLnkKeys           = new ScrollablePanel();
            pnlLnkKeys.BackColor = Color.Bisque;
            pnlLnkKeys.Padding   = new Padding(5);
            pnlLnkKeys.Location  = new Point(lblpnlLnkKeysBackground.Location.X + 3, lblpnlLnkKeysBackground.Location.Y + 3);
            pnlLnkKeys.Size      = new Size(lblpnlLnkKeysBackground.Size.Width - 6, lblpnlLnkKeysBackground.Size.Height - 6);
            pnlLnkKeys.Text      = string.Empty;

            //here you can add additional functionality to mouse events of ScrollablePanel
            pnlLnkKeys.ScrollMouseWheel += (object sender, MouseEventArgs me) => { };
            pnlLnkKeys.ScrollHorizontal += (object sender, ScrollEventArgs se) => { };
            pnlLnkKeys.ScrollVertical   += (object sender, ScrollEventArgs se) => { };
            this.Controls.Add(pnlLnkKeys);
            this.Controls.SetChildIndex(pnlLnkKeys, 1);

            lblTreatKeysPnlBack.drawBorder(3, Color.DarkBlue);
            lblTreatKeysPnlBack.Text = string.Empty;
            this.Controls.SetChildIndex(lblTreatKeysPnlBack, -1);

            pnlTreatRedisKeys                   = new ScrollablePanel();
            pnlTreatRedisKeys.Location          = new Point(lblTreatKeysPnlBack.Location.X + 3, lblTreatKeysPnlBack.Location.Y + 3);
            pnlTreatRedisKeys.Size              = new Size(lblTreatKeysPnlBack.Width - 6, lblTreatKeysPnlBack.Height - 6);
            pnlTreatRedisKeys.BackColor         = Color.Bisque;
            pnlTreatRedisKeys.ScrollMouseWheel += (object sender, MouseEventArgs me) => { };
            pnlTreatRedisKeys.ScrollHorizontal += (object sender, ScrollEventArgs se) => { };
            pnlTreatRedisKeys.ScrollVertical   += (object sender, ScrollEventArgs se) => { };
            this.Controls.Add(pnlTreatRedisKeys);
            this.Controls.SetChildIndex(pnlTreatRedisKeys, 1);

            Label lblCurrKeyConstntMessage = new Label();

            lblCurrKeyConstntMessage.Location = new Point(0);
            lblCurrKeyConstntMessage.AutoSize = true;
            lblCurrKeyConstntMessage.Padding  = new Padding(3);
            lblCurrKeyConstntMessage.Font     = new Font(new FontFamily("Arial"), 15, FontStyle.Bold, GraphicsUnit.Pixel);
            lblCurrKeyConstntMessage.Text     = "redis key:";
            pnlTreatRedisKeys.Controls.Add(lblCurrKeyConstntMessage);

            Button btnViewValue = new Button();

            btnViewValue.Location = new Point(lblCurrKeyConstntMessage.Location.X, lblCurrKeyConstntMessage.Location.Y + lblCurrKeyConstntMessage.Height);
            btnViewValue.Click   += async(object sender, EventArgs e) =>
            {
                _nonBlockinglyPastingTextToControlThread?.Abort();
                _lblDisplayValue.Text = "";

                byte[] currenKeyValue = (byte[])_currentRedisKey.GetType().GetProperty("KeyValue", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(_currentRedisKey);
                if (currenKeyValue == null)
                {
                    return;
                }

                await TryToConnectAndRestoreRedisObjects();

                RedisValue value = await RedisDataAccess.RedisDb.StringGetAsync(_currentRedisKey);

                try
                {
                    object serialized = JsonConvert.DeserializeObject(value.ToString());
                }
                catch (JsonReaderException)
                {
                    _lblDisplayValue.Text = value.ToString();
                    return;
                }

                DialogResult rez = DialogResult.Cancel;
                if (value.ToString().Length > 10000)
                {
                    string title   = "value is too long!";
                    string message = $"Yor value is a JSON object, but it is fairly lengthy ({System.Text.ASCIIEncoding.Unicode.GetByteCount(value.ToString()).FormatSize()} bytes exactly), so prettifying if may take a long time. Maybe you prefer to view it raw instead? \n\nPress OK to raw view\nor Cancel ro prettyfied view";

                    rez = MessageBox.Show(message, title, MessageBoxButtons.OKCancel);
                }

                if (rez == DialogResult.OK)
                {
                    PassTextToControlNonBlockingly(value.ToString(), _lblDisplayValue);
                    return;
                }


                var conv = new JsonToDictionaryConverter();
                conv.RawStringToConsume = value.ToString();
                Dictionary <string, object> valueDict = await conv.ProvideAPIDataFromJSON();

                Task <string> tsk = conv.ProvidePrettyString("    ", valueDict);
                DisplayAwaiting(tsk, lblInfo);
                PassTextToControlNonBlockingly(await tsk, _lblDisplayValue);
            };
            btnViewValue.Text = "View value";

            pnlTreatRedisKeys.Controls.Add(btnViewValue);

            Button btnDeleteKey = new Button();

            btnDeleteKey.Location = new Point(btnViewValue.Location.X + btnViewValue.Width + 5, btnViewValue.Location.Y);
            btnDeleteKey.Click   += async(object sender, EventArgs e) =>
            {
                await TryToConnectAndRestoreRedisObjects();

                var val = await RedisDataAccess.RedisDb.StringGetAsync(_currentRedisKey);

                await RedisDataAccess.RedisDb.KeyDeleteAsync(_currentRedisKey);

                val = await RedisDataAccess.RedisDb.StringGetAsync(_currentRedisKey);

                if (val.IsNull)
                {
                    _lblCurrentKey.Text   = "No key";
                    _lblDisplayValue.Text = "Nokey, so no data";
                }

                await btnGetKeys_Click();
            };
            btnDeleteKey.Text = "Remove";
            pnlTreatRedisKeys.Controls.Add(btnDeleteKey);

            _lblCurrentKey          = new Label();
            _lblCurrentKey.Location = new Point(lblCurrKeyConstntMessage.Location.X + lblCurrKeyConstntMessage.Width, 0);
            _lblCurrentKey.AutoSize = true;
            _lblCurrentKey.Padding  = new Padding(3);
            _lblCurrentKey.Font     = new Font(new FontFamily("Arial"), 15, new FontStyle(), new GraphicsUnit());
            _lblCurrentKey.Text     = "";
            pnlTreatRedisKeys.Controls.Add(_lblCurrentKey);

            lblOutputScrlPanBack.drawBorder(3, Color.DarkBlue);
            lblOutputScrlPanBack.Text = string.Empty;
            this.Controls.SetChildIndex(lblOutputScrlPanBack, -1);

            ScrollablePanel pnlOutput = new ScrollablePanel();

            pnlOutput.Location          = new Point(lblOutputScrlPanBack.Location.X + 3, lblOutputScrlPanBack.Location.Y + 3);
            pnlOutput.Size              = new Size(lblOutputScrlPanBack.Width - 6, lblOutputScrlPanBack.Height - 6);
            pnlOutput.BackColor         = Color.LightCyan;
            pnlOutput.ScrollMouseWheel += (object sender, MouseEventArgs me) => { };
            pnlOutput.ScrollHorizontal += (object sender, ScrollEventArgs se) => { };
            pnlOutput.ScrollVertical   += (object sender, ScrollEventArgs se) => { };
            this.Controls.Add(pnlOutput);
            this.Controls.SetChildIndex(pnlOutput, 1);

            _lblDisplayValue          = new Label();
            _lblDisplayValue.Location = new Point(0);
            _lblDisplayValue.Padding  = new Padding(5);
            _lblDisplayValue.AutoSize = true;
            pnlOutput.Controls.Add(_lblDisplayValue);



            btnStartRedis.Click += StartRedis;
            btnKillRedis.Click  += (object sender, EventArgs e) =>
            {
                string pathToRedisServer = providePathToRedisServer("Please provide path to Redis server in order to be killed");
                if (pathToRedisServer != null)
                {
                    RedisDataAccess.KillRedis(pathToRedisServer);
                }
                else
                {
                    MessageBox.Show("There is no path to Redis server");
                }
            };

            btnisConnected.Click += async(object sender, EventArgs e) => {
                Task <bool> isConnectedTask = RedisDataAccess.isRedisConnected();
                lblInfo.Text = "Please wait ";
                System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
                timer.Interval = 100;
                timer.Tick    += (object sender, EventArgs e) => {
                    lblInfo.Text += ".";
                };
                timer.Start();
                bool isConnected = await isConnectedTask;
                timer.Stop();
                if (isConnected)
                {
                    lblInfo.Text = "Redis is running.";
                }
                else
                {
                    lblInfo.Text = "Redis is not running";
                }
            };

            btnGetKeys.Click += async(object sender, EventArgs e) =>
            {
                Task tsk = btnGetKeys_Click();
                DisplayAwaiting(tsk, lblInfo);
                await tsk;
            };

            btnNewKey.Click += (object sender, EventArgs e) =>
            {
                Form frmAddNewKey = new Form();
                frmAddNewKey.Size                     = new Size(this.Width - 100, this.Height - 100);
                frmAddNewKey.AutoSize                 = false;
                frmAddNewKey.AutoScroll               = false;
                frmAddNewKey.VerticalScroll.Visible   = true;
                frmAddNewKey.HorizontalScroll.Visible = false;

                Label lblAddNewkeyValue = new Label();
                lblAddNewkeyValue.Text     = "Add new key and value:";
                lblAddNewkeyValue.Location = new Point(0);
                lblAddNewkeyValue.Padding  = new Padding(5);
                lblAddNewkeyValue.Font     = new Font("Arial", 15, FontStyle.Bold, GraphicsUnit.Pixel);
                lblAddNewkeyValue.AutoSize = true;
                frmAddNewKey.Controls.Add(lblAddNewkeyValue);

                TextBox txtNewKeyName = new TextBox();
                txtNewKeyName.Location = new Point(lblAddNewkeyValue.Location.X + 10, lblAddNewkeyValue.Location.Y + lblAddNewkeyValue.Height);
                txtNewKeyName.Width    = frmAddNewKey.ClientRectangle.Width - 100;
                txtNewKeyName.Text     = "[key name]";
                frmAddNewKey.Controls.Add(txtNewKeyName);

                RichTextBox rtbNewKeyValue = new RichTextBox();
                rtbNewKeyValue.Location = new Point(txtNewKeyName.Location.X, txtNewKeyName.Location.Y + txtNewKeyName.Height + 10);
                rtbNewKeyValue.Size     = new Size(txtNewKeyName.Width, 300);
                rtbNewKeyValue.Text     = "[key value]";
                frmAddNewKey.Controls.Add(rtbNewKeyValue);

                Button btnAddNewkeyValue = new Button();
                btnAddNewkeyValue.Location = new Point(rtbNewKeyValue.Location.X, rtbNewKeyValue.Location.Y + rtbNewKeyValue.Height + 10);
                btnAddNewkeyValue.Text     = "Add";
                btnAddNewkeyValue.Click   += async(object sender, EventArgs e) =>
                {
                    string keyName  = txtNewKeyName.Text;
                    string keyValue = rtbNewKeyValue.Text;

                    await TryToConnectAndRestoreRedisObjects();

                    await RedisDataAccess.RedisDb.StringSetAsync(keyName, keyValue);


                    await btnGetKeys_Click();

                    frmAddNewKey.Close();
                };
                frmAddNewKey.Controls.Add(btnAddNewkeyValue);
                frmAddNewKey.Show();
            };


            RedisDataAccess.CallBackEvent += (GlobalEventArgs messageCarrier) =>
            {
                if (messageCarrier.clearDisplay)
                {
                    lblInfo.Text = messageCarrier.Message;
                }
                else
                {
                    lblInfo.Text += messageCarrier.Message;
                }
            };
        }