简体   繁体   English

如何实时检查是否将数据从其中一个类输入到texbox中?

[英]How to check in real time if data was enteder from one of the classes into a texbox?

My question comes from a problem which I have right now. 我的问题来自我现在遇到的一个问题。 I have MainWindow, AuthenticateWindow, and AddEntryWindow which all are WinForms. 我有MainWindow,AuthenticateWindow和AddEntryWindow,它们都是WinForms。 In main window I have possibility to Authenticate and Add Entry into my main windows textbox. 在主窗口中,我可以进行身份​​验证并将条目添加到主窗口文本框中。 They can not add an entry until they authenticate (no problem with this). 在他们进行身份验证之前,他们无法添加条目(此操作没有问题)。 I need to add an entry to the text box which will update my main windows textbox. 我需要在文本框中添加一个条目,以更新我的主Windows文本框。 The problem if, how can I check if entry was added to my textbox? 问题是,如何检查条目是否已添加到我的文本框中?

I am trying to have a Save option from menu strip. 我正在尝试从菜单栏中选择“保存”选项。 I am getting an error whenever I am trying to save an empty file. 每当我尝试保存一个空文件时,都会出现错误。 How could I authenticate the saving process by Save button by having it first disabled, and enabled after entry was added? 如何通过先禁用保存按钮并在添加条目后启用保存按钮来验证保存过程?

I could always verify if if textbox had an entry but I want to have button disabled first, and enabled after entry was added. 我总是可以验证文本框是否有条目,但是我想先禁用按钮,然后在添加条目后启用它。 I do not have a privilege to do so as of right now. 截至目前,我没有特权这样做。

Please ask questions if I am not clear enough. 如果我不够清楚,请提出问题。

private void tsmiSave_Click(object sender, EventArgs e)
{
    // Open sfdSaveToLocation which let us choose the
    // location where we want to save the file.
    if (txtDisplay.Text != string.Empty)
    {
        sfdSaveToLocation.ShowDialog();
    }
}

MainWindow.cs MainWindow.cs

using System;
using System.IO;
using System.Windows.Forms;

namespace Store_Passwords_and_Serial_Codes
{
    public partial class MainWindow : Form
    {
        private AuthenticateUser storedAuth;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void MainWindow_Load(object sender, EventArgs e)
        {
            // Prohibit editing.
            txtDisplay.Enabled = false;
        }

        public string ChangeTextBox
        {
            get
            {
                return this.txtDisplay.Text;
            }
            set
            {
                this.txtDisplay.Text = value;
            }
        }

        private void tsmiAuthenticate_Click(object sender, EventArgs e)
        {
            AuthenticationWindow authWindow = new AuthenticationWindow();
            authWindow.ShowDialog();
            storedAuth = authWindow.Result;
        }

        private void tsmiAddEntry_Click(object sender, EventArgs e)
        {
            if (storedAuth == null)
            {
                DialogResult result = MessageBox.Show
                    ("You must log in before you add an entry." 
                    + Environment.NewLine + "You want to authenticate?",
                    "Information", MessageBoxButtons.YesNo, 
                    MessageBoxIcon.Information);

                if (result == DialogResult.Yes)
                {
                    AuthenticationWindow authWindow = 
                        new AuthenticationWindow();
                    authWindow.ShowDialog();
                    storedAuth = authWindow.Result;

                    AddEntryWindow addWindow = new AddEntryWindow
                        (this, storedAuth.UserName, storedAuth.Password);
                    addWindow.ShowDialog();
                }
            }
            else
            {
                AddEntryWindow addWindow = new AddEntryWindow
                    (this, storedAuth.UserName, storedAuth.Password);
                addWindow.ShowDialog();
            }
        }

        private void tsmiClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsmiSave_Click(object sender, EventArgs e)
        {
            // Open sfdSaveToLocation which let us choose the
            // location where we want to save the file.
            sfdSaveToLocation.ShowDialog();
        }

        private void sfdSaveToLocation_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
            string theFileName = sfdSaveToLocation.FileName;

            EncryptDecrypt en = new EncryptDecrypt();

            string encrypted = en.Encrypt(txtDisplay.Text,
                storedAuth.UserName, storedAuth.Password);

            MessageBox.Show(encrypted);

            File.WriteAllText(theFileName, encrypted);
        }
    }
}

AddEntryWindow.cs AddEntryWindow.cs

using System;
using System.Windows.Forms;
// Needed to be used with StringBuilder
using System.Text;
// Needed to be used with ArrayList.
using System.Collections;

