2016-05-23 26 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 –

相关问题