cannot convert from string to system.windows.forms.string iwin32window
This is supposed to show the winner in the xWinner form label but I cant figure it out. xWinnerForm.Show(b1.Text);. I'm new to c# so can you please explain in layman terms thanks.
static public bool CheckWinner(Button[] myControls)
{
bool gameOver = false;
for (int i = 0; i < 8; i++)
{
int a = Winners[i, 0];
int b = Winners[i, 1];
int c = Winners[i, 2];
Button b1 = myControls[a], b2 = myControls[b], b3 = myControls[c];
if (b1.Text == "" || b2.Text == "" || b3.Text == "")
continue;
if (b1.Text == b2.Text && b2.Text == b3.Text)
{
gameOver = true;
Form xWinnerForm = new xWinnerForm();
xWinnerForm.Show(b1.Text);
}
public void Show(string text)
{
this.xWinnerLabel.Text = text;
this.Show();
}
}
ret开发者_C百科urn gameOver;
}
This is the smallest change you need to make it work:
xWinnerForm xWinnerForm = new xWinnerForm();
Though I'd suggest a few changes in addition to this:
- Use PascalCase for class names, but do use camel case for the variable name.
XWinnerForm xWinnerForm = new XWinnerForm();
- Don't overload
Show
in this manner. Instead change the constructor of your form to accept the extra data, or else add a setter to your form.
XWinnerForm xWinnerForm = new XWinnerForm(b1.Text);
- Don't use names like
a, b, c, b1
:
XWinnerForm xWinnerForm = new XWinnerForm(labelWinner.Text);
Currently the variable (xWinnerForm
) is typed as Form
, and Form
has a Show
method with a different signature. Instead, tell it which specific type of Form
it is - a WinnerForm
perhaps (your class name...). Or use a different method name (Show
is going to cause confusion).
Form xWinnerForm = new xWinnerForm();
This line is your problem. Because you're declaring your variable as a Form
, the compiler does not know about your Show(string)
function.
As an aside, it's generally bad form to use a variable name that's the same as its type.
Instead, do something like this:
xWinnerForm winner = new xWinnerForm();
winner.Show(b1.Text);
精彩评论