只需學習C#,單選按鈕和複選框。沒有緊迫感。 該代碼用於顯示檢查控件的名稱,但它似乎不是一個優雅的解決方案。更好的解決gui的代碼?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TVC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "you clicked" + compute();
}
string compute()
{
string result = "";
object o=label1;
while (((Control)o).TabIndex!=7)
{
if ((o is RadioButton)||(o is CheckBox))
{
if ((o is RadioButton)&&((RadioButton)o).Checked)
result += " "+((RadioButton)o).Text;
if ((o is CheckBox)&&((CheckBox)o).Checked)
result += " "+((CheckBox)o).Text;
}
o = GetNextControl((Control)o, true);
}
return result;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
的checboxes和單選按鈕tabindexes從-1至6計數,標記爲0,按鈕7,使得GetNextControl工作。有更好的代碼可以工作嗎?
無論你做什麼,總是避免幻數。即使這是最後的代碼,將這個'7'提取到一個單獨的常量。 – rid 2012-03-22 04:16:12
+1對於魔術數字 – 2012-03-22 04:18:53