Saturday, September 1, 2012

Tab Control


using System.Collections.Generic;

namespace MainForm.Core {
    public class Map<T, V> : Dictionary<T, V> {
        public void put(T t, V v) {
            if (this.Keys == null || this.Keys.Count < 1 || !this.ContainsKey(t)) {
                Add(t, v);
            } else {
                this[t] = v;
            }
        }
    }
}
//---------------------------------------------------------------------------------------

using System;
using MainForm;
public class Variables {

//bu yerda TabControlning TabPanel ida belgi(name, title) qo`yilmaydi
public const string NO_WRITE = "Rahimjon";

}
//---------------------------------------------------------------------------------------

using System.Windows.Forms;
namespace MainForm.Core {
    public class Panel : System.Windows.Forms.Panel {
        HorizontalPanel p = null;
        public Panel() {
            //p.Height = 10;
            p = new HorizontalPanel(true);
            p.Dock = DockStyle.Bottom;
            p.BackColor = System.Drawing.Color.Transparent;
            p.Name = Variables.NO_WRITE;
            p.BorderStyle = BorderStyle.FixedSingle;
            Controls.Add(p);
            p.AutoScroll = false;
        }
        Map<string, string> dict = new Map<string, string>();
        public void add(Control value, string text) {
            dict.put(value.Name, text);
            if (Controls.IndexOfKey(value.Name) > -1) {
                BringToFront(value.Name);
            } else {
                Controls.Add(value);
                value.Dock = DockStyle.Fill;
            }
        }
        public void addNew(Control value, string text) {
            dict.put(value.Name, text);
            Controls.RemoveByKey(value.Name);
            Controls.Add(value);
            value.Dock = DockStyle.Fill;
        }
        public void BringToFront(string name) {
            if (!Controls.ContainsKey(name)) { return; }
            Controls[Controls.IndexOfKey(name)].BringToFront();
            selected(name);
        }
        protected override void OnControlAdded(ControlEventArgs e) {
            if ("".Equals(e.Control.Name)) { Controls.Remove(e.Control); return; }
            if (Controls.Find(e.Control.Name, true).Length > 1) {
                Controls.Remove(e.Control);
                BringToFront(e.Control.Name);
            }
            BringToFront(e.Control.Name);
            getPage(e.Control);
        }
        Button getPage(Control c) {
            if (Variables.NO_WRITE.Equals(c.Name)) { return null; }
            Button b = new Button();
            b.Text = dict[c.Name];
            b.Name = c.Name;
            b.AutoSize = true;
            p.add(b);
            b.Click += new System.EventHandler(pageClick);
            selected(c.Name);
            return b;
        }
        void selected(string name) {
            if (Variables.NO_WRITE.Equals(name)) { return; }
            foreach (Control s in p.Controls) {
                if (s.GetType() == typeof(Button)) {
                    ((Button)s).FlatStyle = FlatStyle.Standard;
                }
            }
            if (p.Controls.ContainsKey(name) && typeof(Button) == p.Controls[name].GetType()) {
                ((Button)p.Controls[name]).FlatStyle = FlatStyle.Flat;
            }
        }
        void pageClick(object sender, System.EventArgs e) {
            BringToFront(((Button)sender).Name);
        }
        public void clear() {
            Controls.Clear();
        }
        protected override void OnControlRemoved(ControlEventArgs e) {
            if (!Variables.NO_WRITE.Equals(e.Control.Name)) {
                p.Controls.RemoveByKey(e.Control.Name);
            }
        }
        public void RemoveByKey(string name) {
            if (!(Utils.isNullOrEmpty(name) || Variables.NO_WRITE.Equals(name))) {
                p.Controls.RemoveByKey(name);
            }
        }
        public void Remove(Control control) {
            if (control != null) {
                RemoveByKey(control.Name);
            }
        }
    }
}


Horizontal Panel


using System.Drawing;
using System.Windows.Forms;
namespace MainForm.Core {
    public class HorizontalPanel : System.Windows.Forms.Panel {
        public HorizontalPanel() {
            Width = 0;
            Height = 1;
            AutoSize = true;
            AutoScroll = false;
        }
        public HorizontalPanel(bool AutoScroll) {
            Width = 0;
            Height = 1;
            AutoSize = !AutoScroll;
            this.AutoScroll = AutoScroll;
        }
        int x = 0;
        int h = 0;
        public void add(Control control, int split) {
            h = split;
            //x = (x == 0) ? 0 : x += split;
            HorizontalScroll.Enabled = true;
            control.Location = getLocation();// new Point(x, 0);
            Controls.Add(control);
            x += control.Width;
            if (x > Width && this.AutoScroll == true) {
                 Height = control.Height + 20;
            } else {
                 Height = control.Height;
            }
        }
        Point getLocation() {
            x = (x == 0) ? 0 : x += h;
            return new Point(x, 0);
        }

