I have a main form that includes TabControl. New forms are added as tabs in TabControl. I have trouble changing current active tab or closing tabs from forms that are not the form with TabControl. All components that i'm trying to access are Public.
Here is the code i used to change tabs:
Main mainForm = new Main();
mainForm.tcMain.SelectTab(mainForm.tpHome);
It doesn't work even if i try it to change it to index of the first tab, 0.
I also have a Label in Main form that i'm trying to change from other forms like this:
Main mainForm = new Main();
mainForm.labelStatus.Text = "Refreshed";
If it would be of any help, this is how the form i'm trying to access main form from is called
Table tableForm = new Table();
tableForm.TopLevel = false;
TabPage tableTab = new TabPage(tableForm.Text);
tcMain.TabPages.Add(tableTab);
tableForm.panelTable.Parent = tableTab;
tableForm.Parent = tableTab;
tcMain.SelectTab(tableTab);
tableForm.Show();
If you create a new Instance of Main for every property you modify, you are actually creating that many windows forms, and they have no relation to each other, They just sit in memory until they are shown to the user. What you need is a reference to the actual Main form created at the start of the application. For that, have the controls that you want to modify as public and something like below will do.
Main Class Global :
static public Main instance;
Main Class Main_Load() method :
instance = this;
In all your other forms just access main form instance like Main.instance. So this
Main mainForm = new Main();
mainForm.labelStatus.Text = "Refreshed";
will become
Main.instance.labelStatus.Text = "Refreshed";
The important thing here will be to set the access modifiers of all the controls you want to modify in the Main form to public.
Related
What is the difference between a user control and a windows form in Visual Studio - C#?
Put very simply:
User controls are a way of making a custom, reusable component. A user control can contain other controls but must be hosted by a form.
Windows forms are the container for controls, including user controls. While it contains many similar attributes as a user control, it's primary purpose is to host controls.
They have a lot in common, they are both derived from ContainerControl. UserControl however is designed to be a child window, it needs to be placed in a container. Form was designed to be a top-level window without a parent.
You can actually turn a Form into a child window by setting its TopLevel property to false:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
var child = new Form2();
child.TopLevel = false;
child.Location = new Point(10, 5);
child.Size = new Size(100, 100);
child.BackColor = Color.Yellow;
child.FormBorderStyle = FormBorderStyle.None;
child.Visible = true;
this.Controls.Add(child);
}
}
A windows form is a container for user controls.
The biggest difference is form.show gives a different window while usercontrol doesnt have feature like popping up without a parent. Rest things are same in both the controls like beind derived from Scrollablecontrol.
A User Control is a blank control, it's a control that's made up of other controls. Building a user control is similar to building a form. It has a design surface, drag and drop controls onto the design surface, set properties, and events. User controls can consolidate UI and code behind. User controls can only be used in the project where they're defined.
I have a Parent Form, on the press of a button, the following code runs:
UCDataSetSearch dataSetSearch = new UCDataSetSearch(formStorage.Schema, "Schema");
InitializeUserControl(dataSetSearch);
On this UserControl the user selected a DataSet and can then modify it by clicking the Modify button, however what is the correct way for on pressing that button in the UserControl it then creates a new instance of another UserControl on the parent form?
InitializeUserControl method just takes a User Control and adds it to the controls of the form and sets its Location to a specific point in the form.
If InitializeUserControl() is a Public method on your Form, then you can call it directly utilizing FindForm() like this:
// ... running from within the UserControl ...
// assuming Form1 is the parent form (change as necessary)
Form1 f1 = (Form1)this.FindForm();
UCDataSetSearch dataSetSearch = new UCDataSetSearch(formStorage.Schema, "Schema");
f1.InitializeUserControl(dataSetSearch);
This is a tightly coupled solution, which means your UserControl is now less re-usable as it can no longer work with other Forms.
A loosely coupled solution would have the UserControl raise a Custom Event to let the Form know it should add a new instance to itself.
I'm trying to build an Application UI using Winform for which will be having multiple pages inside it. Say software will be asking for a Login credentials on startup and then landing in a Dashboard. Then the user will have the option to go different pages like: Page1 - Page2 - Page3.
Now I'm planning to make one Form and all these pages will be separate UserControls. So as per requirement I will be changing the visibility of these UserControls.
Now to do this I'm putting the below code inside Form1.cs
ControlLogin ucLogin = new ControlLogin();
ucLogin.Location = new System.Drawing.Point(12, 67);
this.Controls.Add(ucLogin);
This works fine. But while opening any UserControl from this ControlLogin.cs how will I add the new UserControl (say Page1Control) to the list of Form1?
You need to develop some transaction logic for your pages. I suggest that on your main form you use a panel to use as container. In this container you will place current user control, the one that user selects.
For example:
internal void ReplaceUserPage(Control container, UserControl userRequest)
{
if (container.Controls.Count == 1)
{
container.Controls.RemoveAt(0);
}
container.Controls.Add(userRequest);
userRequest.Dock = DockStyle.Fill;
}
If you don't have dynamic pages, you can make all of them singletons. This way, instance of each will be created on demand and live in memory, ready to reuse. So, when user clicks on a menu or a button to open the page, you can do
UserControl requested = Page1Control.GetInstance();
ReplaceUserPage(container, requested);
With singleton, you don't even need to keep list of your controls. I don't say that this is best or perfect or one-fits-all way. There are many control transaction approaches. It depends on system complexity and other factors.
The basic layout you chose looks fine to me.
Your actual question seems to be: How to reference the form from those UCs?
This is closely related to the questions: How to reference a form or parts of it from other forms? This has been asked here very often..
Here is what I suggest you should do:
Create a public function for opening each of your UCs openLogin, openPageOne..
Change the constructors of each UC to include a Form1 as a parameter (assuming your form has the default name) and call it accordingly like this: ControlLogin ucLogin = new ControlLogin(this);
In the UCs constructors you want to store the passed in form in a class variable.
In the form you write:
public void openLogin(Form1 f)
{
ControlLogin ucLogin = new ControlLogin(this);
ucLogin.Location = new System.Drawing.Point(12, 67);
this.Controls.Add(ucLogin);
}
public void openPageOne(Form1 f)
{
..
}
And in the UC(s):
public ControlLogin(Form1 form1)
{
InitializeComponent();
mainForm = form1;
}
Form1 mainForm = null;
Now you can reference all public fields and methods in the form, maybe like this
if (logingIsOK) mainForm.openPageOne();
I am developing a very basic application Windows Form Application in c# that inserts values into a SQL database.
I have four separate forms
one for inputting customer details
one for inputting any transaction details
one for searching customers/transactions respectively.
What is the best way link all four forms together? I'm only just getting into C# so the most basic way possible would be ideal.
The way I see it working in my head is that you run the program and end up on one screen which shows four buttons for the four corresponding forms. When you press the button a separate window opens showing the insert forms. You can then close the form to return to the Main Start Screen
What would be the basic code for this in C#? For examples sake lets say the 5 different layouts are
Main (Containing the buttons)
TransactionEntry
AddressEntry
TransactionSearch
AddressSearch
Okay, here is an example of how I would do it. On the main form on button click event:
frmSecondForm secondForm = new frmSecondForm(this); //"this" is passing the main form to the second form to be able to control the main form...
secondForm.Show();
this.Hide();
One your Second Forms constructor code:
Form frmHome;
frmSecondForm(Form callingForm) //requires a calling form to control from this form
{
Initialize(); //Already here
frmHome = callingForm as frmMain;
}
Then on the second form's closing unhide the main form:
frmSecondForm_FormClosing()
{
frmHome.Show();
}
So all in all, if you needed to pass data between the forms just add it as a parameter on the second form.
Again, I would consider placing your data collection into a repository package (folder) and class, then create a User.cs class that will hold all of the information you keep in the database.
Hint: Right click your top level item in solution explorer and go to New -> Folder. Name it Repo.
Right click that folder and go to New -> Class and name it UserRepo.
In here build functions to collect the data from the databases.
On your main form's constructor area, call the class (repo).
private Repo.UserRepo userRepo;
frmMain_FormLoad()
{
userRepo = new Repo.UserRepo();
}
Then on log in button click:
private button1_ClickEvent()
{
if(userRepo.isValidLogin(userNameText, passwordText))
{
//Do stuff
}
}
for userRepo.isValidLogin()
public bool isValidLogin(String username, String password)
{
bool isValid = false;
//Write up data code
return isValid;
}
From the Main form use eg:
TransactionEntry trans = new TransactionEntry();
trans.ShowDialog();
.ShowDialog() will show the new form, but will halt any code executing on the Main form until you close it
(This assumes your forms are all in the same solution)
You could try the MDI (Multiple Document Interface) way, here is a nice tutorial: http://www.dreamincode.net/forums/topic/57601-using-mdi-in-c%23/
What is the difference between a user control and a windows form in Visual Studio - C#?
Put very simply:
User controls are a way of making a custom, reusable component. A user control can contain other controls but must be hosted by a form.
Windows forms are the container for controls, including user controls. While it contains many similar attributes as a user control, it's primary purpose is to host controls.
They have a lot in common, they are both derived from ContainerControl. UserControl however is designed to be a child window, it needs to be placed in a container. Form was designed to be a top-level window without a parent.
You can actually turn a Form into a child window by setting its TopLevel property to false:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
var child = new Form2();
child.TopLevel = false;
child.Location = new Point(10, 5);
child.Size = new Size(100, 100);
child.BackColor = Color.Yellow;
child.FormBorderStyle = FormBorderStyle.None;
child.Visible = true;
this.Controls.Add(child);
}
}
A windows form is a container for user controls.
The biggest difference is form.show gives a different window while usercontrol doesnt have feature like popping up without a parent. Rest things are same in both the controls like beind derived from Scrollablecontrol.
A User Control is a blank control, it's a control that's made up of other controls. Building a user control is similar to building a form. It has a design surface, drag and drop controls onto the design surface, set properties, and events. User controls can consolidate UI and code behind. User controls can only be used in the project where they're defined.