简体   繁体   English

c#单击按钮时将变量值设置为“”,但仅在对话框中

[英]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. 我有一个无模式的UI,它将一个userID添加到一个列表中,该列表允许或删除对程序各部分的访问。 When I click the modify button everything works as it should. 当我单击修改按钮时,一切正常。 Suppose I close the dialog and realize "Wait, forgot to do X". 假设我关闭对话框并意识到“等等,忘了做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. 当我重新打开对话框时,执行我的工作并单击“修改”,即使文本框为空,用于添加用户ID的值仍可用于程序。

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. 如果用户在对话框上选择“否”,则tbPermValue的值不适用于该程序。 If the user selects Yes then the value of tbPermValue persists even if the dialog box is closed and reopened. 如果用户选择“是”,则即使关闭并重新打开对话框, tbPermValue的值tbPermValue保留。 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. tbUserName值将从文本框中清除,但不会从上面的代码中清除。 I get the value of tbPermValue like this. 我得到这样的tbPermValue的值。

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. 任何人都可以向我指出一个方向,该方向将tbPermValue在单击按钮后将对tbPermValue访问权限删除到第一个代码框的DialogResult部分。 I can't lose it completely because tbPermValue is used in other code down the line. 我不能完全丢失它,因为tbPermValue在其他代码中使用了。

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. 我有一个对话框Form1,该对话框具有一个打开对话框StartHere的按钮。 On StartHere there is a button that opens Permissions. 在StartHere上,有一个打开“权限”的按钮。 StartHere owns Permissions so that when I close StartHere, Permissions and all other child forms of StartHere will close. StartHere拥有权限,因此当我关闭StartHere时,权限和StartHere的所有其他子窗体都将关闭。 These are all modeless dialogs. 这些都是无模式对话框。 My variable tbPermValue is being held in memory way back to Form1. 我的变量tbPermValue以内存方式保存回Form1。 The value is not being disposed when I close the dialog StartHere. 当我关闭对话框StartHere时,不会处理该值。 I'm going to go back and research Garbage Collection at the advice of Eric below. 我将在以下Eric的建议下回去研究垃圾收集。 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 γηράσκωδ'αείπολλάδιδασκόμε 编辑2:这是您要求输入γηράσκωδ'αείπολλάδιδασκόμε的代码

    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. 如上所述,我尝试了多种方法来清除tbUserName无济于事。

I see that you say you have tried setting the following in the "yes" part of your switch statement: 我看到您说您已经尝试在switch语句的“是”部分中设置以下内容:

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

But in your "no" part, you don't set tbUserName , but instead you set the variable tbPermValue . 但是在“否”部分,您没有设置tbUserName ,而是设置了变量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: 不要使用tbPermValue ,而是直接使用textbox

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

EDIT 编辑

Change the code in addPerm to this, and you are done :): addPerm的代码更改为此,即可完成:):

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() 您在checkSame()不需要switch (dr) checkSame()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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