2016-05-23 27 views
0

我正在做一個小應用程序,我不知道如何打開與點擊按鈕的參數的應用程序的新窗口。例如:如果我點擊氫氣,我想打開名爲prvek的窗體,它將顯示關於它的信息。如何在C#中用按鈕名稱打開新窗體?

對不起,我的英語不好。以下是主窗口的屏幕截圖: Main window

+0

是windows窗體還是web應用程序? –

+0

'fromname.Show();'爲winforms –

+0

WinForms或WPF? (或者別的什麼?)我想象的是關於所使用的技術的任何教程將包括*打開一個表單*,不是? – David

回答

1

在Windows窗體中打開窗體只需創建該窗體的實例並在該實例上調用.Show()。例如:

var someForm = new SomeForm(); 
someForm.Show(); 

如果你想要將其值傳遞給表單,您可以將它們設置爲構造函數的參數。例如,在SomeForm

public SomeForm(int someValue) 
{ 
    // do something with someValue 
} 

然後,當你創建:

var someForm = new SomeForm(aValue); 
someForm.Show(); 

或者,如果不是必需的值,但你碰巧有他們可在這個時候,也許將它們設置爲屬性。在SomeForm

public int SomeValue { get; set; } 

然後,當你創建:

var someForm = new SomeForm(); 
someForm.SomeValue = aValue; 
someForm.Show(); 

或:

var someForm = new SomeForm { SomeValue = aValue }; 
someForm.Show(); 

你在哪裏得到你的價值觀,當然是你。我不確定你的意思是「點擊按鈕的參數」。但是在點擊事件中應該有一個object sender這是觸發事件的UI元素的引用。因此,例如,如果您想要點擊Button中的某個媒體資源,您可以將sender轉換爲Button並閱讀其信息。類似這樣的:

var buttonText = ((Button)sender).Text; 
0

您應該可以爲您的第二個表單prvek提供一個您可以從第一個表單設置的屬性。例如:

public string Element { get; private set; }; 

然後,在你button_onClick方法,你應該能夠做到以下幾點:

ElementForm myForm = new ElementForm(); //Whatever the class name is of your second form 
myForm.Element = ((Button)this).Name; //Get the name of the button 
myForm.Show(); 

在你的第二個窗體的構造函數或初始化方法,你要設置的標題窗體:

public ElementForm() 
{ 
    InitializeComponent() 
    this.Text = Element; 
} 
+0

這是好事嗎?我開始用C#'prvekform = new prvek(); //不管第二種形式的類名是什麼 prvekform.Element =((Button)this).Name; //獲取按鈕的名稱 prvekform.Show();' –

+0

這是正確的。剩下的唯一東西是將表單的文本設置爲構造函數中Element的值。 – Hill

+0

https://yadi.sk/i/DdCedy3arw6tn –

相關問題