namespace Store_Passwords_and_Serial_Codes
{
    public partial class AddEntryWindow : Form
    {
        string user, pass;

        // Initializind ArrayList to store all data needed to be added or retrived.
        private ArrayList addedEntry = new ArrayList();

        // Initializing MainWindow form.
        MainWindow mainWindow;

        // Default constructor to initialize the form.
        public AddEntryWindow()
        {
            InitializeComponent();
        }

        public AddEntryWindow(MainWindow viaParameter, string user, string pass)
            : this()
        {
            mainWindow = viaParameter;
            this.user = user;
            this.pass = pass;
        }

        private void AddEntryWindow_Load(object sender, EventArgs e)
        { }

        private void btnAddEntry_Click(object sender, EventArgs e)
        {
            // Making sure that type is selected.
            if (cmbType.SelectedIndex == -1)
            {
                MessageBox.Show("Please select entry type!", "Error!", 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            // Each field must be filled for specified type.
            // Here we are checking if all fields were filled.
            else if ((cmbType.SelectedIndex == 0 && (txtUserName.Text == string.Empty || txtPassword.Text == string.Empty)) ||
                (cmbType.SelectedIndex == 1 && (txtURL.Text == string.Empty || txtPassword.Text == string.Empty)) ||
                (cmbType.SelectedIndex == 2 && (txtSoftwareName.Text == string.Empty || txtSerialCode.Text == string.Empty)))
            {
                MessageBox.Show("Please fill all the fields!", "Error!", 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                int totalEntries = 0;

                if(cmbType.SelectedIndex == 0)
                    addedEntry.Add(new AddPC(cmbType.Text, 
                        txtUserName.Text, txtPassword.Text));

                else if(cmbType.SelectedIndex == 1)
                    addedEntry.Add(new AddWebSite(cmbType.Text, 
                        txtUserName.Text, txtPassword.Text, txtURL.Text));

                else if(cmbType.SelectedIndex == 2)
                    addedEntry.Add(new AddSerialCode(cmbType.Text, 
                        txtSoftwareName.Text, txtSerialCode.Text));

                StringBuilder stringBuilder = new StringBuilder();

                foreach (var list in addedEntry)
                {
                    if (list is AddPC)
                    {
                        totalEntries++;
                        AddPC tmp = (AddPC)list;
                        stringBuilder.Append(tmp.ToString());
                    }
                    else if (list is AddWebSite)
                    {
                        totalEntries++;
                        AddWebSite tmp = (AddWebSite)list;
                        stringBuilder.Append(tmp.ToString());
                    }
                    else if (list is AddSerialCode)
                    {
                        totalEntries++;
                        AddSerialCode tmp = (AddSerialCode)list;
                        stringBuilder.Append(tmp.ToString());
                    }
                }

                mainWindow.ChangeTextBox = stringBuilder.ToString();

                mainWindow.tsslStatus.Text = "A total of " + totalEntries + " entries added.";

                // Clearing all fields.
                ClearFields();
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearFields();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            // Closing the Add Entry Window form.
            this.Close();
        }

        private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Deciding which data must be entered depending on
            // what type is selected from combo box.

            // PC
            if (cmbType.SelectedIndex == 0)
            {}
            // Web Site
            else if (cmbType.SelectedIndex == 1)
            {}
            // Serial Code
            else if (cmbType.SelectedIndex == 2)
            {}
        }

        private void ClearFields()
        {
            // Clearing all fields to the default state.
        }
    }
}

Regards. 问候。

It sounds like you probably just want to subscribe to the TextChanged event, which will be fired whenever the text in the textbox changes. 听起来您可能只想预订TextChanged事件,该事件将在文本框中的文本更改时触发。

I can't say I really followed everything that you're doing, but I think you should be fine to just enable or disable your Save button within that event handler. 我不能说我真的跟着你做的一切,但我认为你应该罚款只是启用或事件处理程序中禁用保存按钮。

EDIT: It's not really clear where all your different components live, but you want something like: 编辑:尚不清楚所有不同组件都位于何处,但您需要以下内容:

// Put this after the InitializeComponent() call in the constructor.
txtDisplay.TextChanged += HandleTextBoxTextChanged;

...

private void HandleTextBoxTextChanged(object sender, EventArgs e)
{
    bool gotText = txtDisplay.Text.Length > 0;
    menuSaveButton.Enabled = gotText;
}

I'd also strongly advise you not to use ArrayList but to use the generic List<T> type. 我也强烈建议您不要使用ArrayList而要使用通用的List<T>类型。 The non-generic collections should almost never be used in new code. 非泛型集合几乎永远不应在新代码中使用。

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

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