private void buttonScanning_Click(object sender, EventArgs e) { progressInfo.Visible = true; buttonApplyChanges.Enabled = true; comport.GetRealDeviceList(progressInfo); //Образовался comport.realDeviceList (SortedList) RealDeviceTable.Items.Clear(); //Очистили первую таблицу UserTableView.Rows.Clear(); //Очистили вторую таблицу XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("settings.xml"); //Удаляем секцию/секции в конфиг файле, если она/они там есть XmlNodeList nodelist = xmlDoc.SelectNodes("allsettings/FromRealdeviceList"); if (nodelist.Count > 0) { foreach (XmlNode item in nodelist) { xmlDoc.DocumentElement.RemoveChild(item); } xmlDoc.Save("settings.xml"); } int index = 0; if (comport.realDeviceList.Count > 0) { XmlElement realDeviceListSection = xmlDoc.CreateElement("FromRealdeviceList"); xmlDoc.DocumentElement.AppendChild(realDeviceListSection); foreach (KeyValuePair <int, string> infoForDevice in comport.realDeviceList) { index = comport.realDeviceList.IndexOfKey(infoForDevice.Key); //Первая строка сразу создается со значением первого столбца - порядоквым индексом ListViewItem item = new ListViewItem((index + 1).ToString()); //Значение второго столбца - имя реального устройства в сети item.SubItems.Add(infoForDevice.Value); //Значение 3-го столбца - адрес реального устройства в сети item.SubItems.Add(infoForDevice.Key.ToString()); RealDeviceTable.Items.Add(item); //В список комбобокса колонки адресов добавляется очередной адрес из списка ColumnAdress.Items.Add(item.SubItems[2].Text); if (infoForDevice.Value != "часы") { //Собственно само заполнение UserTableView UserTableView.Rows.Add( (index + 1).ToString(), // 1 столбец - порядковый номер строки true, // 2 столбец - видимость в главном окне "", // 3 столбец - метка/псевдоним адреса/имя топлива item.SubItems[1].Text, // 4 столбец - тип устройства comport.GetPriceFromCurrentDevice((byte)infoForDevice.Key), // 5 столбец - значение цены ColumnAdress.Items[index].ToString()); // 6 столбец - значение адреса } XmlElement CurrentDeviceElement = null; if (infoForDevice.Value == "панель с ценой") { CurrentDeviceElement = xmlDoc.CreateElement("pricedevice"); } else { CurrentDeviceElement = xmlDoc.CreateElement("clockdevice"); } CurrentDeviceElement.InnerText = infoForDevice.Key.ToString(); realDeviceListSection.AppendChild(CurrentDeviceElement); //realDeviceListSection.SetAttribute("Адрес" + infoForDevice.Key.ToString(), infoForDevice.Value); } XmlTextWriter tr = new XmlTextWriter("settings.xml", null); tr.Formatting = Formatting.Indented; xmlDoc.WriteContentTo(tr); tr.Close(); } /* * int totalHeight = 0; // высота всех столбцов в таблице * for (int i = 0; i < UserTableView.Rows.Count; i++) // перебираем все строки и колонки * { * totalHeight += UserTableView.Rows[i].Height; // суммируем высоту каждой строки * } * UserTableView.Height = totalHeight + UserTableView.ColumnHeadersHeight; // меняем высоту dataGridView */ System.Threading.Thread.Sleep(300); Application.DoEvents(); System.Threading.Thread.Sleep(600); progressInfo.Visible = false; }
private void ReadPriceFromXMLFile(string filepath) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("settings.xml"); // Ищем устройство часы и спрашиваем время XmlNode clockNode = xmlDoc.SelectSingleNode("allsettings/FromRealdeviceList/clockdevice"); if (clockNode != null) { //Пакет на запрос времени у устройства с часами } #region Второй способ получения названия топлива и значения цены /* * XmlNode tablePriceNode = xmlDoc.SelectSingleNode("allsettings/UserTableViewSection"); * if ((tablePriceNode != null) && (tablePriceNode.HasChildNodes)) * { * int i = 0; * // Обход по каждой строке таблицы устройств с ценой * foreach (XmlNode device in tablePriceNode.ChildNodes) * { * Label NameOfFuel = new Label(); * TextBox PriceOfFuel = new TextBox(); * panelForPriceDisplay.Controls.Add(NameOfFuel); * panelForPriceDisplay.Controls.Add(PriceOfFuel); * * // Получаем список атрибутов у элемента * XmlAttributeCollection attr = device.Attributes; * * // Если устройство с первым адресом, спросим у него пароль пульта ДУ * if (attr.Item(5).Value == "1") * { * labelPasswordDU.Text = "Текущий пароль пульта ДУ: " + CurrentComPortObject.GetPasswordPultDU(); * } * * // Проверяем, есть ли название у топлива * if (attr.Item(2).Value == "") * { * NameOfFuel.Text = attr.Item(5).Value; * } * else * { * NameOfFuel.Text = attr.Item(2).Value; * } */ #endregion // Опрашиваем сохраненные устройства из секции Aliases, если они есть у нас XmlNode aliasNode = xmlDoc.SelectSingleNode("allsettings/Aliases"); if ((aliasNode != null) && (aliasNode.HasChildNodes)) { int i = 0; // Обход по каждой строке секции Alias foreach (XmlNode device in aliasNode.ChildNodes) { Label NameOfFuel = new Label(); TextBox PriceOfFuel = new TextBox(); panelForPriceDisplay.Controls.Add(NameOfFuel); panelForPriceDisplay.Controls.Add(PriceOfFuel); NameOfFuel.Text = device.Name; NameOfFuel.Enabled = true; NameOfFuel.Width = 130; NameOfFuel.Font = new Font(new FontFamily("Arial"), 8); PriceOfFuel.Text = CurrentComPortObject.GetPriceFromCurrentDevice(Convert.ToByte(device.InnerText)); PriceOfFuel.Enabled = true; PriceOfFuel.Width = 100; PriceOfFuel.MaxLength = 5; PriceOfFuel.Font = new Font(new FontFamily("Arial"), 10); PriceOfFuel.Tag = device.InnerText; NameOfFuel.Location = new Point(20, i * PriceOfFuel.Height + 40); PriceOfFuel.Location = new Point(150, i * PriceOfFuel.Height + 40); PriceOfFuel.Enter += PriceOfFuel_Enter; PriceOfFuel.KeyPress += new KeyPressEventHandler(EditPrice_KeyPress); PriceOfFuel.TextChanged += new EventHandler(EditPrice_TextChanged); PriceOfFuel.MouseEnter += PriceOfFuel_MouseEnter; i++; } } // Если устройств в таблице не оказалось (нет сохраненных данных об устройствах) else { ReadPriceFromRealDeviceList(); } }