        public void add(Control control) {
            add(control, 0);
        }
        protected override void OnControlRemoved(ControlEventArgs e) {
            x = 0;
            foreach (Control c in Controls) {
                c.Location = getLocation();
                x += c.Width;
            }
        }
    }
}

Vertical Panel



using System.Drawing;
using System.Windows.Forms;
namespace MainForm.Core {
    public class VerticalPanel : System.Windows.Forms.Panel {
        public VerticalPanel() {
            Width = 1;
            Height = 0;
            AutoSize = true;
            AutoScroll = false;
        }
        //public VerticalPanel(bool AutoScroll) {
        //    Width = 1;
        //    Height = 0;
        //    AutoSize = !AutoScroll;
        //    this.AutoScroll = AutoScroll;
        //}
        int x = 0;
        int h = 0;
        public void add(Control control, int split) {
            h = split;
            //x = (x == 0) ? 0 : x += split;
            HorizontalScroll.Enabled = true;
            control.Location = getLocation();// new Point(x, 0);
            Controls.Add(control);
            x += control.Height;
            //if (x > Height && this.AutoScroll == true) {
            //    Width = control.Width + 20;
            //} else {
            //    Width = control.Width;
            //}
        }
        Point getLocation() {
            x = (x == 0) ? 0 : x += h;
            return new Point(0, x);
        }
        public void add(Control control) {
            add(control, 0);
        }
        protected override void OnControlRemoved(ControlEventArgs e) {
            x = 0;
            foreach (Control c in Controls) {
                c.Location = getLocation();
                x += c.Height;
            }
        }
    }
}

Tuesday, May 22, 2012

ArrayList-Utils


using System;
public class ArrayUtils<T>
    {
        System.Collections.Generic.Dictionary<string, T> registrationList;
        public void setList(System.Collections.ArrayList list, string key)
        {
            int k = -1, n = list.Count;
            registrationList = new System.Collections.Generic.Dictionary<string, T>();
            System.Reflection.PropertyInfo property = typeof(T).GetProperty(key);
            while (++k < n)
            {
                if (!Utils.isNull(property))
                {
                    registrationList.Add(property.GetValue((T)list[k], null).ToString(), (T)list[k]);
                }
            }
        }
        public int ListLength { get { return registrationList.Count; } }
        public T getByKey(string key)
        {
            return (T)registrationList[key];
        }
    }


///////////////////////// Foydalanish


//class1 bizni class imiz
public class class1{
int id;
int name;
inr code;
public int ID{get{return id;}set{id=value;}}
public string Name{get{return name;}set{name=value;}}
public string Code {get{return code;}set{code=value;}}
}

//bizga ArrayList bor uning tarkibi class1 lardan tashkil topgan
ArrayList list=new ArrayList()
for(int i=0;i<10;i++)
{ class1 c=new class1();
   c.id=1; c.name="bir"; c.code="BIR";
}

//biz ArrayUtils class tool imizga kiritamiz, e`tibor bering "Name" so`ziga- ya`ni By Name set qilingan. Name property bo`yicha search qilishimiz mumkin bu yerda
Utils.ArrayUtils<ColumnItem> utils = new Utils.ArrayUtils<ColumnItem>();

utils.setList(list, "Name");

//va uni ichidan "Rahim" so`zi yozilgan elementni Code ni olishimiz mumkin
col = ((ColumnItem)utils.getByKey("Rahim")).Code




______________________________________________________________
   Kind Regards
   Rakhim Turdiev

Saturday, April 7, 2012

dynamic xotira

Dynamic xotira haqida
Dasturlashda ikki xil xotiradan foydalanamiz: static, dynamic.
Static - bu usuldan foydalaniladigan objectlar stack dan joy oladi, Stack esa chegaralangan. Katta bo`lmagan, ko`p foydalanilmaydigan yo block ichidagi object larda foydalanish maqsadga muvofiq.

{
string a="rahim",b="123";
int e=3;
}


Dynamic - bu usulda object ma`lumoti dynamic xotirada saqlanadi va bizga bu dynamic ma`lumot uchun link beriladi xolos.

{
Program p=new Program();
...
TextBox t=new TextBox();
}


Agarda biz

{
TextBox t=new TextBox();
Controls.Add(t);
t=new TextBox();
Controls.Add(t);
t=new TextBox();
Controls.Add(t);
}

