From e5dc3fd2bc890d0a595d503e60cfc5e14a4f2f61 Mon Sep 17 00:00:00 2001 From: FlorianDahn Date: Wed, 15 Apr 2020 20:23:39 +0200 Subject: [PATCH] Update ButtonForm.cs - adding a Duplicate function - improving Count property - adding InsertButtonRow methods - adding a dictionary property to get all buttons in a specific row --- TelegramBotBase/Form/ButtonForm.cs | 49 +++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/TelegramBotBase/Form/ButtonForm.cs b/TelegramBotBase/Form/ButtonForm.cs index 8346c8c..eca6213 100644 --- a/TelegramBotBase/Form/ButtonForm.cs +++ b/TelegramBotBase/Form/ButtonForm.cs @@ -42,6 +42,15 @@ namespace TelegramBotBase.Form } } + + public List this[int row] + { + get + { + return Buttons[row]; + } + } + public ButtonForm() { @@ -62,6 +71,16 @@ namespace TelegramBotBase.Form AddButtonRow(row.ToList()); } + public void InsertButtonRow(int index, IEnumerable row) + { + Buttons.Insert(index, row.ToList()); + } + + public void InsertButtonRow(int index, params ButtonBase[] row) + { + InsertButtonRow(index, row.ToList()); + } + public static T[][] SplitTo(IEnumerable items, int itemsPerRow = 2) { T[][] splitted = default(T[][]); @@ -86,7 +105,10 @@ namespace TelegramBotBase.Form { get { - return this.Buttons.Select(a => a.ToArray()).Aggregate((a, b) => a.Union(b).ToArray()).Length; + if (this.Buttons.Count == 0) + return 0; + + return this.Buttons.Select(a => a.ToArray()).ToList().Aggregate((a, b) => a.Union(b).ToArray()).Length; } } @@ -153,5 +175,30 @@ namespace TelegramBotBase.Form return ikm; } + + /// + /// Creates a copy of this form. + /// + /// + public ButtonForm Duplicate() + { + var bf = new ButtonForm() + { + Markup = this.Markup, + DependencyControl = this.DependencyControl + }; + + foreach(var b in Buttons) + { + var lst = new List(); + foreach(var b2 in b) + { + lst.Add(b2); + } + bf.Buttons.Add(lst); + } + + return bf; + } } }