I had two forms form1 and form2 .I need to acess the datas of toolbar components of form 2 from form1
it may be like
string s= form1.lblstatus.text ;
and when I searched it says the control in form 2 must be declared private to acess like this but how can we set the acessibility mode for labels and other controls in vs2010
You need it public, not private.
You can do this in Designer mode (what it seems you're using) of VS2010 as follows:
Click on the Control you need to access.
On the bottom-right of the screen, in the "Properties" window, scroll down to "Modifiers" and click on it.
Click on the arrow that appears to the right of "Private".
Click on "Public".
Assuming you are only reading the data, you could create a public, read-only property on Form2.
public string Status
{
get
{
return lblStatus.Text;
}
}
Related
I am making a game using Windows Forms, and I feel that the easiest way to make different screens (Main Menu, Settings, Etc...) is using a separate form for each screen. I have looked up up many different ways to do this. Many people talk of using form2.Show(); this.Hide();, however, this creates a new popup window.
People have also suggested using this.IsMdiContainer = true; to create a new window inside of the current window, but this is also not the functionality I want.
EDIT: Another question, multiple-guis-one-window, explains slightly that this can be achieved using UserControls, but does not elaborate with any examples, or what I should do.
I want to completely replace the current form's functionality with that of a new form.
Is there a way to achieve this? If not, is there a good alternative?
As I understood, you want to keep the same form and change the data inside that form (to not get the effect that your form was closed and reopened again).
The problem is windows form does not support such thing directly (at least not in an optimal way), I would use another UI frameworks for that. However, if you want to stick though with windows forms, you may use GroupBox or Panel tools (available from windows form design tools in Visual studio). Here you can group your elements as required, and show/hide the group/panel as required.
That is one of the best available solution for windows form AFAIK.
You want to open child forms within main form then you should try this i have create it without any User Control.
I have manage one parent form and two child forms. child forms should be open within parent form.
frmMain(Parent Form)
Set frmMain Property as Following
WindowState = System.Windows.Forms.FormWindowState.Maximized
I have take 3 panel in frmMain window.
pnlMenu (For Display Menu)
Set pnlMenu.Dock = System.Windows.Forms.DockStyle.Top property
Set height of this panel as you require.
pnlMain (For Display Child Forms)
Set pnlMain.Dock = System.Windows.Forms.DockStyle.Fill property
pnlFooter (For Footer Section)
Set pnlFooter.Dock = System.Windows.Forms.DockStyle.Bottom; property
Set height of this panel as you require.
I have set menubar in pnlMenu(Click on that menu for display child form in pnlMain)
frmMain.cs
public void BindFormIntoMainForm(Form Main)
{
Main.TopLevel = false;
Main.WindowState = FormWindowState.Maximized;
Main.AutoScroll = true;
pnlMain.Controls.Clear();
pnlMain.Controls.Add(Main);
pnlMain.Refresh();
Main.Show();
}
private void childToolStripMenuItem_Click(object sender, EventArgs e)
{
frmChildForm1 ChildForm1 = new frmChildForm1();
BindFormIntoMainForm(ChildForm1);
}
private void childForm2ToolStripMenuItem1_Click(object sender, EventArgs e)
{
frmChildForm2 ChildForm2 = new frmChildForm2();
BindFormIntoMainForm(ChildForm2);
}
BindFormIntoMainForm method responsible for display child form in main window.
frmChildForm1 & frmChildForm2(ChildForm)
Set both form Property as Following
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
Now when you click on Child Form 1 Menu then display following output:
when you click on Child Form 2 Menu then display following output:
I think it can be helpful for you.
The majority of my forms for my project include an OK and Cancel button. (always positioned at the bottom right of the form). Currently I have a base class that inherits from System.Windows.Forms which contains an OK and Cancel button. All forms that use this then inherit from this base form class. Is there a better way of doing this that takes localization into consideration?
I would use MDI Child Forms for this. Parent Form Can contain OK/Cancel button where as you would have your child form in MDI container.
For More help visit
https://msdn.microsoft.com/en-us/library/aa984329(v=vs.71).aspx
You could just create a single form that has an empty panel or table layout, where you dynamically load the desired user control. It is basically the composition over inheritance principle.
public partial class MyFormWithButtons : Form
{
public MyFormWithButtons(UserControl control)
{
InitializeComponent();
control.Dock = DockStyle.Fill;
myPanel.Controls.Add(control);
}
}
Doing form inheritance is very useful in many levels:
Make a base form, and name it for ex: FrmBase.
Add the Ok, Cancel Buttons to it and set the Anchor property for both to Bottom.
Set the Buttons "Modifiers" property to "Internal", this way you can access these buttons from inherited forms:
Make as many forms as you want and make each inherit from the FrmBase ex: Form1 : FrmBase
now you can access the buttons from this from, using the properties.
Hope this being useful for you.
is there a way to quick copy selected control name to paste it then to text editor? i have lot of control that i have to set my class property to, then i have to select each one then click on properties, then select name and ctrl+C. Is there no way to just select control on designer then ctrl+C+someOtherKey end voila i can paste it to text editor ? (or even maybe there is a way to quick copy of name of all selected contols :) ?
I use visual studio proffesional 2012 (going to change it to 2015 in near future).
What I would do is create my own custom extention. Say for example that you want all your Buttons to be green and say "Zelda". You would create the following class.
public GreenZeldaButton : Button
{
public GreenZeldaButton()
{
BackColor = Color.Green;
Text = "Zelda";
}
}
Then you have to get this into the Toolbox, I'm not sure of the proper method but what I do is create a new instance in the form contructor. As so:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
new GreenZeldaButton();
}
}
And then press F5 to debug in order to add the new control to the toolbox. Then you can drag and drop the new control from the toolbox to the Designer. This is what I do when I want multiple copies of a control with specific properties. Hope this helps ^_^. P.S. You can remove the "new GreenZeldaButton();" thing (or your alternative obviously) after the control has been added to the toolbox.
Currently I have a C# program with a windows form and then a user control template put onto the form. The user control template is really just used as a placeholder. I have a series of other controls which inherit from this user control template.
Each of those controls have navigation buttons like 'Continue' and 'Back' on them and each control knows which control needs to be loaded next. However what I need to figure out is an easier way to have variables that are global to these controls.
The only workaround I have is that I pass the form to each control when they are loaded and use variables inside of the form to read and write to. What would be the proper way to have each of these user control screens be built off of a base control which contained objects all of the controls could get to?
Sorry for the rambling nature of the post but I've been thinking about this problem all morning.
Here is some of the code:
Most of what I have written was based on hiding and showing the user controls so that content in the controls wouldn't be lost during navigation. I won't be needing to do that as eventually it will be loading the fields of data from a database.
Code for initially loading control from form click:
conTemplate1.Controls.Clear();
conInbound Inbound = new conInbound(this);
Inbound.Dock = DockStyle.Fill;
Inbound.Anchor = (AnchorStyles.Left | AnchorStyles.Top);
conTemplate1.Controls.Add(Inbound);
Code for Continue button inside of one of the controls:
if ((Parent.Controls.Count - 1) <= Parent.Controls.IndexOf(this))
{
UserControl nextControl = new conPartialClear();
nextControl.Dock = DockStyle.Fill;
Parent.Controls.Add(nextControl);
this.Hide();
Parent.Controls[Parent.Controls.IndexOf(this) + 1].Show();
}
else
{
this.Hide();
Parent.Controls[Parent.Controls.IndexOf(this) + 1].Show();
}
The best-practice for communicating from a control to a parent is to use events, and for communicating from a parent to a control is to call methods.
However, if you don't want to or can't follow this practice, here's what I would recommend.
Each UserControl has a ParentForm property that returns the Form that contains the control. If you know that the UserControl will always be attached to MyParentForm, you just cast the ParentForm and then you can access all public controls, methods, etc.
Here's what I mean:
public class conTemplate
{
public MyParentForm MyParentForm
{
get
{
return (MyParentForm)this.ParentForm;
}
}
}
This way, you can easily access any public members of MyParentForm. Your conInbound class could have code such as this.MyParentForm.GlobalSettings.etc..., and could even have access to any public controls.
I'm not totally sure I understand your problem. It sounds like you want the user control to "do something" with it's parent form. If that's the case, you may want to consider adding events to the UC and then handle them on the form itself.
Basically, for your UC's "continue", you'll have an event that's fired when it's pressed. You'll want to handle that in your form. I'm not real sure about the syntax from memory, or I'd work something out for you code-wise. But I think that's the route you'll want to take. Think of your UC like any other windows form control. If you add a button to your form, you assign it it's event method. Do the same with the UC.
I found this and thought it may be helpful. Scroll down to where it talks about UC's and events.
http://www.akadia.com/services/dotnet_user_controls.html
Hope this helps.
EDIT after new info from OP.
You could declare a global variable inside the UC of type yourForm and then set that variable to the ParentForm at run-time, if I'm understanding you correctly.
So, inside your UC Class, you could do:
private parentFormInstance;
then inside the constructor of the UC, you could set it as such:
parentFormInstance = this.ParentForm; (or whatever the property name is).
This allows you at design-time to use:
parentFormInstance.DoSomething();
without the compiler yelling at you.
Just basic advice, but if you can go back and make it easier on yourself, even if it takes some additional time re-working things, it'd be worth it. It may save you time in the long run.
I googled this and still cant get it to work. I know how to add a tab using the toolbox. I have also read about how to do it programmatically, but i still dont get it. (MSVC# Express 2010)
I have an easy project set up. Just a windows Form with a TabControl in it, i used the Designer to add a new TabControl and made that TabControl public instead of private.
I wrote this code to
a) access the Windows Form
b) add a tabpage.
The code compiles just fine, but the Tabpage is not displayed during runtime.
static class Program
{
[STAThread]
static void Main()
{
Application.SetCompatibleTextRenderingDefault(false);
Application.EnableVisualStyles();
Form1 ApplicationMainForm = new Form1();
Application.Run(ApplicationMainForm); //LABEL B
ApplicationMainForm.tabControl1.TabPages.Add("MyPage"); //LABEL A
}
}
How can i get the form to display my Tabpage?
My TabPage is displayed when the Lines A and B change position. Am i missing an update method, oder is the TabPage Add never called until the application closes?
Edit #1: Some minor edits.
Edit #2: Edited in some more examplecode.
Edit #3: Removed some earlier / irelevant points.
Edit #4: Found a hint and edited this information in
Form1.tabControl1.Controls.Add(myNewTabItem);
The tab control is a collection of tab pages, so you add tab pages like you add any control to a collection. Note that the tabs show up in the order you add them.
If you are trying to add a tab to the form at runtime, probably the issue is that you are trying to adjust the form definition instead of the specific instance of that form that you are currently displaying. When a form is opened, it is an instance of the form definition. You have to find that instance in order to modify its properties. Therefore, you would have to have the other part of your program somehow know about that particular instance of your form through something like a reference variable.