3 Ağustos 2012 Cuma

a few newbie tips (WinForms)

In this post, i shared a few tips which i noted down during my first experience with WinForms.

1st Tip:
populating a combobox with enum :

           this.comboBox.Items.AddRange(Enum.GetNames(typeof(MyEnumType)));


populating a combobox with collection:


            List sentenceList = server.retrieveSentences();
            this.comboBoxSentences.DataSource =  sentenceList  ;
            this.comboBox Sentences .DisplayMember = "SentenceCode";
            this.comboBox Sentences .ValueMember = "ID";

http://stackoverflow.com/questions/2417960/populating-a-combobox-using-c-sharp

2nd Tip:
the error message:

Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.

http://stackoverflow.com/questions/5532810/entity-framework-code-first-defining-relationships-keys
In my case, the problem was due to cascading deletions. Entity A includes a list of Entity B. And Entity S both includes Entity A and Entity B. In order to resolve it, i disabled cascade delete convention.

              modelBuilder.Conventions.Remove(<OneToManyCascadeDeleteConvention>);  


3rd Tip:
several LINQ samples here: http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b/viewsamplepack

4th Tip:
TreeView example:
         // the selected sentence in the sentence tree

         public Sentence selectedSentence;

         // fills treeview with sentence list retrieved from database
         private void fillTreeView()
        {
            // retrive sentences from database
            List sentenceList = server.retrieveSentences();
         
            // this dictionary holds tree nodes. The key is the id of sentence.
            Dictionary treeNodeDict<int, TreeNode &gt = new Dictionary(<int, TreeNode >);

            // the root sentence of the tree
            Sentence root = findRootNode( sentenceList  );
           
            // root sentence is turned into a tree node.
            TreeNode rootNode = this.treeView.Nodes.Add( root.Summary);
            rootNode.Name = root.ID.ToString();
            treeNodeDict.Add(root.ID, rootNode);

            // main loop for constructing the treeview
            foreach (var sentence in  sentenceList  )
            {
                TreeNode parent;
               
                // if dictionary contains the sentence, this means that sentence has been already
                // converted to a tree node, so use it
                if (treeNodeDict.ContainsKey( sentence  .ID))
                {
                    parent = treeNodeDict[ sentence  .ID];
                }
                 // if dictionary does not contain the sentence, create a new tree node and
                 // add to dictionary for later reuse.
                else
                {
                    parent = new TreeNode( sentence.Summar );
                    parent.Name =  sentence   .ID.ToString();
                    treeNodeDict.Add( sentence   .ID, parent);
                }

                // the inner loop is to find the children of the parent node (the node in the main loop)
                foreach (var child in  sentenceList)
                {
                    // skip non-children nodes.
                    if (child.ParentSentenceID !=sentence.ID) continue;

                    TreeNode childNode;

                    if (treeNodeDict.ContainsKey(child.ID))
                    {
                        childNode = treeNodeDict[child.ID];
                    }
                    else
                    {
                        childNode = new TreeNode(child.Summary);
                        childNode.Name = child.ID.ToString();
                        treeNodeDict.Add(child.ID, childNode);
                    }

                    parent.Nodes.Add(childNode);
                }
            }
        }

        // finds root node of the list. root node is the node with no parent.
        private Sentence findRootNode(List sentenceList)
        {
            foreach (var sentence in  sentenceList )
            {
                if (sentence.ParentSentenceID == null || sentence.ParentSentenceID == 0) return sentence;
            }

            return null;
        }

        // the following methods are for turning the color of the selected node to green.
        private void treeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (treeView.SelectedNode != null)
                treeView.SelectedNode.ForeColor = Color.Black;
            e.Node.ForeColor = Color.Green;
            // selected sentence in treeviw is highlighted to green
        }

        private void OwnerDrawAll(object sender, DrawTreeNodeEventArgs e)
        {
            if (((e.State & TreeNodeStates.Selected) != 0) && (!treeViewBirim.Focused))
                e.Node.ForeColor = Color.Blue;
            else
                e.DrawDefault = true;
        }

        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView.SelectedNode != null)
            {
                selectedSenetnce = server.retrieveSentenceByID(Int32.Parse(treeView.SelectedNode.Name));
                treeView.Focus();
            }
        }
    }


http://stackoverflow.com/questions/1838807/winforms-treeview-how-to-manually-highlight-node-like-it-was-clicked
http://www.java2s.com/Code/CSharp/GUI-Windows-Form/TreeViewExample.htm
http://www.java2s.com/Code/CSharp/GUI-Windows-Form/TreeViewDataBinding.htm

5th Tip:
Selected tabPage name:
http://stackoverflow.com/questions/3545624/return-selected-tabpage-name

              tab.SelectedTab.Name

Hiç yorum yok:

Yorum Gönder