Search This Blog

Thursday, February 7, 2013

Location and Size (C#)

C# > Controls > Location and Size

Location

Gets or sets the coordinates of the upper-left corner of the control relative to the upper-left corner of its container.

Size

Gets or sets the height and width of the control.

Example

label1.Location = new Point(4, 302);
label1.Size = new Size(85, 13);






Add dynamic event to controls (C#)

C# > ComponentModelCancelEventHandler 

Delegates the method that handles a cancel event.

Example

Add dynamic event to TextBox


txtReportHeader.Validating += new CancelEventHandler(MyValidating);
private void MyValidating(object sender, CancelEventArgs e)
{
     // some validation
}







Set font bold label (C#)

C# > Drawing > Font > Bold

Bold indicates when Font is bold.

Example:
label1.Font = new System.Drawing.Font(label1.Font, FontStyle.Bold);




Tuesday, January 29, 2013

?: Operator (Ternary) (C#)

C# > OperatorsTernary (?:)

Ternary (conditional) operator (?:) returns one of two values depending on the value of a Boolean expression.

The syntax for the ternary operator is

condition ? first_expression : second_expression;

Example:




namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string OddOrEven(int x)
        {
            return (x % 2 == 0) ? "Number is even" : "Number is odd";
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            string s;
            s = OddOrEven(10); // Number is even
            s = OddOrEven(11); // Number is odd
        }
    }
}










Verbatim String Literals (C#)

C# > Verbatim String Literals 

In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence.

Suppose you want to set a string as following:

string path = "C:\\Programs\\MyProgram\\My.exe";


You can use the verbatim string literal character (@) to build the string exactly as it appears within the double quotation marks.
string path = @"C:\Programs\MyProgram\My.exe";






Escape sequences (C#)



C# > Escape sequences

The special escape character is the backslash character (\).
\" Display a double quotation mark.
\'  Display a single quotation mark.
\\  Display a backslash.
\0 Null
\a Alarm
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Tab
\v Vertical tab
  
string msg = "Dan said: \"Hello world!\"";
Result is:
Dan said: "Hello world!"

string path = "C:\\MyDir";
Result is:
C:\MyDir






Filter DataTable with LINQ (Visual Basic)


VB.NET > Data > DataTable > Filter with LINQ

Use AsEnumerable method to return the input type DataTable as IEnumerable.
The CopyToDataTable method takes the results of a query and copies the data into a DataTable.






Example:

Public Class Form1

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim dt As DataTable = New DataTable("table")
    dt.Columns.Add("id", Type.GetType("System.Int32"))
    dt.Columns.Add("name", Type.GetType("System.String"))

    Dim dr As DataRow = dt.NewRow()
    dr("id") = 1
    dr("name") = "john"
    dt.Rows.Add(dr)

    dr = dt.NewRow()
    dr("id") = 2
    dr("name") = "dan"
    dt.Rows.Add(dr)

    Dim filteredTable As DataTable = (From n In dt.AsEnumerable()
        Where n.Field(Of Int32)("id") = 1
                    Select n).CopyToDataTable()

    ComboBox1.DataSource = filteredTable
    ComboBox1.DisplayMember = "name"
    ComboBox1.ValueMember = "id"
  End Sub

End Class