我有這個類(從網上獲取):打開窗體使用辛格爾頓和參數傳遞
class SingletonFormProvider
{
static Dictionary<Type, Form> mTypeFormLookup = new Dictionary<Type, Form>();
static public T GetInstance<T>(Form owner)
where T : Form
{
return GetInstance<T>(owner, null);
}
static public T GetInstance<T>(Form owner, params object[] args)
where T : Form
{
if (!mTypeFormLookup.ContainsKey(typeof(T)))
{
Form f = (Form)Activator.CreateInstance(typeof(T), args);
mTypeFormLookup.Add(typeof(T), f);
f.Owner = owner;
f.FormClosed += new FormClosedEventHandler(remover);
}
return (T)mTypeFormLookup[typeof(T)];
}
static void remover(object sender, FormClosedEventArgs e)
{
Form f = sender as Form;
if (f == null) return;
f.FormClosed -= new FormClosedEventHandler(remover);
mTypeFormLookup.Remove(f.GetType());
}
}
如果使用標準的開放,我知道如何傳遞參數:
Form f = new NewForm(parameter); f.Show();
但我使用這種方式打開新窗體(在上述類的幫助下):
var f = SingletonFormProvider.GetInstance<NewForm>(this); f.Show();
那麼,怎樣才能以這種方式打開新窗體的參數?
請幫忙。
謝謝。
你是什麼意思的參數? –
參數,變量,對象 – Iyas
像這樣:Form f = new NewForm(textBox1.Text); f.Show(); – Iyas