我希望为文件中的每一行添加一个按钮.到目前为止我的代码是:
StreamReader menu = new StreamReader("menu.prefs");
int repetition = 0;
while(!menu.EndOfStream)
{
Button dynamicbutton = new Button();
dynamicbutton.Click += new System.EventHandler(menuItem_Click);
dynamicbutton.Text = menu.ReadLine();
dynamicbutton.Visible = true;
dynamicbutton.Location = new Point(4+repetition*307, 4);
dynamicbutton.Height = 44;
dynamicbutton.Width = 203;
dynamicbutton.BackColor = Color.FromArgb(40,40,40);
dynamicbutton.ForeColor = Color.White;
dynamicbutton.Font = new Font("Lucida Console", 16);
dynamicbutton.Show();
menuPanel.Controls.Add(dynamicbutton);
repetition++;
MessageBox.Show(dynamicbutton.Location.ToString());
}
menu.Close();
Run Code Online (Sandbox Code Playgroud)
问题是只创建了第一个控件.
想不出更好的头衔.我只想让别人告诉我这段代码有什么问题:
public AddressChooser(string argstreet, string argsuburb, string argcountry, string argstate, string argunit, int argstreetNumber, int argpostCode)
{
streetNameBox.Text = argstreet;
suburbBox.Text = argsuburb;
countryBox.Text = argcountry;
stateBox.Text = argstate;
unitBox.Text = argunit;
streetNumberBox.Value = argstreetNumber;
postCodeBox.Value = argpostCode;
InitializeComponent();
cancel.DialogResult = DialogResult.Cancel;
save.DialogResult = DialogResult.OK;
}
Run Code Online (Sandbox Code Playgroud)
返回的错误是:
System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=MultipleForms
StackTrace:
at MultipleForms.AddressChooser..ctor(String argstreet, String argsuburb, String argcountry, String argstate, String argunit, Int32 argstreetNumber, Int32 argpostCode) in C:\Users\Yoshie\Local Settings\Documents\Visual …Run Code Online (Sandbox Code Playgroud) 我不能使用GetCultures,从我可以告诉它返回一个空白列表.
private void AddressChooser_Load(object sender, EventArgs e)
{
MessageBox.Show("Form load event successfully triggered") //Debug message - This appears at runtime
foreach (string country in GetCountryList())
{
MessageBox.Show(country); //Debug message - This does not appear at runtime!!
countryBox.Items.Clear();
countryBox.Items.Add(country);
}
}
public static List<string> GetCountryList()
{
MessageBox.Show("Function has been triggered successfully"); //Debug message - This appears at runtime
List<string> cultureList = new List<string>();
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
foreach (CultureInfo culture in cultures)
{
RegionInfo region = new RegionInfo(culture.LCID);
if …Run Code Online (Sandbox Code Playgroud)