Solved

Help with overcoming C# .NET error - The name 'ViewExists' does not exist in current context.

Posted on 2014-03-05
4
974 Views
Last Modified: 2014-03-11
Please note code below where at this line item:

if (ViewExists(sNewNameCheck) == true)

it throws an error - The name 'ViewExists' does not exist in current context.

I'm new in C# .NET so please forgive me but I'd thought that it would work since the public bool ViewsExist(string sViewName) does exist within the same context.


Please note full listing of code here:  (has more than just the two areas of concern)  Some of the code does have Revit api .dlls though.

Also please note the two attachment listings of what ViewsExist looks like and the line item that is calling it!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Autodesk.Revit.DB;
using System.Diagnostics;
using Autodesk.Revit.UI;


namespace Copy_Views
{
    public partial class frmCopyViews : System.Windows.Forms.Form
    {
        static Autodesk.Revit.DB.Document _doc = null;
        private ExternalEvent m_ExEvent;
        private ExternalEventHandler m_Handler;
        //protected override void OnFormClosed(FormClosedEventArgs e)
        //{
        //    // we own both the event and the handler
        //    // we should dispose it before we are closed
        //    m_ExEvent.Dispose();
        //    m_ExEvent = null;
        //    m_Handler = null;

        //    // do not forget to call the base class
        //    base.OnFormClosed(e);
        //}

        public void EnableCommands(bool status)
        {
            foreach (System.Windows.Forms.Control ctrl in this.Controls)
            {
                ctrl.Enabled = status;
            }
       //     this.btnClose.Enabled = true;

            if (status == true)
                this.TopMost = true;
            else
                this.TopMost = false;

        }

        public frmCopyViews(ExternalEvent exEvent, ExternalEventHandler handler, Autodesk.Revit.DB.Document doc)
        {
            _doc = doc;
            InitializeComponent();

            m_ExEvent = exEvent;
            m_Handler = handler;
            buildGrid();
        }

        public bool ViewsExist(string sViewName)
        {


            // Filtered element collector is iterable
            string snamedata = "";
           
            IEnumerable<View> viewList = new FilteredElementCollector(_doc)
             .OfClass(typeof(View))
             .Cast<View>()
             .Where(q => !q.IsTemplate && q.ViewType != ViewType.SystemBrowser &&
                 q.ViewType != ViewType.ProjectBrowser)
             .OrderBy(c => c.ViewType).ThenBy(n => n.Name);

            foreach (View v in viewList)
            {
                snamedata =  v.Name;
                if (snamedata == sViewName)
                {
                    return true;

                }
            }

            return false;
            
        }

        public void buildGrid()
        {

            // Filtered element collector is iterable
            string namedata = "";
            DataTable dt = new DataTable();
            dt.Columns.Add("chk", Type.GetType("System.Boolean"));
            dt.Columns.Add("Name", Type.GetType("System.String"));
            dt.Columns.Add("Type", Type.GetType("System.String"));
            dt.Columns.Add("ID", Type.GetType("System.Int32"));
            IEnumerable<View> viewList = new FilteredElementCollector(_doc)
             .OfClass(typeof(View))
             .Cast<View>()
             .Where(q => !q.IsTemplate && q.ViewType != ViewType.SystemBrowser &&
                 q.ViewType != ViewType.ProjectBrowser)
             .OrderBy(c => c.ViewType).ThenBy(n => n.Name);

            foreach (View v in viewList)
            {
                DataRow dr = dt.NewRow();
                dr[0] = "false";
                dr[1] = v.Name;
                dr[2] = v.ViewType.ToString();
                dr[3] = v.Id.IntegerValue;
                dt.Rows.Add(dr);
                namedata += v.ViewType.ToString() + " " + v.Name + "\n";
                Debug.Print(v.Name);
            }

            dt.AcceptChanges();
            this._ViewData = dt;
        }

        public DataTable  _ViewData { get; set; }


        private void frmCopyViews_Load(object sender, EventArgs e)
        {

            DataTable dt = _ViewData.DefaultView.ToTable(true, "type");
            foreach (DataRow dr in dt.Rows)
            {
                comboBox1.Items.Add(dr[0].ToString());
            }
            
            comboBox1.Items.Insert(0, "(All)");
           SetGrid( _ViewData);
            comboBox1.Text = "(All)";
            
        }

