Click here to Skip to main content
15,914,010 members
Home / Discussions / C#
   

C#

 
GeneralRe: c# array Pin
Pete O'Hanlon16-May-18 2:18
mvePete O'Hanlon16-May-18 2:18 
GeneralRe: c# array Pin
Eddy Vluggen16-May-18 1:10
professionalEddy Vluggen16-May-18 1:10 
GeneralRe: c# array Pin
Dave Kreskowiak16-May-18 2:19
mveDave Kreskowiak16-May-18 2:19 
GeneralRe: c# array Pin
Kevin Marois16-May-18 5:40
professionalKevin Marois16-May-18 5:40 
GeneralRe: c# array Pin
Rob Philpott17-May-18 6:18
Rob Philpott17-May-18 6:18 
QuestionSolution Explorer > FOLDER > Text Files Pin
Member 1374647813-May-18 23:46
Member 1374647813-May-18 23:46 
AnswerRe: Solution Explorer > FOLDER > Text Files Pin
OriginalGriff14-May-18 0:15
mveOriginalGriff14-May-18 0:15 
QuestionGetting error Cannot access a disposed object. Object name: 'TreeView' Pin
ranimiyer1013-May-18 23:18
ranimiyer1013-May-18 23:18 
Hi,

I am trying to load a treeview with the list of directories present in the folder location asynchronously in a wizard type form.
The same code works as a separate standalone form.
The below is the code for the wizard form:
C#
public partial class DLWizardPage3 : WizardFormLib.WizardPage
{
    frmAlertForm alert;
    public delegate void Add(TreeView tv, string value);

    public DLWizardPage3(WizardFormBase parent)
                : base(parent)
    {
        InitPage();
    }
    public DLWizardPage3(WizardFormBase parent, WizardPageType pageType)
                : base(parent, pageType)
    {
        InitPage();
    }

    //--------------------------------------------------------------------------------
    /// <summary>
    /// This method serves as a common constructor initialization location,
    /// and serves mainly to set the desired size of the container panel in
    /// the wizard form (see WizardFormBase for more info).  I didn't want
    /// to do this here but it was the only way I could get the form to
    /// resize itself appropriately - it needed to size itself according
    /// to the size of the largest wizard page.
    /// </summary>
    public void InitPage()
    {
        InitializeComponent();
        base.Size = this.Size;
        this.ParentWizardForm.DiscoverPagePanelSize(this.Size);
    }
    public override bool SaveData()
    {
        return true;
    }

    public override bool ValidateData()
    {
        return true;
    }

    private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
    {
        _logger.DebugFormat(LogMessageFormatString, "DLWizardPage3::backgroundWorker2_DoWork", "Started loading Directories for Metadata collected...");
        BackgroundWorker worker = sender as BackgroundWorker;
        Message = "Loading Data... Please Wait...";
        worker.ReportProgress(0);
        for (int i = 0; i < 10; i++)
        {
            // Perform a time consuming operation and report progress.
            Message = "Loading Data... Please Wait...";
            tvDir.Invoke(new Add(AddRootNode), new object[] { tvDir, CommonData.ProjectPath });
            worker.ReportProgress(i * 10);
        }
    }