deb yozadigan bo`lsak formaga 3 ta textbox chiziladi. 
Savol: biz 1 ta t ni ishlatdikku nega 3 ta chizdi?
Javob: chunki t -shunchaki bir link edi. biz 3 marta textbox ni dynamic chaqirib qo`shib qo`ydik, bizda faqt link yangilandi. ma`lumot esa shu noma`lum joyda qolib ketdi. ma`lumot turgan link esa yangisiga o`zgarib qoldi.

Tuesday, April 3, 2012

DBmanager


using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
public partial class DBmanager
{
    public DBmanager()
    {
        DBmanager.connectionString = System.Configuration.ConfigurationManager.AppSettings["Cnstr"].ToString();
    }
    static string connectionString = "";
    public static string con_str()
    {
        return connectionString;
        //return @"Initial Catalog=Uzstandart;Data Source=.; Integrated Security=true";
    }
    public static int? exec(SqlCommand cmd)
    {
        int? val = null;
        using (SqlConnection conn = new SqlConnection(con_str()))
        {
            cmd.Connection = conn;
            try
            {
                if (conn.State != ConnectionState.Open) conn.Open();
                val = cmd.ExecuteNonQuery();
                conn.Close();
                return val;
            }
            catch
            {
                if (conn.State != ConnectionState.Closed) conn.Close();
                // bu qachonki xatoni aniqlash qiyin bo`lganda xato ba`zada bo`lsa vaqtincha ochib qo`yiladi
                //throw new Exception(exp.Message);
                return null;
            }
        }
    }
    public static ArrayList getDataList(SqlCommand cmd)
    {
        ArrayList lst = new ArrayList();
        using (SqlConnection conn = new SqlConnection(con_str()))
        {
            cmd.Connection = conn;
            try
            {
                if (conn.State != ConnectionState.Open) conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    int n = dr.FieldCount, i = -1;
                    object[] o = new object[n];
                    while (++i < n)
                    {
                        o[i] = dr[i];
                    }
                    lst.Add(o);
                }
                conn.Close();
                dr.Close();
                return lst;
            }
            catch (Exception exp)
            {
                if (conn.State != ConnectionState.Closed) conn.Close();
                //bu qachonki xatoni aniqlash qiyin bo`lganda xato ba`zada bo`lsa vaqtincha ochib qo`yiladi
                throw new Exception(exp.Message);
                //return null;
            }
        }
    }
    public static Object[] getData(SqlCommand cmd)
    {
        Object[] o = null;
        using (SqlConnection conn = new SqlConnection(con_str()))
        {
            cmd.Connection = conn;
            try
            {
                if (conn.State != ConnectionState.Open) conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    int n = dr.FieldCount, i = -1;
                    o = new object[n];
                    while (++i < n)
                    {
                        o[i] = dr[i];
                    }
                }
                conn.Close();
                dr.Close();
                return o;
            }
            catch
            {
                if (conn.State != ConnectionState.Closed) conn.Close();
                // bu qachonki xatoni aniqlash qiyin bo`lganda xato ba`zada bo`lsa vaqtincha ochib qo`yiladi
                //throw new Exception(exp.Message);
                return null;
            }
        }
    }
    public static object value(SqlCommand cmd)
    {
        object val = null;
        using (SqlConnection conn = new SqlConnection(con_str()))
        {
            cmd.Connection = conn;
            try
            {
                if (conn.State != ConnectionState.Open) conn.Open();
                val = cmd.ExecuteScalar();
                conn.Close();
                return val;
            }
            catch (Exception exp)
            {
                if (conn.State != ConnectionState.Closed) conn.Close();
                //bu qachonki xatoni aniqlash qiyin bo`lganda xato ba`zada bo`lsa vaqtincha ochib qo`yiladi
                throw new Exception(exp.Message);
                //return null;
            }
        }
    }


    SqlConnection sqlConnection;



    public void newConnection()
    {
        if (sqlConnection == null)
        {
            sqlConnection = new SqlConnection(con_str());
        }
    }
    public void open()
    {
        if (sqlConnection.State != ConnectionState.Open)
        {
            sqlConnection.Open();
        }
    }
    SqlTransaction transaction;
    public void beginTransaction()
    {
        transaction = sqlConnection.BeginTransaction();
    }
    public SqlTransaction Transaction
    {
        get
        {
            return transaction;
        }
    }
    public void close()
    {
        if (sqlConnection.State != ConnectionState.Closed)
        {
            sqlConnection.Close();
        }
    }
    public void commit()
    {
        transaction.Commit();
    }
    public void rollBack()
    {
        transaction.Rollback();
    }
    public Boolean execute(SqlCommand cmd)
    {
        if (cmd == null) { return false; }
        cmd.Connection = sqlConnection;
        cmd.Transaction = transaction;
        try
        {
            if (null != cmd.ExecuteNonQuery())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }
    }
}