        void GetTotal()
        {
           
        }
        void SetGrid(object obj1)
        {
            dataGridView1.DataSource = obj1;

            dataGridView1.Columns["ID"].Visible = false;
            dataGridView1.Columns["Type"].Width = 100;
            dataGridView1.Columns["Name"].Width = 300;
            dataGridView1.Columns["chk"].Width = 30;
            lbltotal.Text = dataGridView1.RowCount.ToString();
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex > 0)
            {
                DataRow[] drows = _ViewData.Select("type='" + comboBox1.Text  + "'");
               
                SetGrid(GetTableFromrows(drows));
            }
            else
                SetGrid( _ViewData);
            
        }
        private DataTable GetTableFromrows(DataRow[] drows)
        {
            DataTable dt1 = new DataTable();
            dt1.Columns.Add("chk", Type.GetType("System.Boolean"));
            dt1.Columns.Add("Name", Type.GetType("System.String"));
            dt1.Columns.Add("Type", Type.GetType("System.String"));
            dt1.Columns.Add("ID", Type.GetType("System.Int32"));
           
            foreach (DataRow dr in drows)
            {
                DataRow drow = dt1.NewRow();
                drow[0] = dr[0];
                drow[1] = dr[1];
                drow[2] = dr[2];
                drow[3] = dr[3];
                dt1.Rows.Add(drow);
            }
            return dt1;
        }


        void FilterByText(string _filter  )
        {
            DataRow[] drow;
            if (comboBox1.SelectedIndex > 0)
                drow = _ViewData.Select("Name " + _filter + " like '%" + txtName.Text + "%' and type ='" + comboBox1.Text + "'");
            else

                drow= _ViewData.Select("Name " + _filter + " like '%" + txtName.Text + "%'");
                SetGrid(GetTableFromrows(drow));
        }

        private void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            {
                dataGridView1.Rows[i].Cells[0].Value = chkAll.Checked;
            }          
        }

        private void btnAction_Click(object sender, EventArgs e)
        {
            m_ExEvent.Raise();



            //DialogResult = System.Windows.Forms.DialogResult.OK;
            //Close();

            //for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            //{
            //    string checkvalue = this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString();

            //    if (checkvalue == "True")
            //    {
            //        string sID = "";
            //        string sNewname = "";
            //        string sOldname = "";
            //        string sFind = txtAdd.Text;
            //        string sReplace = txtReplace.Text;
            //        sID = Convert.ToString(dataGridView1.Rows[i].Cells[3].Value);
            //        sNewname = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
            //        sOldname = sNewname;
            //        if (sFind != "" && sReplace != "")
            //        {
            //            sNewname = sNewname.Replace(sFind, sReplace);
            //        }

            //        txtData.Text = sNewname;
            //    }
            //}
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            FilterThis(sender, e);
        }

        private void FilterThis(object senderValue, EventArgs eValue)
        {
            string filter = "";
            if (txtName.Text.Length > 0)
            {
                if (rbContains.Checked)
                    filter = "";
                else
                    filter = " not ";
                FilterByText(filter);
            }
            else
            {
                comboBox1_SelectedIndexChanged(senderValue, eValue);
            }
        }

        private void rbReplace_CheckedChanged(object sender, EventArgs e)
        {
            if (rbReplace.Checked == true)
            {
                txtReplace.Visible = true;
                lblReplace.Visible = true;
                lblAddText.Text = "Find";
                //gbCopyOptions.Text = "Find / Replace Options";
                //btnAction.Text = "Find / Replace";
            }
            else
            {
                txtReplace.Visible = false;
                lblReplace.Visible = false;
                lblAddText.Text = "Add Text";
                //gbCopyOptions.Text = "Copy Options";
                //btnAction.Text = "Copy";
            }
        }

        public string getName()
        {
            return txtName.Text;
        }

        public IList<ViewCopy> getViewInformation()
        {
            IList<ViewCopy> views = new List<ViewCopy>();

            string sID = "";
            for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            {
                string checkvalue = this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString();

                if (checkvalue == "True")
                {
                 
                    string sNewname = "";
                    string sOldname = "";
                    string sFind = txtAdd.Text;
                    string sReplace = txtReplace.Text;

                    sID = Convert.ToString(dataGridView1.Rows[i].Cells[3].Value);
                    sNewname = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
                    sOldname = sNewname;

                    if (sFind != "" && sReplace != "")
                    {
                        sNewname = sNewname.Replace(sFind, sReplace);
                    }

                    ViewCopy view = new ViewCopy();
                    views.Add(view);
                    view.ViewID = sID;
                    view.NewName = sNewname;
                    view.OldName = sOldname;

                    //ids.Add(sID);
                    //txtData.Text = sNewname;
                }
            }

            return views;
        }

        private void txtName_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            //
            // Detect the KeyEventArg's key enumerated constant.
            //
            if (e.KeyCode == System.Windows.Forms.Keys.Enter)
            {

                ActiveControl = btnChange;


            }

        }

        private void btnChange_Enter(object sender, EventArgs e)
        {
            FilterThis(sender, e);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnList_Click(object sender, EventArgs e)
        {

        }

    }


    public class ExternalEventHandler : IExternalEventHandler
    {
        public string GetName()
        {
            return "ViewCopy";
        }

        public void Execute(UIApplication app)
        {
            copyTheView(app.ActiveUIDocument.Document);
            externalApp.thisApp.WakeFormUp();
        }

        public void copyTheView(Document doc)
        {
            IList<ViewCopy> viewsToCopy = externalApp.thisApp.getViews();
            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Transaction Name");
                foreach (ViewCopy viewToCopy in viewsToCopy)
                {
                    int idInt = Convert.ToInt32(viewToCopy.ViewID);
                    ElementId id = new ElementId(idInt);
                    Element element = doc.GetElement(id);
                    View view = element as View;

                   

                    string sNewNameCheck = "";
                    sNewNameCheck = viewToCopy.NewName.ToString();
                    
                    if (ViewExists(sNewNameCheck) == true)

                    {
                        View newView = doc.GetElement(view.Duplicate(ViewDuplicateOption.Duplicate)) as View;
                        newView.ViewName = viewToCopy.NewName;

                    }
                }
                tx.Commit();
            }
        }

    }


}

