简体   繁体   中英

c# Make a variable value “” on button click, but only to the dialog box

I have a modeless UI that adds a userID to a list that allows or removes access to parts of a program. When I click the modify button everything works as it should. Suppose I close the dialog and realize "Wait, forgot to do X". When I reopen the dialog box, perform my work and click Modify, the value for adding the userID is still available to the program even though the textbox is blank.

It's happening somewhere in the following code.

public static void checkSame()
{
    int count = 0;
    bool test = false;

    while (linesPerm.Length >= count && tbPermValue != "")
    {
        if (linesPerm.Length >= count)
        {
            test = linesPerm.Contains(tbPermValue);
            count += (linesPerm.Length + 1);

            if (test == true)
            {
               DialogResult dr = MessageBox.Show("The UserID " + tbPermValue + 
               " already exists in the Permissions column. " 
                    + Environment.NewLine + "Would you like to add the UserID" + 
                    tbPermValue + " to the Permissions column anyway?", 
                    "User Already Exists", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);

                switch (dr)
                {
                    case DialogResult.Yes:
                        break;

                    case DialogResult.No:
                        tbPermValue = "";
                        break;
                }
            }
        }
        else
        {
            MessageBox.Show("Do Nothing");
        }
    }
}

If the user selects No on the dialog box, the value of tbPermValue is not available to the program. If the user selects Yes then the value of tbPermValue persists even if the dialog box is closed and reopened. I have tried to clear the textbox value like so.

tbUserName.Text = "";
tbUserName.Clear();

and several other ways. tbUserName value is being cleared from the textbox, but not from the code above. I get the value of tbPermValue like this.

public static void addPerm(System.Windows.Forms.Form targetForm)
{
    foreach (Control C in targetForm.Controls)
    {
        if (C.GetType() == typeof(TextBox))
        {
            if (C.Text != "")
            {
                tbPermValue = C.Text;
            }
        }
    }
}

This is a modeless dialog box owned by it's parent.

Can anyone point me in a direction that would remove access to tbPermValue to the DialogResult portion of the first code box after the button is clicked. I can't lose it completely because tbPermValue is used in other code down the line.

EDIT: Ok. I just tested this and the value is being held in memory. I have a dialog Form1 that has a button that opens dialog StartHere. On StartHere there is a button that opens Permissions. StartHere owns Permissions so that when I close StartHere, Permissions and all other child forms of StartHere will close. These are all modeless dialogs. My variable tbPermValue is being held in memory way back to Form1. The value is not being disposed when I close the dialog StartHere. I'm going to go back and research Garbage Collection at the advice of Eric below. Thank you Eric. I'll delete the question or at least post a new better question once I find out the rules for this process. Thank You.

Edit 2: Here is the code you asked for γηράσκωδ'αείπολλάδιδασκόμε

    private void bModify_Click(object sender, EventArgs e)
    {
        WidgetLogic.addPerm(this);
        WidgetLogic.checkSame();
        WidgetLogic.writePerm(this);
        WidgetLogic.writeAdmin(this);
        WidgetLogic.writeDetailer(this);
        tbUserName.Clear();


    }

As noted above I have tried numerous ways to clear tbUserName to no avail.

I see that you say you have tried setting the following in the "yes" part of your switch statement:

tbUserName.Text = "";
tbUserName.Clear();

But in your "no" part, you don't set tbUserName , but instead you set the variable tbPermValue . From what I can tell, you should also be setting

tbPermValue = "";

in your "yes" part as well to clear that variable, or even just move it out of the switch and have it do that before the dialog closes since you would be setting it in all of the possible switch cases anyways.

Don't use tbPermValue but instead use the textbox directly:

while (linesPerm.Length >= count && tbUserName.Text != "")

EDIT

Change the code in addPerm to this, and you are done :):

public static void addPerm(System.Windows.Forms.Form targetForm)
{
    foreach (Control C in targetForm.Controls)
    {
        if (C.GetType() == typeof(TextBox))
        {
            tbPermValue = C.Text;
        }
    }
}

You don't need the switch (dr) in checkSame()

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