30 Ağustos 2012 Perşembe

to see all running tasks on windows

to see all running tasks on windows :

TASKLIST /v /fi "STATUS eq running"

for more: http://ss64.com/nt/tasklist.html

29 Ağustos 2012 Çarşamba

using toString() without doing null check

Checking for null each time before calling toString() for an object is annoying, especially while logging..
Here is the solution: 

string.Format("{0}", myObj); 
displays an empty string for null object, otherwise calls toString()..

28 Ağustos 2012 Salı

generate toString() automatically in Visual Studio

I am looking for a way to override toString() methods of my beans as i did in Eclipse..
here is a discussion about it: http://stackoverflow.com/questions/4932136/is-there-a-tostring-generator-available-in-visual-studio-2010

Pressing a dot, selecting override and toString() generates something like that:


public override string  ToString()
{
  return base.ToString();
}


However, what i want is a long string which is a concatanation of the bean's attributes.. I do not want to use reflection because of performance issues.. So, the solution is installing Autocode 4.0 http://visualstudiogallery.msdn.microsoft.com/48eeb43f-cb46-4680-b7df-11e73cf894ca
http://www.devprojects.net/download

After installation (which is running the downloaded .msi file), just press ctrl + enter to see the Autocode window in Visual Studio.. ( http://www.devprojects.net/blog/article/start-using-autocode )



Select tostr in the window and the following code will be genarated automatically:



override public string ToString()
        {
            string str = String.Empty;
            str = String.Concat(str, "ID = ", ID, "\r\n");
            str = String.Concat(str, "EskiID = ", EskiID, "\r\n");
            str = String.Concat(str, "Aktif = ", Aktif, "\r\n");
            str = String.Concat(str, "VersiyonSayisi = ", VersiyonSayisi, "\r\n");
            str = String.Concat(str, "Tanimi = ", Tanimi, "\r\n");
            return str;
        }



26 Ağustos 2012 Pazar

using description for enums while filling combobox

My ongoing project has a strict deadline, so we have to deliver it on time, we have no other chance..
Fortunately (or maybe unfortunately) we do not have to deliver the whole functionality at once.We deliver pieces of software gradually..
During development, i note down what to post, and i do post when i have time.. This is one of them..

writing enum descriptions:
public enum DescriptionType
    {
        [Description("Adres Türü")]
        AddressType= 1,
        [Description("Puan Türü")]
        ScoreType = 2,
        [Description("Arşiv Türü")]
        ArchiveType= 3,
...


// where the combobox is initially loaded (for me formLoad)
 this.comboDescriptionType.DataSource = Enum.GetValues(typeof( DescriptionType ));

// add this to combobox format event
 private void comboDescriptionType _Format(object sender, ListControlConvertEventArgs e)
        {
            try
            {
                 DescriptionType desc= ( DescriptionType )e.ListItem;
                e.Value = GetDescription(desc);
            }
            catch (Exception ex)
            {
                // No index selected.
            }
        }

     public static string GetDescription(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attributes =
                  (DescriptionAttribute[])fi.GetCustomAttributes(
                  typeof(DescriptionAttribute), false);
            return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
        }



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