-1

I'm trying to clear my textBox from another form in Visual C# when I click a button on another form, but nothing is working. I have done this is VB.Net with ease but in Visual C# I can't do it. Tell me an easy way to do that in (WinForm). I'm using .Net Framework 4.8.

This is my code which I used in VB.Net. Tell me how to do this in C#.

Note: form1 is a form where my textBox1 is present and form2 is a form where my button is present and I want that when I click that button textBox1 text become empty.

form1.textBox1.Text = ""
  • 2
    Is it web form or win form or what? – Karan Jun 19 '20 at 05:42
  • `form1.textBox1.Text = "";` – Gusman Jun 19 '20 at 05:43
  • try form1.textBox1.Clear(). You need to specify a bit more about what you are trying to do. is it winform? – Karthik Jun 19 '20 at 05:45
  • Is `form1` the name of the form or a variable that you've used when you created the form (i.e. `var form1 = new Form1();`)? – Enigmativity Jun 19 '20 at 05:52
  • https://stackoverflow.com/questions/4822980/how-to-access-a-form-control-for-another-form – Dilshod K Jun 19 '20 at 05:56
  • let me guess you created simply a new `Form1 form1 = new Form1()` in your second form and apply this code that you have posted on the created object ?=! And in your VB application you probably had this form declared globally so that it exists only 1 time. It really sounds for me like you have the wrong instance. – Mong Zhu Jun 19 '20 at 06:13
  • 1
    You are missing a [mcve] of the problem. There is no declaration of `form1`. If I declare an instance of a Form and name it `form1`, and the text box `textBox1` has the correct accesibility, we can do `form1.texBox1.Text = "";` with no problem. – Cleptus Jun 19 '20 at 06:14
  • 1
    you definetely need to provide more information. Post the creation of the object `form1`, post the entire button-click-handler-method, and post a screenshot of the `textBox1` properties that declare the textbox as `public`! – Mong Zhu Jun 19 '20 at 06:14
  • 1
    In VB.Net, you're probably using default Form instances. You don't have those in C#. You'll need a reference of the current `form1` instance. Of course, you cannot create another with `var form1 = new Form1();`: this creates a new instance, distinct from the existing one. – Jimi Jun 19 '20 at 06:48

1 Answers1

1

Normally, I am working with C on embedded system. I am newbie at OOP and C#. I found 2 solutions Idk they are proper way or not but I hope it solve your problem.

Solution 1:

Go to your WinForm and add the code below to produce a reference to call your Form later

public partial class Form1:Form 
{
    public static Form1 form;
    public Form1()
    {
      form = this;
      InitializeComponent();
    }
}

Now, go to your other Form and try this

Form1.form.textBox1.Clear();

Solution 2:

I tried to use call by ref and it worked. If this method not proper, please inform me with reason.

I added new button with click action to Form1.

private void ButtonClear_Click(object sender, EventArgs e)
{
     class.TextClear(ref textBox1);
} 

And I created new class named "class" and added method below.

public static void TextClear(ref TextBox textBox1)
{
    textBox1.Clear();
}
  • 1
    @TusharChaurasia - The first option is a bad idea - never use `static` like this as it will eventually get you into a bind and make your code fragile. The second option is just completely wrong. – Enigmativity Jun 19 '20 at 07:45
  • 1
    @TusharChaurasia - You should respond to the comments under your question. We're trying to help diagnose what you've done so that we can give you a good robust solution. – Enigmativity Jun 19 '20 at 07:46
  • 1
    Actually, I wrote the solutions not only help but also learn it is good or not. So, thank you for comment. But why it is bad idea use static like that? and Second one not completely wrong, I tried it and it totally worked – UglukFromIsengard Jun 19 '20 at 07:49
  • When you work with instances of objects, static methods can have really unwanted outcomes. Remove `ref` from there (`ref TextBox textBox1` altogether). You need to work with instances. A static method is possible, but you need to test what happens if you have more than one instance of the same object around. If you need to interact with an instance of an object, you have pass/inject the current instance and, possibly, use public methods, properties, events that the instance provides to modify its *internals*. – Jimi Jun 19 '20 at 08:03
  • @UglukFromIsengard - Static variables are a way of having multiple parts of your code interact with each other in ways that you don't want. You have no idea what bit of code caused your program to fail. If you pass instance variables then things remain private and secure. Static variables should always be read-only and contain objects that are read-only. – Enigmativity Jun 19 '20 at 10:34
  • @UglukFromIsengard - The second one the `ref` is totally redundant. `class` is a keyword and should have given you an error when compiling. And then you use another `static` method which is redundant. You could have just called `textBox1.Clear()` directly. But on top of all that you are referencing `textBox1` in the wrong form. You just wrote code that has the same problem that the OP is trying to solve. – Enigmativity Jun 19 '20 at 11:15
  • @TusharChaurasia - Please respond to the questions in the comments under your question. – Enigmativity Jun 19 '20 at 11:15
  • Thank everyone but my question is solved. I have found the other which more secure. –  Jul 19 '20 at 04:07