I am trying to loop through components on a form, but components seems to be null.
So, how do I loop through components (NOT CONTROLS) on a form ?
public partial class FormBase : Form
{
public FormBase()
{
InitializeComponent();
FixVisualDesignerIssues();
}
protected void FixVisualDesignerIssues()
{
// this.components is always NULL ????????
foreach (var comp in this.components.Components.OfType<BindingSource>())
{
((BindingSource)comp).do something, whatever
}
}
EDITED as requested
public partial class FormBaseList : Test_app.FormBase
{
public FormBaseList()
{
InitializeComponent();
}
public partial class FormBaseDetail : Test_app.FormBase
{
public FormBaseDetail():base()
{
InitializeComponent();
gttDataGridView1.AutoGenerateColumns = false;
}
If you are creating the BindingSource the following way, only then it will be contained in the components container.
BindingSource bindingSource1 = new BindingSource(components);
If you are using any other way to create the binding source, there will be nothing in the components container.
Related
I got a task that need to share camera view from a webcam into 2 Window. I've tried using singleton for create camera view only one time. The problem is that created instance can not share between 2 class. I'm now really confused. What am I doing wrong here?
MainWindow class
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ReceptionMainWindow receptionMainWindow = new ReceptionMainWindow();
CustomerMainWindow customerMainWindow = new CustomerMainWindow();
receptionMainWindow.Show();
customerMainWindow.Show();
this.Hide();
}
}
Receptionist class
public partial class ReceptionMainWindow : Window
{
public ReceptionMainWindow()
{
InitializeComponent();
ReceptionWindowHoler.Content = CameraPage.getInstance();
}
}
Customer Class
public partial class CustomerMainWindow : Window
{
public CustomerMainWindow()
{
InitializeComponent();
CustomerWindowHoler.Content = CameraPage.getInstance();
}
}
private static CameraPage instance;
Camera class
public CameraPage()
{
InitializeComponent();
//DataContext = CameraViewModel.getInstance();
this.DataContext = this;
GetVideoDevices();
}
public static CameraPage getInstance()
{
if (instance == null)
instance = new CameraPage();
return instance;
}
You could create a Bitmap from the camera view and share it via events to the two other windows.
Take this answer as a workaround. There must be a better way.
I am feeling kind of stupid at the moment, because everywhere I read this is a normal procedure, and I just cannot find why I am not able to do it also!
So, the situation is the following, I have a Parent Form and a Child Form. The Child Form has a public property. From the Parent Form, i want to access the Child Form public property, and I can't.
My code is the following:
Parent code:
namespace myProgram.UserInterfaces
{
public partial class ProjectNew : Form
{
public ProjectNew()
{
InitializeComponent();
}
private void ButtonSelectCustomer_Click(object sender, EventArgs e)
{
using (Form f = new ProjectCustomerList())
{
this.SuspendLayout();
f.ShowDialog(this);
}
this.Show();
}
}
}
Child code:
namespace myProgram.UserInterfaces
{
public partial class ProjectCustomerList : Form
{
public EntCustomer _selectedCustomer = new EntCustomer();
public EntCustomer SelectedCustomer {
get
{
return _selectedCustomer;
}
}
public ProjectCustomerList()
{
InitializeComponent();
}
// --- other code ---
}
}
After the using (Form f = new ProjectCustomerList()) i would like to do the following: var sCustomer = f.SelectedCustomer;, but when I do this, Visual Studio doesn't recognize the Child Form public property.
What am I doing wrong? :|
This is normal with inheritance, since f in your case is handled as a simple Form.
You could typecast it to ProjectCustomerList to access the Property.
The is operator is also useful.
if (f is ProjectCustomerList)
{
(f as ProjectCustomerList).SelectedCustomer =...;
}
or simply
using (ProjectCustomerList f = new ProjectCustomerList())
{
f.SelectedCustomer =...;
}
seen var in other comments, works too
using (var f = new ProjectCustomerList())
{
f.SelectedCustomer =...;
}
Hi i need to be able to use a list from my Main Window in a Usercontrol i need to be able to edit and read from it in various Usercontrols.
MainWindow:
public partial class MainWindow : Window
{
public List<Termin> termine = new List<Termin>();
public MainWindow()
{
InitializeComponent();
}
}
Usercontrol:
public partial class KalenderAnsicht : UserControl
{
public KalenderAnsicht()
{
InitializeComponent();
}
private void SomeMethod()
{
//i need to be able to use the list here
}
}
You need to get a reference to the MainWindow one way or another. The easiest way to do this is probably to use the Application.Current.Windows property:
private void SomeMethod()
{
var mw = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
List<Termin> termine = mw.termine;
//...
}
You could also consider making termine static:
public partial class MainWindow : Window
{
public static List<Termin> termine = new List<Termin>();
public MainWindow()
{
InitializeComponent();
}
}
...and access it directly without a reference to an instance of MainWindow:
private void SomeMethod()
{
List<Termin> termine = MainWindow.termine;
//...
}
I have 2 classes in the same project, ProjectView and FeatureView. I need to access a BindingSource in one class from another class. I have a kluge in which I make the BindingSource scope internal instead of private. Shame, shame. Is there a better way to do this?.
// ProjectView.cs
public partial class ProjectView : System.Windows.Forms.UserControl {
}
// ProjectView.Designer.cs
partial class ProjectView {
// This should be private
internal System.Windows.Forms.BindingSource bsFeatures;
}
// FeatureView.cs
public partial class FeatureView : System.Windows.Forms.UserControl {
// Get ProjectView
Project currentProject = this._presenter.WorkItem.State["CurrentProject"] as Infrastructure.Interface.Aml.BusinessEntities.Project;
string key = System.String.Concat("Project", currentProject.Id);
this._presenter.WorkItem.State["CurrentProject"] = currentProject;
ProjectView view = _presenter.WorkItem.Items.Get<ProjectView>(key);
// Populate currentProject.Features with ProjectView.bsFeatures.List
currentProject.Features.Clear();
IList featureList = view.bsFeatures.List;
foreach (Feature feature in featureList)
{
currentProject.Features.Add(feature);
}
}
maybe something like that, not sure:
partial class ProjectView
{
// This should be private
private System.Windows.Forms.BindingSource bsFeatures;
public System.Windows.Forms.BindingSource BindingSource
{
get { return bsFeatures; }
}
public void ShareOnlyWith(FeatureView fw)
{
fw.BindingSource = bsFeatures;
}
}
of course we break one of the principles, don't depend on concretions.
I'm struggling to get a class from a different form without making it static, here's what I want to do:
//First form
public partial class SetupScreen : Form
{
Control myObject;
public Battleship myBattleship;
public SetupScreen()
{
InitializeComponent();
//Create Class Object
myBattleship = new Battleship();
}
}
//Launch second form
public partial class GameScreen : Form
{
Control myObject;
Battleship myBattleship;
Battleship fredBattleship;
public GameScreen()
{
InitializeComponent();
//Get the class
myBattleship = SetupScreen.myBattleship;
}
}
I keep getting the error "an object reference is required for the non-static field, method or property"
I want the class to be accessible by the whole form, not just a single method therefore I don't want to pass it through each time because this is a hassle
I don't want to make the class static since it cannot be erased, how would I go about doing this?
You are getting this error because you are trying to access a non-static field in a static manner.
Where do you instantiate SetupScreen and GameScreen?
Why not something like this:
public partial class SetupScreen : Form
{
private Control myObject;
public Battleship myBattleship;
private GameScreen gameScreen;
public SetupScreen()
{
InitializeComponent();
//Create Class Object
myBattleship = new Battleship();
gameScreen = new GameScreen(this);
}
}
public partial class GameScreen : Form
{
private Control myObject;
private Battleship myBattleship;
private Battleship fredBattleship;
private SetupScreen setupScreen;
public GameScreen(SetupScreen setupScreen)
{
InitializeComponent();
this.setupScreen = setupScreen;
myBattleship = this.setupScreen.myBattleship;
}
}
Of course, this will only work if you can instantiate GameScreen in SetupScreen. I could give you a better answer if you tell me where/how you are "launching" these forms.
You could pass a reference of your first form to your second form, or (what I would do), create a public Battleship property on your second form and pass your object that way.
//First form
public partial class SetupScreen : Form
{
Control myObject;
public Battleship myBattleship;
public SetupScreen()
{
InitializeComponent();
//Create Class Object
myBattleship = new Battleship();
Form gameForm = new GameScreen(); // New form object
gameForm.MyBattleship = myBattleship; // Set property
gameForm.Show(); // Show form
}
}
//Second form
public partial class GameScreen : Form
{
Control myObject;
Battleship fredBattleship;
public BattleShip MyBattleship { set; get; }
public GameScreen()
{
InitializeComponent();
}
}
you must use singleton pattern.
so your code must be like this:
//First form
public partial class SetupScreen : Form
{
public static SetupScreen setupScreenFrm;
Control myObject;
public Battleship myBattleship;
public SetupScreen()
{
setupScreenFrm=this;
InitializeComponent();
//Create Class Object
myBattleship = new Battleship();
}
}
//Launch second form
public partial class GameScreen : Form
{
Control myObject;
Battleship myBattleship;
Battleship fredBattleship;
public GameScreen()
{
InitializeComponent();
//Get the class
SetupScreen ssFrm=SetupScreen.setupScreenFrm;
myBattleship = ssFrm.myBattleship;
}
}
and first of all, in the start of your app, create an instant of SetupScreen form.
now you can access to SetupScreen in anywhere.