2017-08-31 88 views
0

如何創建一個動態的鍵盤按鈕,我找到了一些代碼,但他們對於內聯按鈕? 我想從數據庫中獲取數據,各行中創建一個按鈕,鍵盤按鍵C#創建動態鍵盤電報機器人C#中的鍵盤按鍵沒有內嵌按鈕

var keyboard = new ReplyKeyboardMarkup(
    new[] { 
     new[]{ 
      new KeyboardButton("a"), 
     }, 
     new[]{ 
      new KeyboardButton("b"), 
     }, 
    }); 
+0

歡迎SO。如果您有任何具體問題,請了解該技術的基本知識以及回來。 –

回答

0

第一部分(即增加從數據庫的按鈕)是你寫相同的代碼確定。

第二部分(即改變按鈕佈局)恐怕是不可能的,因爲據我所知。因爲佈局是由客戶端設備中的Telegram應用程序完成的,並且基於很多事情,包括設備屏幕大小,並可能在不同的應用程序版本中更改。

0

使用InlineKeyboardMarkupInlineKeyboardButton代替它,你可以參考this example

請你的庫文件下一次擡頭。

0
private static ReplyKeyboardMarkup calendarMenu; 

SqlDataAdapter sc3 = new SqlDataAdapter("select KeyboardName from dbo.Keyboards", SqlConnection); 
DataTable dt3 = new DataTable(); 
sc3.Fill(dt3); 

int keyboardRows = 0; 
if (dt3.Rows.Count % 2 == 0) 
{ 
    keyboardRows = dt3.Rows.Count/2; 
} 
else 
{ 
    keyboardRows = (dt3.Rows.Count/2) + 1; 
} 

KeyboardButton[][] kbc = new KeyboardButton[(keyboardRows + 1)][]; 

KeyboardButton[] keys = new KeyboardButton[dt3.Rows.Count]; 

var i = 0; 
foreach (DataRow cn3 in dt3.Rows) 
{ 
    keys[i] = new KeyboardButton(cn3["KeyboardName"].ToString()); 
    i++; 
} 

for (int r = 0, s = 0; r < keyboardRows; r++, s++) 
{ 
    if (dt3.Rows.Count % 2 == 0) 
    { 
     kbc[r] = new KeyboardButton[] {keys[r + s], keys[r + s + 1]}; 
    } 
    else 
    { 
     if ((r + s) != keys.Length) 
     { 
      kbc[r] = new KeyboardButton[] { keys[r + s], keys[r + s + 1] }; 
     } 
     else 
     { 
      kbc[r] = new KeyboardButton[] { keys[r + s] }; 
     } 
    } 
} 

kbc[keyboardRows] = new KeyboardButton[] { new KeyboardButton("Return to Main Menu"), }; 

calendarMenu = new ReplyKeyboardMarkup 
{ 
    Keyboard = kbc 
};