Home > other >  How to change a button enabled state in form1 depending on flag state in another form?
How to change a button enabled state in form1 depending on flag state in another form?

Time:06-29

In Form1 :

private void recordStripMenuItem_Click(object sender, EventArgs e)
        {
            recordToggle = !recordToggle;

            if (recordToggle)
            {
                recordStripMenuItem.Text = "Stop";
                Icon = iconRed;
                TextInfo("Recording");
                record.Start();
            }
            else
            {
                recordStripMenuItem.Text = "Record";
                Icon = iconGreen;
                TextInfo("Waiting");
                record.Stop();
            }
        }

And i want to enabled false/true the button in form1 depending on the flag state in this form :

using Capture_Screen.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test
{
    public partial class SettingsForm : Form
    {
        public static bool isSettingsEmpty = false;

        private FFmpeg_Capture ffmpegCapture;

        public SettingsForm()
        {
            InitializeComponent();

            ffmpegCapture = new FFmpeg_Capture();
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {

        }

        private void btnWorkingFolder_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            folderBrowserDialog1.Description = "Browse for working folder";
            if (result == DialogResult.OK)
            {
                textBoxWorkingFolder.Text = folderBrowserDialog1.SelectedPath;
                ffmpegCapture.workingDirectory = folderBrowserDialog1.SelectedPath;
            }
        }

        private void btnFfmpegFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog
            {
                InitialDirectory = @"C:\",
                Title = "Browse For Ffmpeg exe File",

                CheckFileExists = true,
                CheckPathExists = true,

                DefaultExt = "exe",
                Filter = "ffmpeg exe file (ffmpeg.exe)|ffmpeg.exe",
                FilterIndex = 2,
                RestoreDirectory = true,

                ReadOnlyChecked = true,
                ShowReadOnly = true
            };

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBoxFfmpegFile.Text = openFileDialog1.FileName;
                ffmpegCapture.outputDirectory = openFileDialog1.FileName;
            }
        }

        private void btnConrimArguments_Click(object sender, EventArgs e)
        {
            ffmpegCapture.arguments = textBoxArguments.Text;
            btnConrimArguments.Enabled = false;
        }

        private void textBoxArguments_TextChanged(object sender, EventArgs e)
        {
            if(textBoxArguments.Text == "")
            {
                isSettingsEmpty = true;
            }

            if(textBoxArguments.Text != "" && textBoxWorkingFolder.Text != "" 
                && textBoxFfmpegFile.Text != "")
            {
                isSettingsEmpty = false;
            }

            btnConrimArguments.Enabled = true;
        }

        private void textBoxWorkingFolder_TextChanged(object sender, EventArgs e)
        {
            if(textBoxWorkingFolder.Text == "")
            {
                isSettingsEmpty = true;
            }

            if (textBoxArguments.Text != "" && textBoxWorkingFolder.Text != ""
                && textBoxFfmpegFile.Text != "")
            {
                isSettingsEmpty = false;
            }
        }

        private void textBoxFfmpegFile_TextChanged(object sender, EventArgs e)
        {
            if(textBoxFfmpegFile.Text == "")
            {
                isSettingsEmpty= true;
            }

            if (textBoxArguments.Text != "" && textBoxWorkingFolder.Text != ""
                && textBoxFfmpegFile.Text != "")
            {
                isSettingsEmpty = false;
            }
        }
    }
}

I'm using a public static variable flag isSettingsEmpty and check if one of the textBoxes is empty make the flag true and if all the textBoxes filled make the flag flase.

but how do i apply the flag isSettingsEmpty state changes to the form1 and change the record button state enabled false/true in real time ?

I want that if one of the textBoxes is empty enabled false the record button.

CodePudding user response:

There are many ways of doing that, one of them is using events.

create custom EventArgs class:

public class RecordingEventArgs : EventArgs
{
     public bool Enabled {get; set;}
}

Form1:

public partial class Form1 : Form
{

    // you need to subscribe your Form1 to the SettingsForm event
    // supposing you are creating and calling it from Form1
    public void SettingsShowButton_Click(object sender, EventArgs e)
    {
         var settingsForm = new SettingsForm();
         settingsForm.Notify  = NotifiactionHandler;
         settingsForm.Show();
    }

    // handling notification event from SettingsForm
    public void NotifiactionHandler(object sender, RecordingEventArgs e)
    {
        recordButton.Enabled = e.Enabled;
    }
}

SettingsForm:

public event EventHandler Notify;

private void textBoxArguments_TextChanged(object sender, EventArgs e)
{
    var eventArgs = new RecordingEventArgs();
    if(textBoxArguments.Text == "")
    {
       isSettingsEmpty = true;
       eventArgs.Enabled = true;
       Notify?.Invoke(this, eventArgs);      
    }
    
    // ...
} 

              
  • Related