To add a column to an already exists table use this code to do it string[] sql_array = new string[] { "ALTER TABLE table_name ADD COLUMN column_name column_cons." }; foreach (string sql_com in sql_array) { m_dbConnection = new SQLiteConnection(sql_path); m_dbConnection.Open(); try { string sql = sql_com; SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection); command.ExecuteNonQuery(); } catch (Exception ex) { //catch exception } finally { m_dbConnection.Close(); m_dbConnection.Dispose(); GC.Collect(); } } This command "ALTER TABLE table_name ADD COLUMN column_name column_cons."
example: ALTER TABLE employees ADD COLUMN age INT the command that tell SQLite database to create a column in the database. You can add as much as you want in the array to create the desire number of columns
0 Comments
If you want to make a software that deal with files or folders and you want to open windows explorer and select the file and folder use this code string path = path_to_your_file_or_folder; Process.Start("explorer.exe", @"/select, " + "\"" + path + "\""); select your path and call Process.Start method and windows explorer will open while the file or folder is being selected;
If you want to add a button in a textbox put this code in Load method or in a method and call it when windows load (Load method) private void Form1_Load(object sender, EventArgs e) { var btn = new Button(); btn.Size = new Size(25, yourtextbox.ClientSize.Height + 2); btn.Location = new Point(yourtextbox.ClientSize.Width - btn.Width, -1); btn.Cursor = Cursors.Default; btn.Image = Properties.Resources.Icon_name_in_resource; toolTip1.SetToolTip(btn, "Sometext"); btn.Click += btn_Click; yourtextbox.Controls.Add(btn); //Send EM_SETMARGINS to prevent text from disappearing underneath the button SendMessage(yourtextbox.Handle, 0xd3, (IntPtr)2, (IntPtr)(btn.Width << 16)); base.OnLoad(e); } [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp); private void btn_Click(object sender, EventArgs e) { MessageBox.Show("hello world"); } Just change the code in black and it will work like charm.
To call in a method in another form (not in another class) you can use this code which will easily let you call the method if (System.Windows.Forms.Application.OpenForms["Form1"] != null) { (System.Windows.Forms.Application.OpenForms["Form1"] as Form1).Method() } You can change 'Form1' to the name of the form you need.
To control your GUI better, it is better to have a knowledge how your labels are in the design time without having to select the label every time you need to know. You can do this by using a simple code that create a normal label but with border in the design time by using the below source code. Make a .cs file put it in your package and build your package them in toolbox project's components you will find MyLabel item using System;
using System.Drawing; using System.Windows.Forms; namespace Owf { public class MyLabel : Label { public MyLabel() { InitializeComponent(); } private void InitializeComponent() { this.SuspendLayout(); // // MyLabel // this.Paint += new System.Windows.Forms.PaintEventHandler(this.MyLabel_Paint); this.ResumeLayout(false); } private void MyLabel_Paint(object sender, PaintEventArgs e) { if (this.DesignMode) { using (Pen pen = new Pen(Color.Gray)) { pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; e.Graphics.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1); } } } } } You can know if your software runs as administrator or not by using a simple variable like this public bool IsElevated { get { return new WindowsPrincipal(WindowsIdentity.GetCurrent()) .IsInRole(WindowsBuiltInRole.Administrator); } } IsElevated will return true if the software runs as admin or false if not.
To get a software's version you need to use this code: string version = Assembly.GetExecutingAssembly().GetName().Version.ToString() "version" now has the version of your software in x.x.x.x way you can use .SubString to cut it as you want.
Hi All,
This blog is for posting tips and tricks about C# and Java (mainly C#). And i hope that this blog will be useful for many new programmers specially who learn programming by themselves. Everyday will be a new tip/trick. If someone is interesting in specific thing please comment and i will try my best to show help. |
SamiHi, I program in C# and also in Java. I hope that i will be useful for you with these basic tips and tricks in programming ArchivesCategories |