Interaction between components of 2 forms - c#

At first i disable a Toolstrip menu item so that when a user click the "Enable" button, the Toolstrip menu item can be enablem like:
private System.Windows.Forms.ToolStripMenuItem QLKHTSM;
The QLKHTSM is disable on the forms.
The problem is the Enable button is on the other form, so i tried to interact between 2 forms by this code(under the same form of that ToolStripMenuItem)
public static void enabletoolstrip()
{
QLKHTSM.enable = true;
}
but the problem is with static, the QLKHTSM is unavailable, and without static, i can't call it in the other form.
Please help. Thanks.

In the form where QLKTHSM is, Go to be Properties of QLKTHSM and change the Modifier property to Public. Then go to the second form and use.
public void enabletoolstrip()
{
FirstForm f1 = (FirstForm)Application.OpenForms["FirstForm"];
f1.QLKHTSM.Enabled = true;
}
If the form with QLKHTSM is not yet shown then you can create a global object.
FirstForm f1 = new FirstForm();
Then in your enable tool strip
public void enabletoolstrip()
{
f1.QLKHTSM.Enabled = true;
}
Then wherever you want to show the form you use
f1.Show();

Related

Reloading the form instead of making a new one / accessing textbox outside of form

I am having a problem controlling a textbox. I need to change a value from outside of Form1.cs where the textbox is located for this I have found this snippet.
public void UpdateText(string newValue)
{
torrent_name0.Text = newValue;
}
this allows me in theory to control the textbox outside of Form1.cs, but here comes the problem, every time I want to access it I create a new instance of Form1 instead of using the old one and refreshing it.
Form1 frm = new Form1();
frm.UpdateText("aaaaaaaaaaaa");
frm.Show();
am I missing something? is there a better way to do this? I have tried multiple ways to update the new form but got nowhere.
Bokker,
You will have to have a reference to the singular form1 to which all things refer.
If this is a child form, then as Aybe commented, create a public member of your mainform and name it something.
Public Form myForm1;
You probably have some Event by which you would like Form1 be launched...
A Button click, menu item, toolbar item, etc. In that event you will have to check if the form exists and create if required.
private SomeEvent() {
if (myForm1 == null)
{
myForm1 = new Form1();
myForm1.Show(this);
}
myForm1.UpdateText("some text");
}
Alternatively, you could create the form in the Form Load event, just so long as you create the form prior to attempting the myForm1.UpdateText()
If you follow this paradigm, you are creating myForm1 as part of the main form, best practice says you should also dispose of the form in your main form Closing Event.
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (myForm1 != null)
{
myForm1.Close();
myForm1.Dispose();
}
}
This is all off the top of my head, so it might not be perfect.
In that case you can pass the instance of form in the method as argument and make the changes like
public void UpdateText(string newValue, Form1 frm)
{
frm.torrent_name0.Text = newValue;
}

how to change a control property from another form

I have two forms in a windows form application. lets call them "first form" and "second form".
i want by clikcing on a button on second form, change the property of one of the controls of the first form. i've defined an event for this. by that i mean when i click on the second form's button, a method within the first form is called. here's the method:
// changes the visibility of the specified control
public void change_visibility()
{
this.new_customer_label.Visible = true;
}
but when i set a breakpoint on this method and check the value after it is executed. the property has't changed. what is wrong? thanks in advance
note: on the second form button's click event, i also close the form.
So firstly, Open up Form1.designer.cs and change the control to public
Form1 this will open Form 2.
Form2 frm2 = new Form2();
frm2.Owner = this;
frm2.Show();
Form2 this will change the property of a control in Form 1
(this.Owner as Form1).label1.Visible = true;
Here is an example of what you can do:
class Form1 : Form {
private Label labelInForm1;
public string LabelText {
get { return labelInForm1.Text; }
set { labelInForm1.Text = value; }
}
}
class Form2 : Form {
Form1 form1; // Set by the property
private Form1 Form1 {
get { return form1; }
set { form1 = value; }
}
private ChangeVisibility()
{
Form1.labelInForm1.Visible = true;
}
}
"note: on the second form button's click event, i also close the form."
Then it would probably be a better design to display the second form with ShowDialog() instead of Show(). Something like:
Form2 f2 = new Form2();
f2.ShowDialog(); // code STOPS here until "f2" is closed
this.new_customer_label.Visible = true;
By default, the designer generates code in the 'Form1.Designer.cs' class. In there, you can see that all the controls are set private, change them to public and then try again...
As you will search this problem on internet you will find different solutions but I think the best solution is to make controls public then you can access these controls from any form.
Follow these instructions.
Open your desire form whose control properties you want to access
Open Form.Designer.cs
Change the desire control class to public
Go to the main form where you want to access or change property
Write this code
Form Form2 objForm=new Form2();
Now access your control property here
objForm.new_customer_label.Visible=true;
I hope this will be helpful to you!!!

