Используйте значение Keyascii, но перед тем, как установить его на 0:
Private Sub TextBoxEvents_KeyPress(ByVal Keyascii As msforms.ReturnInteger)
Userform2.CommentBoxUserform.Value = TextBoxEvents.Value & Chr(Keyascii)
Keyascii = 0 'return value ~~> 0 for no return value
DataBaseSheet.Cells(3, 3).Value = TextBoxEvents.Tag 'saves tag (name) of active comment box to fill up later with string of big textbox
userform2.Show
End Sub
Ваша структура XML неудачна, поскольку она использует элемент продукта для трех уровней иерархии. У Вас есть другие элементы подобными "домашнему"?
Принятие нас только хочет домашние, можно использовать:
Объекты количества в каждом из дешевых/дорогостоящих
xe.Element("Product") // Select the Product desc="household" element
.Elements() // Select the elements below it
.Select(element => new { Name=(string) element.Attribute("desc"),
Count=element.Elements().Count() });
Перечислите все категории
xe.Descendants() // Select all descendant elements
.Attributes() // All attributes from all elements
// Limit it to "category" elements
.Where(attr => attr.Name == "category")
// Select the value
.Select(attr => attr.Value)
// Remove duplicates
.Distinct();
Для сортировки этого просто используйте .OrderBy(x => x)
в конце.
Выберите 'дорогостоящие' продукты
xe.Descendants() // Select all elements
// Only consider those with a "Costly" description
.Where(element => (string) element.Attribute("desc") == "Costly")
// Select the subelements of that element, and flatten the result
.SelectMany(element => element.Elements());
Ну, лично я нахожу это легче с XmlDocument
:
XmlDocument root = new XmlDocument();
root.LoadXml(xml); // or .Load(path);
var categories = root.SelectNodes(
"/root/Product/Product/Product/@category")
.Cast<XmlNode>().Select(cat => cat.InnerText).Distinct();
var sortedCategories = categories.OrderBy(cat => cat);
foreach (var category in sortedCategories)
{
Console.WriteLine(category);
}
var totalItems = root.SelectNodes(
"/root/Products/Product/Product").Count;
Console.WriteLine(totalItems);
foreach (XmlElement prod in root.SelectNodes(
"/root/Product/Product[@desc='Costly']/Product"))
{
Console.WriteLine(prod.GetAttribute("desc"));
}