Open in new window

ExpertsExchange1.PNG
ExpertsExchange2.PNG
0
Comment
Question by:stephenlecomptejr
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
Typing error.

The method is called ViewsExists, with a s after View, but you are calling it without the s : ViewExist.

ViewsExist
ViewExist
0
 
LVL 1

Author Comment

by:stephenlecomptejr
Comment Utility
I was about to do a major face plant into palm on that one.  

However as I copied/pasted the correct syntax it is still giving me the error - please note attachment.
ExpertsExchange3.PNG
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
That's because you're working with two different classes. ViewsExist is a part of the frmCopyViews class; the method you are trying to call from, copyTheView, is a member of the ExternalEventHandler class. You'll probaly want to break the ViewsExist out into a utility class so that both classes can use it, if that's your intention.
0
 
LVL 1

Author Closing Comment

by:stephenlecomptejr
Comment Utility
Made the following class called Existing.cs

And then used this coding:

                    if (Copy_Views.Existing.ViewsExist(sNewNameCheck, doc) == true)
                    {
                        TaskDialog.Show("Warning", "View already exists!");
                    }

And it rebuilt without an error.

Thank you for your reply.  Sorry about the delay.   I was a little lazy expecting some code back but it was nice instead to try and figure it out on my own with applying what you were saying.  Again thank you very much!


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace Copy_Views
{
    class Existing
    {

        public static bool ViewsExist(string sViewName, Document sRevitDoc)
        {


            // Filtered element collector is iterable
            string snamedata = "";

            IEnumerable<View> viewList = new FilteredElementCollector(sRevitDoc)
             .OfClass(typeof(View))
             .Cast<View>()
             .Where(q => !q.IsTemplate && q.ViewType != ViewType.SystemBrowser &&
                 q.ViewType != ViewType.ProjectBrowser)
             .OrderBy(c => c.ViewType).ThenBy(n => n.Name);

            foreach (View v in viewList)
            {
                snamedata = v.Name;
                if (snamedata == sViewName)
                {
                    return true;

                }
            }

            return false;

        }


    }
}

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
optimize  c# code 7 48
Image(2) 3 21
ConsoleSql 1 12
getting id from database 5 24
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now