简体   繁体   中英

How can I change multiple .text values to “”, through calling a method?

I m trying to make a method and call it everytime I want my textboxes to go blank in my form.

The code I ve tried is listed below

In the main(form) code:

 _BlankSpaces.NineBlankTextboxes( ref txtSup.Text, ref txtSupName.Text, ref txtSupCode.Text, ref txtZenonName.Text, ref txtZenonCode.Text, ref txtInAmount.Text, ref Combo_Mech_El.Text, ref txtDescr.Text, ref txtID.Text);

                    /*   Instead of:
                    txtSup.Text = "";
                    txtSupName.Text = "";
                    txtSupCode.Text = "";
                    txtZenonName.Text = "";
                    txtZenonCode.Text = "";
                    txtInAmount.Text = "";
                    Combo_Mech_El.Text = "";
                    txtDescr.Text = "";
                    txtID.Text = ""; */

                    //And In the Class I call:

namespace WarehouseManagementToolv1.Secondary
{
    public class BlankSpaces
    {

        //public string Blank1, Blank2, Blank3, Blank4, Blank5, Blank6, Blank7, Blank8, Blank9;

        public void NineBlankTextboxes(ref string blank1, ref string blank2, ref string blank3, ref string blank4,
            ref string blank5, ref string blank6, ref string blank7, ref string blank8, ref string blank9)
        {
            blank1 = "";
            blank2 = "";
            blank3 = "";
            blank4 = "";
            blank5 = "";
            blank6 = "";
            blank7 = "";
            blank8 = "";
            blank9 = "";

        }

    }
}

When I run it the massage I get is:

A property or indexer may not be passed as an out or ref parameter

Property "Text" access returns temporary value. "ref" argument must be an assignable variable, field or an array element.

this is only possible with jquery or css selectors (web app), for C# (desktop app) this is not possible, you should clear your text using yourtext.Text = string.Empty, or by binding a property and set it to empty each time you want to.

You could try something like

private void ClearTextBoxes(params TextBox[] list)
{
    foreach(TextBox tb in list)
    {
        tb.Text = "";
    }
}

This would be used as

ClearTextBoxes(txtSup, txtSupName, txtSupCode); // TODO : Add any other textboxes

This sidesteps the issue of trying to send a reference copy of a property and sends the object itself to allow it to be cleared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM