實現矩陣轉換功能。
矩陣由一個4*4的文字框資料表示,在相應文字框中輸入矩陣元的值,單擊“轉換”按鈕,完成矩陣的轉換。
代碼如下:
public partial class Form1 : Form
{
TextBox[,] tbList = null;
public Form1()
{
InitializeComponent();
//得到介面的矩型TexbBox控制項
tbList = new TextBox[4, 4] {
{txb11,txb12,txb13,txb14},
{txb21,txb22,txb23,txb24},
{txb31,txb32,txb33,txb34},
{txb41,txb42,txb43,txb44},
};
}
/// <summary>
/// 轉換開關
/// </summary>
string sSwitch = "0";
private void Convert()
{
if (sSwitch=="0")
{
sSwitch = "1";
btnConvert.Text = "返原";
}
else
{
sSwitch = "0";
btnConvert.Text = "轉換";
}
string[,] sList = new string[4, 4];
//得到介面所有值
for (int iRow = 0; iRow < 4; iRow++)
{
for (int iCol = 0; iCol < 4; iCol++)
{
sList[iRow, iCol] = tbList[iRow, iCol].Text;
}
}
string sTemp = "";
//得到倒置結果
for (int iRow = 0; iRow < 4; iRow++)
{
for (int iCol = iRow; iCol < 4; iCol++)
{
sTemp = sList[iRow, iCol];
sList[iRow, iCol] = sList[iCol, iRow];
sList[iCol, iRow] = sTemp;
}
}
//介面顯示
for (int iRow = 0; iRow < 4; iRow++)
{
for (int iCol = 0; iCol < 4; iCol++)
{
tbList[iRow, iCol].Text = sList[iRow, iCol];
}
}
}
private void btnConvert_Click(object sender, EventArgs e)
{
Convert();
}
private void btnCancel_Click(object sender, EventArgs e)
{
//清除所有值
for (int iRow = 0; iRow < 4; iRow++)
{
for (int iCol = 0; iCol < 4; iCol++)
{
tbList[iRow, iCol].Text = "";
}
}
}
}