/*您好,我正在嘗試在visual studio上創建我的第一個c#應用程序。我在main中創建了一個類和一個類的實例,我簡單地試圖在表單上的click事件中查詢該實例的成員,但它告訴我實例名稱在當前上下文中不存在。任何幫助將不勝感激這裏是我的代碼。 */訪問主窗體中的實例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication10
{
public class character // this is my class
{
public bool hair_black;
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
character deviljin = new character(); // instance of my class
deviljin.hair_black = true; // initiating a member of the instance
}
}
}
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 WindowsFormsApplication10
{
public partial class Form1 : Form
{
int cs1 = 0,cs2=0;
public Form1()
{
InitializeComponent();
public void pictureBox1_Click(object sender, EventArgs e)
{
flowLayoutPanel1.Visible = true;
if (deviljin.hair_black == true) // trying to access instance member
{ // but getting deviljin does not
// exist in the current context
pictureBox28.Visible = false;
}
}
}
}
這不會讓你得到任何地方。你真的需要閱讀一本書,Winforms編程不是你可以從試驗和錯誤中學到的東西。 –