Esempio n. 1
0
        /// <summary>
        /// 将paralist里的数值填充入容器控件中的对应映射控件;
        /// </summary>
        /// <param name="paralist">IntPtr(paralist);</param>
        /// <param name="ownerPage">容器控件;</param>
        /// <param name="channelNum">参数所属通道号;</param>
        /// <returns>是否成功</returns>
        private bool SetParameterToProperty(IntPtr paralist, Control ownerPage, int channelNum)
        {
            //通过控件名与设备参数间的映射关系,将从设备获取的参数值填充至对应的配置界面控件;
            if (paralist == IntPtr.Zero)
            {
                return(false);
            }

            //遍历容器控件,通过控件名与设备参数间的映射关系,从参数列表中读取设备参数填充入对应控件;
            foreach (Control control in ownerPage.Controls)
            {
                //控件为非叶节点控件,则使用递归继续向下遍历;
                if (control.Controls.Count != 0)
                {
                    SetParameterToProperty(paralist, control, channelNum);
                }

                //使用control.AccessibleName来标识参与映射的控件;
                //可依据需求修改为其他标识;
                if (control.AccessibleName != null)
                {
                    bool isFind;

                    //查找控件映射的PARA_TYPES
                    DevControl.PARA_TYPES paraType = DevControl.PARA_TYPES.END_OF_PARA_TYPES;
                    isFind = this._propertyParaMap.FindParaByProperty(control.AccessibleName, ref paraType);
                    if (isFind == true)
                    {
                        byte[] valueBuf = new byte[100];
                        int    valueLen = valueBuf.Length;

                        //使用PARA_TYPES从参数列表中读取设备参数填充入对应控件;
                        DevControl.tagErrorCode eCode = DevControl.paralist_getnode(paralist, channelNum, paraType, ref valueLen, valueBuf);
                        if (eCode == DevControl.tagErrorCode.DM_ERR_OK)
                        {
                            //当前设备不支持此参数;
                            if (valueLen == 0)
                            {
                                control.Enabled = false;
                                continue;
                            }

                            //将获取到的参数统一转换为字符串再按控件类型分别填充;
                            StringBuilder bufferString = new StringBuilder(100);
                            int           stringLen    = bufferString.Capacity;
                            eCode = DevControl.DM_Value2String(paraType, valueBuf, valueLen, bufferString, ref stringLen);
                            Debug.Assert(eCode == DevControl.tagErrorCode.DM_ERR_OK);

                            Type controlType = control.GetType();
                            if (controlType == typeof(System.Windows.Forms.TextBox))
                            {
                                ((System.Windows.Forms.TextBox)control).Text = bufferString.ToString();
                            }
                            else if (controlType == typeof(System.Windows.Forms.CheckBox))
                            {
                                if (bufferString[0] == '1')
                                {
                                    ((System.Windows.Forms.CheckBox)control).Checked = true;
                                }
                                else if (bufferString[0] == '0')
                                {
                                    ((System.Windows.Forms.CheckBox)control).Checked = false;
                                }
                                else
                                {
                                    Debug.Fail("out of range!");
                                }
                            }
                            else if (controlType == typeof(System.Windows.Forms.ComboBox))
                            {
                                if (valueBuf[0] < ((System.Windows.Forms.ComboBox)control).Items.Count)
                                {
                                    ((System.Windows.Forms.ComboBox)control).SelectedIndex = int.Parse(bufferString.ToString());
                                }
                                else
                                {
                                    Debug.Fail("out of range!");
                                }
                            }
                            else if (controlType == typeof(System.Windows.Forms.NumericUpDown))
                            {
                                ((System.Windows.Forms.NumericUpDown)(control)).Value = decimal.Parse(bufferString.ToString());
                            }
                            else
                            {
                                Debug.Fail("must support new controlType!");
                                continue;
                            }
                        }
                    }
                }
            }

            return(true);
        }