I want to disable a button from clicking - c#

Bassicly im creating a program that allows the user to enter values and if the values exceed a certain amount then disable a button that was on a different form. But im unsure how to access its buttons control. I thought it would be something like this? Thanx
if(value>120)
{
Form3 form3 = new Form3();
Button.Disable();
this.close();
}

Your request is to disable a button that was on another form - from reading that I assume the form already exists. By creating a new instance:
Form3 form3 = new Form3();
You're creating a new instance of Form3 so you'll never disable a button on a form that was already visible.
You'll have to make the current form aware of the instance of Form3 to be able to change anything there. Here are a few ways to make them interact:
Store these values in a separate object and make your forms react on changes through events for example
Provide a reference to the instance of Form3 upon creating or Show()ing "this" form
Keep a reference to each created form in a static class
Also keep in mind having multiple related forms active at the same time may confuse your end-user.

You need a reference to an instance of Form3. You are creating a new instance of Form3 which is probably not what you wanted. Then your Form3 needs to expose the button you are interested in as a public property so that you can access it from outside of the class. Then you should be able to set the Disabled property to true.

You can disable the button like this:
otherForm.Button.Enabled = false;
To be able to disable this button from another context (form), you need to declare it as public. You can do this as follows:
selecting the button in design view
in the properties window set Modifiers to Public
Then you can show the form with the disabled button, like so:
var newForm = new Form3();
newForm.Button.Enabled = false;
newForm.Show();

I guess you have to do something like this.
update
if(value>120)
{
Form3 form3 = new Form3();
form3.Button.Enabled = false;
this.close();
}
update

Related

How to update data in form2 without create new instance of form2

it's just a simple stuff that i wanna test
i have two forms (Form1 and Form2)
In Form1 have:
textbox and button
In Form2 have only TextBox that use for displayData when I click button in Form1
the problems is when i clicked buttton the data send to Form2 but it's created a new form everytime I clicked it. Is there any way to updateData in Form2 without create a new one.
Here the code:
In Form1:
private void sendBt1_Click(object sender, EventArgs e) //sendBt1 is button in Form1
{
Form2 f2 = new Form2(typeBox1.Text);//typeBox1 is TextBox in Form1 use to type
f2.Show();
}
In Form2:
public Form2(string data)
{
InitializeComponent();`enter code here`
this.ds2.Text = data; //ds2 is textBox in Form2 that i wanna display
}
Can anyone help? Im kinda stuck rn :)
As commented already, there are numerous ways to get this done. The current code, as you describe, is creating multiple Form2 objects since the code is creating a new Form2 each time the button is clicked on Form1.
Form2 f2 = new Form2(typeBox1.Text);
In addition, you want the “TEXT” in typeBox1 TextBox to be passed to Form2's TextBox ds2. The current code does this however, if the text in typeBox1 TextBox in Form1 changes, the TextBox in Form2 (ds2) is NOT updated. Again, there are numerous ways to fix this, however, I suggest to create a DataBinding for Form2's TextBox such that IF the typeBox1 TextBox changes in Form1, then the TextBox ds2 in Form2 will automatically be updated at the same time and the user does not have to do anything.
The example below uses this DataBinding approach and works as described….
I may be missing something here, however, in my tests, one approach is to simply “check” to see if Form2 is already open. If Form2 IS OPEN… then, we do not have to do anything since our code sets up the data binding for the text box in Form2’s constructor. Since Form2 IS OPEN it may be minimized, therefore, if the user clicks the button on Form1 and Form2 IS OPEN then we will set the forms state back to normal which will basically restore the form. Also the code will call the forms Show() method in case the Form has somehow been hidden.
IF Form2 IS NOT OPEN, then obviously we need to create it as your current code does, however, instead of passing in JUST the TEXT from the TextBox, I suggest you send over the WHOLE TextBox from From1 to Form2. This way, we can set up the DataBinding for Form2’s TextBox and Bind Form2’s TextBox to the passed in TextBox from Form1. This will automatically update Form2’s TextBox ds2 when Form1’s TextBox changes.
Therefore I suggest you change the code in Form1’s sendBt1_Click event to something like…
private void sendBt1_Click_Click(object sender, EventArgs e) {
var form = Application.OpenForms["Form2"];
if (form == null) {
form = new Form2(typeBox1);
}
form.Show(); // <- in case the form is somehow hidden
form.WindowState = FormWindowState.Normal; // <- in case the form is minimized
}
NOTE: in the above code we are passing the WHOLE TextBox to From2, not just the text in the TextBox.
Then Form2’s constructor may look something like….
public Form2(TextBox f1TextBox) {
InitializeComponent();
ds2.DataBindings.Add("Text", f1TextBox, "Text");
}
This should be all you need. When the text is changed in Form1’s text box, the text box in Form2 is automatically updated.

C# How to go back from a window from ,where 2 different window forms lead to

