這是一個noob問題,但我試圖使用一個字符串(myButton),它是根據調用該方法的按鈕設置的,並試圖在內部使用該字符串一個參數,但我的IDE認爲我直接使用字符串。這裏是代碼:在參數中使用字符串
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 GMA
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string myButton;//Currently selected button
private void button1_Click(object sender, EventArgs e)//This will be replicated for every button on the Form
{
myButton = "button1";
SoundRoute();
}
public void SoundRoute()
{
if ((myButton).Text == string.Empty)//This is the line I'm having trouble with. I want the string of myButton to be converted to button1, button2, etc.
{
SoundCall subForm = new SoundCall();
subForm.Show();
}
}
}
}
我會很感激任何幫助。我知道我可以爲每個按鈕製作一個新的按鈕,但這對於實際學習來說同樣重要。我已經找遍了所有的答案,但沒有運氣。
「我的IDE認爲我在直接使用字符串」是什麼意思?而不是什麼? –
@BenAaronson看起來OP試圖將一個包含'「button1」'的字符串轉換爲表單上的'button1'的實例,這導致了混淆。 –
IDE給你一個關於你的代碼出現問題的非常具體的錯誤信息。即使你不瞭解它,你絕對沒有理由沒有把它包含在你的問題中。特定的錯誤信息比「我的IDE認爲我直接使用字符串」更容易理解。如果你需要幫助,*提供你已經擁有的信息*所以我們也會收到。 –