小弟剛接觸C#所以有滿滿的問題......
目前打算在forms裡建一個陣列讓其他usercontrol在執行時可以存值在裡面,但在寫usercontrol程式碼時都會說找不到這個陣列,請問各位C#高手是發生什麼問題QQ
是陣列沒有Public嗎??
forms1
using System;
using System.Windows.Forms;
using System.Collections.Generic;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string[] orderlist = new string[5];
private void button1_Click(object sender, EventArgs e)
{
SidePanel.Height = button1.Height;
SidePanel.Top = button1.Top;
panel2.Controls.Clear();
panel2.Controls.Add(new UserControl1());
}
private void Form1_Load(object sender, EventArgs e)
{
panel2.Controls.Add(new UserControl1());
SidePanel.Height = button1.Height;
SidePanel.Top = button1.Top;
}
}
}
usercontrol
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
orderlist[0] = "漢堡";
this.Controls.Clear();
this.Controls.Add(new UserControl6());
}
}
}
你的orderlist
是定義在Form1
而不是UserControl1
, 當然無法存取....
不知道你完整的功能, 但把orderlist
定義在UserControl1
就能用它了
Form1多做一個功能設定orderlist的值
再由UserControl1呼叫Form1.XXX("漢堡")
C#_WindowsForm_跳頁x單向傳遞資料寫法_雙向傳遞資料筆記
https://coolmandiary.blogspot.com/2016/08/cwindowsformx.html
1.可以用Delegate
2.再創一個新類別 Define_Form.cs
Define_Form:
public static Form _main = new From();
Form1:
在Form1_Load裡
Define_Form._main = this;
在UserControl1裡
Define_Form._main.屬性 或 方法 ...等