I hope the title is not very confusing. I'll try to explain it as good as i can.
I am building a virtual museum with c# at visual studio. At form1 the user can choose if he will continue unregisterd ( leads to form2) or if he will continue as registered user( leads to form3). Some options are the same for the registered and the unregisterd user and some are not. So lets say that at both form2 and form3 i have a linklabel that leads to form4 with the code
this.close();
Form4 myForm4 = new Form4;
myForm4.ShowDialog();
So far so good. At form4 i put a "previous" button.
How can i make it to go back to the form that led me to form4?
if i click the linklabel from form3 and push the previous button , i want to go to form3 and if i click from form2 i want to return to form2.
One solution is to create similar form and go back without a problem. But i have 6 same options which means 12 new forms.
Can anyone help me how to do it?
Just don't close the form; hide it instead. Execution will return to it after ShowDialog(), where you can unhide it again:
this.Hide();
Form4 myForm4 = new Form4;
myForm4.ShowDialog();
this.Show();
You could actually trap the result of ShowDialog() and do something different besides just unhiding the form.
A more robust approach would be to use one form and change what is currently displayed in it by using either UserControls or borderless forms. Then you could have a List to track the order and allow easy traversal for back/forward buttons.

Refresh an open form from another open form

I want to refresh an already opened form (form1) from another opened form's (form2) button_click(). In form1 I display the data saved by form2 and when form1 is already opened I want it to refresh if new data is saved on form2 in order to display it.
The problem is that I've tried iterating through `Application.Openforms`, but it turns out that it is read-only and I cannot access the form once found and I don't know how to access *form1* from *form2*, since I can't simply find it.
How can I access *form1* from *form2*?
Edit:
Form1 is actually opened from Form2.
The problem with Application.Openforms is that , as I've stated, a read-only list of forms already opened and I cant actually access the forms through it. They simply don't have the methods for it, I sugest you try using Application.OpenForms and look it up if you don't know how it works.
Also it's pointless to show what I've already tried because it includes Application.OpenForms, but for the sake of information:
FormCollection of = Application.OpenForms;
foreach (var f in of)
{
if (f.GetType().ToString() == "Kontrl_Doc.Visualizar")
{
f.Refresh();
}
}
When I click the button (button_click()) in Form2 it checks if Form1 is open or not. If Form1 isn't open it opens one and if it is than I'd like it to refresh it. Simultaneously, it closes Form2 and opens Form2 again, in order to reset is fields.
What I wan to do is, if the form1 is already opened , it form2 should tell it to refresh the already opened window with the form 1.
"Form1 is actually opened from Form2" - If this is the case, then just call Refresh using the form variable that you have in Form2. If necessary, make that a private field in the Form2 class or store it in an array for later use.
For example:
(Somewhere in Form2)
Form1 form1 = new Form1();
form1.Show();
(Inside the button click in Form2)
form1.Refresh();
you can use events. In form2 you place this code
public event Action ReloadForm1;
//on the place where you will reload form1
ReloadForm1();
and in form1 if you have opening form2:
form2.ReloadForm1 += Reload;
//outside method
void Reload()
{
this.Reload();
}
Create a void method in form1 and add the components u want to refresh maybe you want to reload a dropdown from db
public void Refresh()
{
...
}
then open a dialog of form2
catch the dialog result

New form disappear after creation

I'm facing a problem with form show. I have a main form where I have my GUI and I choose an option that creates an instance of a form. For example in my main form I have:
Form2 f2 = new Form2();
f2.Show();
The problem is that the form shows for about 1-2 secs and then goes behind the main form.
I tried some instructions in the main form below f2.Show() command like
f2.BringtoFront();
this.SendtoBack();
Also I added commands to the new form (Form2) load method:
this.BringtoFront();
this.Activate();
this.Focus();
Nothing of the above commands seems to be a solution for this. Only when I use f2.ShowDialog(); instruction in my main form but I don't want to do that because I need immediate access to both of these forms at the same time.
Any help? Thanks
If you don't want your second form to never go behind your main form then pass the owner in the overload of Show method that accepts the owner parameter
Form2 f2 = new Form2();
f2.Show(this); // Assuming this code runs inside the main form
If you remove or change it to comment this.SendtoBack(); :
f2.BringtoFront();
//this.SendtoBack();
it'll be OK!

c# gui - navigate between forms

I'm programming c# GUI and I have 2 forms.
Form1 is my main form, and it has a button to open form2.
When the button in form1 is being clicked, I hide form1, create a new object of form2 and show form2.
I have a back button in form2. I want the behavior of this button to close form2, and show again the hidden form1.
How can I do it?
Have your form1 subscribe to the VisibleChange event of form2 and act accordingly. It will have to "remember" whether form2 is visible or hidden (or query it directly).
The alternatives are:
Your form2 will need a reference to form1.
This can be done in a number of ways - passing it in a constructor parameter, adding a property and assigning form1 to it.
Either of these ways will tightly couple these forms to each other (bad thing).
Have you tried
Form1.Visible = true;
Form1.Activate();
And then in the Form1 VisibleChanged Event you simply write
Form2.Close();

Categories

Resources