-3

I'm attempting to disable Form1 in both VB and C#.

Example from the Created child forms, button click or w/e

  • VB.NET

    Me.Focus()
    Me.Enabled = True
    Form1.Enabled = False
    
  • C#

      //This is my attempt at a functional translation
      //if (_form1 == null)
      //        _form1 = new Form1();
      //    _form1.Enabled = False;
    
     //This is a literal translation
     // This Code will not disable form1
     this.Focus();
     this.Enabled = true;
     Form1.Enabled = false;
    

The C# code will not disable Form1 from form2 the way the VB.NET code does.

From these C# references I gathered the above commented code would work (I knew how to use it in VB.net) : Ref1,Ref2,Ref3

What I want to know is

  • Why is it so different, why can't I just access the Form1 (from Form2) like I can in VB?
  • What other steps are needed to have full access of Form1?
Community
  • 1
  • 1
Don Thomas Boyle
  • 3,055
  • 3
  • 32
  • 54
  • 6
    The difference is that your code does different things. If you want the same behaviour then make the code do the same thing for both versions. – David Heffernan Sep 19 '13 at 14:18
  • How would one do that typing `this.Focus();` `this.Enabled = true;` `Form1.Enabled = false;` won't access the form – Don Thomas Boyle Sep 19 '13 at 14:19
  • 1
    you not doing the things right, thats why you think C# has less control than VB. – Dilshod Sep 19 '13 at 14:19
  • Are you sure you don't just want something like `form2.ShowDialog();`? – musefan Sep 19 '13 at 14:20
  • NO you guys are misunderstanding or I'm saying it wrong then ... the Form shows fine, i cannot however from the "Showed"(StartedForm) Disable the First form. In C# anyways. – Don Thomas Boyle Sep 19 '13 at 14:21
  • whene you create the childForm set the parent field of the child to the first form. in this way you can access to first form inside the parent property of the second form – Emanuele Sep 19 '13 at 14:22
  • @DonThomasBoyle: No, you misunderstand. You have two completely different pieces of code, that is why they don't do the same thing. – musefan Sep 19 '13 at 14:22
  • What do you mean by disabling the form1? Are you saying you want to hide the form or just can't do anything until form2 is closed? – Dilshod Sep 19 '13 at 14:23
  • No i want to disable `Form1` from `Form2`. UPdated above code to be "Equivilent". – Don Thomas Boyle Sep 19 '13 at 14:25
  • This question is a train wreck. You've completely changed it and now have two pieces of code that do the exact same thing. – David Heffernan Sep 19 '13 at 18:02
  • @DavidHeffernan serious? get downvoted for code not being the same now being told off for changing it to be the same ... really? not like im asking a real hard question here. – Don Thomas Boyle Sep 19 '13 at 18:54
  • Well, the question is different now. Completely. And that invalidated your first answer which now looks daft. But after the edit where's the question? The two versions are now identical. So what exactly do you want us to tell you? – David Heffernan Sep 19 '13 at 19:01
  • @DavidHeffernan Actually, after his edit the question makes a lot more sense. It's still not a great question, but the code snippets *don't* do the same thing. The underlying issue surrounding the question is that VB has a "default form instance" feature that doesn't exist in C# (thank god). Therefore writing code that looks the same will work in VB but not in C#. – Servy Sep 19 '13 at 19:21
  • @Servy Well I'm still confused! Sounds like one needs to know about this "feature" to get a grip of this Q. – David Heffernan Sep 19 '13 at 19:27
  • @DavidHeffernan You're welcome to do some research into the feature if you're interested. You can just google the term along with VB to get some basic information on the subject. It's not actually that complex. – Servy Sep 19 '13 at 19:30
  • @Servy I'll read your answer when it comes ;-) The question is phrased so badly though. Perhaps you should fix the question, as well as writing the answer! – David Heffernan Sep 19 '13 at 19:37
  • 2
    @DavidHeffernan I cleaned it up a bit. After all of the OP's edits, I didn't see much beyond basic grammar/formatting to fix. The underlying question is reasonably sensible. – Servy Sep 19 '13 at 19:46

1 Answers1

3

Your VB code is leveraging the feature of a default form instance.

(In VB.NET) Behind the scenes each of the Form classes is given a static variable of the same type as the form itself, and when a new form is created it sets itself as that static variable's value. That variable is also the default static property of that type (another feature that VB has that C# does not) which is why you can use the type name to directly access this property. You can see this related question for a bit of history as to why this feature still exists in VB, despite the fact that it doesn't strictly make sense any more.

There are ways of mostly replicating this behavior in C#, creating the static property yourself and setting/clearing it manually, but I strongly discourage you from doing so. This general concept is rather poor practice, which is why this feature was intentionally omitted in C#.

There isn't conceptually just a single instance of forms, even if some of the forms that you use my happen to only exist once.

A more preferred practice would be to create an event in your second form that the first form subscribes to. (Or uses an existing event, if appropriate). In this case you want to have the parent form have Enabled set to false whenever the child form sets it's Enabled to true, you can do this through the use of an existing event.

When you create your child form, from within the parent, just use the EnableChanged event to change the parent's property based on the child's actions:

Form2 child = new Form2();
child.EnabledChanged += (s, args) => Enabled = !child.Enabled;

Then the child form never needs to know anything about it's parent. This is fantastic from a design perspective. You reduce coupling, and improve modularity. The developer of the child form doesn't need to know anything about the parent form or what it needs to do. The child form just needs to know how anyone might want to use it and provide the needed functionality.

Community
  • 1
  • 1
Servy
  • 202,030
  • 26
  • 332
  • 449