I need some help cause I don't know what's wrong with my new project!
I stared a new Empty Project (.NET) in VS 2019, added same references to create form and draw different controls on it like buttons, labels and so on... Everything looks good for me and I don't get any warning/error. When I run my project it shows the form how I expected, but it's empty... no buttons or labels on the form, only the whitish coloured blank window [see picture].
I'm not familiar with the empty projects, so probably I missed something.. I tried to type "InitializeComponent()" to the class constructor but I got an error "Error CS0103 The name 'InitializeComponent' does not exist in the current context".. I don't use the designer as this is an empty project and it's not come with it (and I don't need it!), so I suppose I don't need this method?
Here's the code and pictures to help localize the issue.. Probably just a noob mistake somewhere but I can't figure it out.. Please help!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FileManager
{
class EntryPoint
{
static void Main(string[] args)
{
Manager manager = new Manager();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
namespace FileManager
{
public class Manager
{
public Form WindowMain { get; set; }
public ListView ViewArea { get; set; }
public ImageList IconList { get; set; }
public Button Back_btn { get; set; }
public TextBox Path_tbx { get; set; }
public Label FileNameTxt_lbl { get; set; }
public Manager()
{
//InitializeComponent(); << Drop Error CS0103
WindowMain = new Form
{
StartPosition = FormStartPosition.CenterScreen,
FormBorderStyle = FormBorderStyle.Sizable,
Size = new Size(960, 540),
Text = "FileManager"
};
ViewArea = new ListView
{
SmallImageList = IconList,
LargeImageList = IconList,
View = View.LargeIcon
};
IconList = new ImageList
{
ImageSize = new Size(48, 48),
ColorDepth = ColorDepth.Depth32Bit
};
Back_btn = new Button
{
Location = new Point(100, 100),
Text = "Back"
};
FileNameTxt_lbl = new Label
{
Text = "File Name:"
};
Application.Run(WindowMain);
}
}
}
I find the solution of my issue! It was quite simple to display the controls on my form... I should have add each of my control (buttons etc..) to the control collection of my form object (WindowMain), so simple!
Here's my fixed code:
namespace FileManager
{
public class Manager
{
public Form WindowMain { get; set; }
public ListView ViewArea { get; set; }
public ImageList IconList { get; set; }
public Button Back_btn { get; set; }
public TextBox Path_tbx { get; set; }
public Label FileNameTxt_lbl { get; set; }
public Manager()
{
WindowMain = new Form
{
StartPosition = FormStartPosition.CenterScreen,
FormBorderStyle = FormBorderStyle.Sizable,
Size = new Size(960, 540),
Text = "FileManager"
};
ViewArea = new ListView
{
SmallImageList = IconList,
LargeImageList = IconList,
View = View.LargeIcon
};
IconList = new ImageList
{
ImageSize = new Size(48, 48),
ColorDepth = ColorDepth.Depth32Bit
};
Back_btn = new Button
{
Location = new Point(100, 100),
Text = "Back"
};
FileNameTxt_lbl = new Label
{
Text = "File Name:"
};
// These lines need to fix:
WindowMain.Controls.Add(ViewArea);
WindowMain.Controls.Add(Back_btn);
WindowMain.Controls.Add(FileNameTxt_lbl);
Application.Run(WindowMain);
}
}
}