Windows Forms C#

I am learning windows forms and can create a one form with textboxes and stuff, but I was wondering how can I change the form upon let's say clicking a button?, so for instance my initial form has a textbox and a button, if the button is clicked I want to show a form with a dropdown and a button. SO the question should be:
1) How do I change the form upon clicking a button but without creating a new instance of the form.
2) If I wanted, how can I add a form when the button is clicked showing the same drop down and button as a pop up form?
In reality I would like to know both cases, changing the form via using the same form and via popping a new form on top.
Should the questions not be clear, I am willing to further explain
Thank you
I'm assuming you already know how to add controls in the form designer and how to implement event handlers.
Question 1
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Visible)
{
comboBox1.Visible = false;
textBox1.Visible = true;
}
else
{
comboBox1.Visible = true;
textBox1.Visible = false;
}
}
The button click handler simply toggles the visibility of the two controls.
Question 2
private void button2_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
form.ShowDialog();
}
This time the button handler instantiates a new form an then shows it as a modal dialog. Call Show() if you don't want to show modally.

How to disable multi clicks in the c# application programming?

In the c sharp application program, if a button is clicked then a dialog box will be opened but if the user clicked more than one time then more dialog boxes are opened. How to overcome it?
Please give me the solution regarding this issue.
If its a desktop application use Modal Dialogs.
use ShowDialog function:
using (Form2 frm = new Form2())
{
frm.ShowDialog();
}
This will disable the current form and only make the new form usable.
Alternatively, you can disable the button to prevent it from being clicked again.
button1.Enabled = false;
But make sure you enable the button when it should be accessible again.
Furquan has a good answer. Edit: as does Fun.
If you can't or don't want your dialog to be modal, you can add extra state to see if the subdialog is already open. Here's some example pseudo-code (it probably won't compile):
class MyForm : Form
{
public void OnButtonClick()
{
if(!isSubDialogOpen)
{
isSubDialogOpen = true;
ShowSubDialog();
}
}
private void OnSubDialogClose()
{
isSubDialogOpen = false;
}
private void ShowSubDialog()
{
SubDialog subDialog = new SubDialog(this);
subDialog.OnClose += OnSubDialogClose;
subDialog.Show();
}
private bool isSubDialogOpen;
}
class SubDialog : Form
{
// ...
}

How to access controls on an inherited form?