    private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        alert.Message = Message + e.ProgressPercentage.ToString() + "%";
        alert.ProgressValue = e.ProgressPercentage;
    }

    private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        bool isProcessCompleted = false;
        if (e.Cancelled == true)
        {
            lblResult.Text = "";
            isProcessCompleted = false;
        }
        else if (e.Error != null)
        {
            lblResult.Text = "Error: " + e.Error.Message;
            isProcessCompleted = false;
            _logger.ErrorFormat(LogExceptionNameMessageFormatString, "DLWizardPage3::backgroundWorker2_RunWorkerCompleted", e.Error.Message);
        }
        else
        {
            isProcessCompleted = true;
        }
        // Close the AlertForm
        alert.Close();
        if (isProcessCompleted)
        {
            _logger.DebugFormat(LogMessageFormatString, "DLWizardPage3::backgroundWorker2_RunWorkerCompleted", "Completed loading folders for Metadata collected...");
            //Getting the user input for metadata collection
            DialogResult diagRes = MessageBox.Show("Has the metadata been collected using Omni Tool?", "Data Load", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
            if (diagRes == DialogResult.Yes)
            {
                CommonData.IsReportGeneratedThroughSOMA = true;
            }
            else
            {
                CommonData.IsReportGeneratedThroughSOMA = false;
            }
        }
    }

    private void DLWizardPage3_Load(object sender, EventArgs e)
    {
        //ListDirectory(tvSchemas, CommonData.ProjectPath);
        if (backgroundWorker2.IsBusy != true)
        {
            // create a new instance of the alert form
            alert = new frmAlertForm(false, ProgressBarStyle.Marquee, 30);
            // event handler for the Cancel button in AlertForm
            alert.Canceled += new EventHandler<EventArgs>(btnCancel_Click);
            alert.Show();
            // Start the asynchronous operation.
            backgroundWorker2.RunWorkerAsync();
        }
    }

    /// <summary>
    /// Populates the treeview control on the UI with the Directory data
    /// </summary>
    private void ListDirectory(TreeView treeView, string path)
    {
        treeView.Nodes.Clear();
        var rootDirectoryInfo = new DirectoryInfo(path);
        CommonData.ProjectName = rootDirectoryInfo.Name;
        treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
    }

    private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
    {
        var directoryNode = new TreeNode(directoryInfo.Name);
        directoryNode.Expand();
        //var files = directoryInfo.GetDirectories().Where(dir => Regex.IsMatch(dir.Name, "^[A-Za-z]+"));
        foreach (var directory in directoryInfo.GetDirectories())
        {
            if ((!Regex.IsMatch(directory.Name, @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$")) && (directory.Name.ToString().ToUpper() != "CSS") && (directory.Name.ToString().ToUpper() != "IMG") && (directory.Name.ToString().ToUpper() != "JS") && (directory.Name.ToString().ToUpper() != "DMV") && (directory.Name.ToString().ToUpper() != "MESSAGES"))
            {
                directoryNode.Nodes.Add(CreateDirectoryNode(directory));
            }
        }
        return directoryNode;
    }

    public void AddRootNode(TreeView treeView, string path)
    {
        treeView.Nodes.Clear();
        var rootDirectoryInfo = new DirectoryInfo(path);
        //CommonData.ProjectName = rootDirectoryInfo.Name;
        treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
    }
}


Below is the code AlertForm that shows a progress bar.
C#
public partial class frmAlertForm : Form
    {
        public string Message { set { lblMessage.Text = value; } }
        public int ProgressValue { set { progressBar1.Value = value; } }


        public frmAlertForm(bool isCancelVisible)
        {
            InitializeComponent();
            btnCancel.Visible = isCancelVisible;           
        }

        public frmAlertForm(bool isCancelVisible, ProgressBarStyle style, int animationSpeed)
        {
            InitializeComponent();
            btnCancel.Visible = isCancelVisible;
            progressBar1.Style = style;
            progressBar1.MarqueeAnimationSpeed = animationSpeed;      
        }

        public event EventHandler<EventArgs> Canceled;

        private void btnCancel_Click(object sender, EventArgs e)
        {
            EventHandler<EventArgs> ea = Canceled;
            if (ea != null)
                ea(this, e);
        }

        private void frmAlertForm_Load(object sender, EventArgs e)
        {

        }
    }


I am using backgroundWorker to make the asynchronous loading of treeview.

When ever, I add the tree node, I am getting exception "
Cannot access a disposed object. Object name: 'TreeView'
" What is it that I am doing wrong in the code.

Kindly help.
Regards,
Rani Iyer

AnswerRe: Getting error Cannot access a disposed object. Object name: 'TreeView' Pin
Eddy Vluggen14-May-18 2:00
professionalEddy Vluggen14-May-18 2:00 
Questionc# unique character in char array Pin
swathiii12-May-18 2:44
swathiii12-May-18 2:44 
AnswerRe: c# unique character in char array Pin
Gerry Schmitz12-May-18 2:54
mveGerry Schmitz12-May-18 2:54 
QuestionRe: c# unique character in char array Pin
Richard MacCutchan12-May-18 3:32
mveRichard MacCutchan12-May-18 3:32 
AnswerRe: c# unique character in char array Pin
OriginalGriff12-May-18 4:16
mveOriginalGriff12-May-18 4:16 
AnswerRe: c# unique character in char array Pin
#realJSOP14-May-18 6:43
professional#realJSOP14-May-18 6:43 
GeneralRe: c# unique character in char array Pin
Mycroft Holmes14-May-18 14:13
professionalMycroft Holmes14-May-18 14:13 
GeneralRe: c# unique character in char array Pin
#realJSOP15-May-18 3:46
professional#realJSOP15-May-18 3:46 
QuestionWinforms - Find Value in Collection Containing Instantiations of a Custom Class Pin
DabbingTree11-May-18 8:37
DabbingTree11-May-18 8:37 
AnswerRe: Winforms - Find Value in Collection Containing Instantiations of a Custom Class Pin
Richard Deeming11-May-18 9:04
mveRichard Deeming11-May-18 9:04 
AnswerRe: Winforms - Find Value in Collection Containing Instantiations of a Custom Class Pin
BillWoodruff11-May-18 23:43
professionalBillWoodruff11-May-18 23:43 
QuestioniTextSharp - Character problem Pin
User 1367511411-May-18 1:10
User 1367511411-May-18 1:10 
AnswerRe: iTextSharp - Character problem Pin
Luc Pattyn13-May-18 23:18
sitebuilderLuc Pattyn13-May-18 23:18 
QuestionA second form from the first form, but both are active Pin
trottl10-May-18 23:18
trottl10-May-18 23:18 
AnswerRe: A second form from the first form, but both are active Pin
OriginalGriff10-May-18 23:25
mveOriginalGriff10-May-18 23:25 
GeneralRe: A second form from the first form, but both are active Pin
trottl10-May-18 23:32
trottl10-May-18 23:32 
GeneralRe: A second form from the first form, but both are active Pin
OriginalGriff10-May-18 23:47
mveOriginalGriff10-May-18 23:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.