<commit_before>﻿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.Globalization;

namespace FreeSCADA.Communication.MODBUSPlug
{
    public partial class ModifyChannelForm : Form
    {
        bool test = false;
        List<string> forbiddenNames;
        List<string> stations;
        string selectedStation;
        
        public ModifyChannelForm(ModbusChannelImp ch, List<string> forbiddenNames, List<string> stations, string selectedStation)
        {
            InitializeComponent();
            this.Tag = ch;
            this.forbiddenNames = forbiddenNames;
            this.stations = stations;
            this.selectedStation = selectedStation;
            this.FormClosing += new FormClosingEventHandler(ModifyChannelForm_FormClosing);

            nameTextBox.Text = ch.Name;
            foreach (ModbusDataTypeEx s in Enum.GetValues(typeof(ModbusDataTypeEx)))
            {
                modbusDataTypeComboBox.Items.Add(s);
            }
            modbusDataTypeComboBox.SelectedItem = ch.ModbusDataType;

            foreach (string s in stations)
            {
                stationComboBox.Items.Add(s);
            }
            stationComboBox.SelectedItem = ch.ModbusStation;

            slaveIdUpDown.Value = ch.SlaveId;
            modbusDataAddressNumericUpDown.Value = ch.ModbusDataAddress;
            deviceDataLenNumericUpDown.Value = ch.DeviceDataLen;
            bitIndexNumericUpDown.Value = ch.BitIndex;
            CultureInfo ci = CultureInfo.GetCultureInfo("en-US");
            kMaskedTextBox.Text = ch.K.ToString(ci.NumberFormat);
            dMaskedTextBox.Text = ch.D.ToString(ci.NumberFormat);

            
            MakeControlsValidation(ch);

            // must be AFTER makeControlValidation
            modbusDataTypeComboBox.SelectedIndexChanged += new EventHandler(modRegisterComboBox_SelectedIndexChanged);
            deviceDataTypeComboBox.SelectedIndexChanged += new EventHandler(modRegisterComboBox_SelectedIndexChanged);
            modbusFs2InternalTypeComboBox.SelectedIndexChanged += new EventHandler(modRegisterComboBox_SelectedIndexChanged);
        }

        void ModifyChannelForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if ((sender as ModifyChannelForm).test)
            {
                ModbusChannelImp ch = (ModbusChannelImp)this.Tag;

                if (forbiddenNames != null && forbiddenNames.Contains(ch.Name))
                {
                    e.Cancel = true;
                    MessageBox.Show(StringConstants.NameAssigned);
                }
                (sender as ModifyChannelForm).test = false;
            }
        }

        void MakeControlsValidation(ModbusChannelImp ch)
        {
            modbusDataTypeComboBox.SelectedIndexChanged -= new EventHandler(modRegisterComboBox_SelectedIndexChanged);
            deviceDataTypeComboBox.SelectedIndexChanged -= new EventHandler(modRegisterComboBox_SelectedIndexChanged);<commit_msg>MODBUS plugin tooltip for setting MODBUS address<commit_after>﻿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.Globalization;

namespace FreeSCADA.Communication.MODBUSPlug
{
    public partial class ModifyChannelForm : Form
    {
        bool test = false;
        List<string> forbiddenNames;
        List<string> stations;
        string selectedStation;

        public ModifyChannelForm(ModbusChannelImp ch, List<string> forbiddenNames, List<string> stations, string selectedStation)
        {
            InitializeComponent();
            InitializeTooltips();
            this.Tag = ch;
            this.forbiddenNames = forbiddenNames;
            this.stations = stations;
            this.selectedStation = selectedStation;
            this.FormClosing += new FormClosingEventHandler(ModifyChannelForm_FormClosing);

            nameTextBox.Text = ch.Name;
            foreach (ModbusDataTypeEx s in Enum.GetValues(typeof(ModbusDataTypeEx)))
            {
                modbusDataTypeComboBox.Items.Add(s);
            }
            modbusDataTypeComboBox.SelectedItem = ch.ModbusDataType;

            foreach (string s in stations)
            {
                stationComboBox.Items.Add(s);
            }
            stationComboBox.SelectedItem = ch.ModbusStation;

            slaveIdUpDown.Value = ch.SlaveId;
            modbusDataAddressNumericUpDown.Value = ch.ModbusDataAddress;
            deviceDataLenNumericUpDown.Value = ch.DeviceDataLen;
            bitIndexNumericUpDown.Value = ch.BitIndex;
            CultureInfo ci = CultureInfo.GetCultureInfo("en-US");
            kMaskedTextBox.Text = ch.K.ToString(ci.NumberFormat);
            dMaskedTextBox.Text = ch.D.ToString(ci.NumberFormat);


            MakeControlsValidation(ch);

            // must be AFTER makeControlValidation
            modbusDataTypeComboBox.SelectedIndexChanged += new EventHandler(modRegisterComboBox_SelectedIndexChanged);
            deviceDataTypeComboBox.SelectedIndexChanged += new EventHandler(modRegisterComboBox_SelectedIndexChanged);
            modbusFs2InternalTypeComboBox.SelectedIndexChanged += new EventHandler(modRegisterComboBox_SelectedIndexChanged);
        }

        void InitializeTooltips()
        {
            ToolTip expressionTooltip = new ToolTip();
            expressionTooltip.AutomaticDelay = 180000;
            expressionTooltip.InitialDelay = 100;
            expressionTooltip.ShowAlways = true;

            string tip = "Offset from the start register of the respective data space.\n\n" +
                "The IO ranges in MODICON PLC and other devices are:\n" +
                "00001 - 09999 Coils / Discrete Outputs\n" +
                "10001 - 19999 Inputs (Discrete, Read only)\n" +
                "30001 - 39999 Input registers/analog/counters (Read only)\n" +
                "40001 - 49999 Holding registers / analog output";
            expressionTooltip.SetToolTip(modbusDataAddressNumericUpDown, tip);
        }

        void ModifyChannelForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if ((sender as ModifyChannelForm).test)
            {
                ModbusChannelImp ch = (ModbusChannelImp)this.Tag;

                if (forbiddenNames != null && forbiddenNames.Contains(ch.Name))
                {
                    e.Cancel = true;
                    MessageBox.Show(StringConstants.NameAssigned);
                }
                (sender as ModifyChannelForm).test = false;
            }
        }

        void MakeControlsValidation(ModbusChannelImp ch)
        {
            modbusDataTypeComboBox.SelectedIndexChanged -= new EventHandler(modRegisterComboBox_SelectedIndexChanged);
            deviceDataTypeComboBox.SelectedIndexChanged -= new EventHandler(modRegisterComboBox_SelectedIndexChanged);
{'fraction_non_alphanumeric': 0.07247810115851935, 'fraction_numerical': 0.007205425261373269, 'mean_word_length': 2.380611270296084, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2970047', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1747, 'n_words': 333}