I'm using the DockPanel Suite in my winforms app. The DockContent class is derived from System.Windows.Forms.Form class and my two forms, dockRounds and dockToolbox, inherit from the DockContent class.
This is the first time I've done this and this is probably a stupid question, but in runtime, how do I access the controls of the dockRounds and dockToolbox forms?
Here is how I load the two forms when the app first runs:
public partial class frmMainNew : Form
clsMWDockPanel mapPanel;
dockToolbox dockT = new dockToolbox();
dockRounds dockR = new dockRounds();
public frmMainNew()
{
InitializeComponent();
dockPanel = new DockPanel();
SuspendLayout();
dockPanel.Parent = panelMain;
dockPanel.Dock = DockStyle.Fill;
dockPanel.DefaultFloatWindowSize = new Size(108, 528);
dockPanel.BringToFront();
dockPanel.BackColor = Color.Transparent;
dockPanel.DocumentStyle = DocumentStyle.DockingSdi;
ResumeLayout();
string error = "Errors:\r\n";
try
{
loadRounds();
loadToolbox();
}
catch (Exception)
{
error = error + "The Toolbox and/or Rounds menu could not be created\r\n";
}
}
public void loadToolbox()
{
dockT.CloseButton = false;
dockT.ShowHint = DockState.Float;
dockT.Text = "Toolbox";
dockT.BackColor = Color.WhiteSmoke;
dockT.Icon = this.Icon;
dockT.Show(dockPanel);
}
public void loadRounds()
{
if (mapPanel == null)
{
CreateMapPanel().Show(dockPanel, DockState.Document);
}
mapMain.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
//mapMain.BringToFront();
dockR.CloseButton = false;
dockR.ShowHint = DockState.DockRightAutoHide;
dockR.Text = "Rounds Menu";
dockR.BackColor = Color.WhiteSmoke;
dockR.Icon = this.Icon;
dockR.Show(dockPanel);
}
DockContent CreateMapPanel()
{
mapPanel = new clsMWDockPanel();
mapPanel.ShowHint = DockState.Document;
mapPanel.Controls.Add(mapMain);
return mapPanel;
}
Many thanks in advance
leddy
There are several strategies you can use to achieve communication/linkage between objects on different Forms. Note : My reply here is not going to address any issues specifically related to DockPanelSuite, and is not going to consider the difference between the "secondary" forms being "independent" (i.e., they are not "owned" by the MainForm) or being made child Forms of the MainForm. This is a conscious choice made on the basis of believing that what you are asking about is independent of those possible variations in implementation.
the simplest strategy (if tedious for a lot of controls) is to declare Public Properties in your secondary Forms that expose the controls you want to manipulate from your Main Form. For example, let's say Form2 has a button, and you want to handle its click event on your main form :
In Form2 define a property like :
public Button form2Button
{
get { return button1; }
}
Now in the Load event of your Main Form, assuming that's where an instance of Form2 is created, you can subscribe to the Click event of the Button on Form2 :
Form2 myForm2;
Form3 myForm3;
private void Form1_Load(object sender, EventArgs e)
{
myForm2 = new Form2();
myForm2.form2Button.Click += new EventHandler(form2Button_Click);
myForm3 = new Form3();
}
And you can easily imagine that in Form3 you have a TextBox that you have exposed with a Public Property in the same way you exposed the Button on Form2.
So you can implement the MainForm's event handler like this for the Button click on Form2 :
public void form2Button_Click(object sender, EventArgs e)
{
// do something here with the TextBox on Form3
// myForm3.theTextBox.Text =
}
... other strategies ...
in your secondary form, for example, a button press can raise a Public Event which the Main Form (or any other entity to which Form2 is exposed) could subscribe to and then dispatch the appropriate whatever to the appropriate target.
you can abstract message-passing in general at a higher level into a single (perhaps static) class where publishers send messages, and the messages are dispatched to registered listeners.
Finally, the discussion here may be of interest to you :
Using The Controls Of One Form Into Another
best,
Your classes, dockRounds and dockToolbox should expose any properties/events that you want to access. So if you want to hook up to a control's event, route it to a public event.
You can set the access modifier on a control to make it as accessible as you like. The default is "Private" which is why you can't access the controls from the main form.
In Visual Studio, on the Properties tab, there is a Modifiers property which sets the access modifier that is used in the generated designer file.
Set this to "Public" and you be able to access the control from the main form.
I've just used this when I inherited one form from another. By setting the modifier to "Protected" (or "Protected Internal") I was able to access the controls defined in the base class.
Have you tried accessing the Controls property?
var controls = dockRounds.Controls;

Categories

Resources