Monday, November 13, 2006

realize inputBox in C#.net by referencing VB.net component

this will add VB.NET runtime dependency in C# application, you can even add a reference to Microsoft.VisualBasic assembly and call InputBox function implemented there:

string aaa = Microsoft.VisualBasic.Interaction.InputBox("enter a name", "Inputbox Demo", "ROCKY", 100, 100);
MessageBox.Show(aaa);

Another way of doing it without referencing VB.net is to create a form that looks like an inputBox:


using System;

namespace MyNamespace
{
public class InputDialog : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtInput;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button cmdOK;
private System.Windows.Forms.Button cmdCancel;

public string Value
{
get
{
return txtInput.Text;
}
set
{
txtInput.Text = value;
}
}

public InputDialog()
{
// Required for Windows Form Designer support
InitializeComponent();
}

private void InitializeComponent()
{
this.txtInput = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.cmdOK = new System.Windows.Forms.Button();
this.cmdCancel = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtInput
//
this.txtInput.Location = new System.Drawing.Point(48, 16);
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(232, 20);
this.txtInput.TabIndex = 0;
this.txtInput.Text = "";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(33, 16);
this.label1.TabIndex = 1;
this.label1.Text = "Input:";
//
// cmdOK
//
this.cmdOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.cmdOK.Location = new System.Drawing.Point(69, 48);
this.cmdOK.Name = "cmdOK";
this.cmdOK.TabIndex = 2;
this.cmdOK.Text = "OK";
//
// cmdCancel
//
this.cmdCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.cmdCancel.Location = new System.Drawing.Point(149, 48);
this.cmdCancel.Name = "cmdCancel";
this.cmdCancel.TabIndex = 2;
this.cmdCancel.Text = "Cancel";
//
// Form1
//
this.AcceptButton = this.cmdOK;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CancelButton = this.cmdCancel;
this.ClientSize = new System.Drawing.Size(292, 86);
this.Controls.Add(this.cmdOK);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtInput);
this.Controls.Add(this.cmdCancel);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
}
}

Sunday, November 05, 2006

convert sting value to the value of datetimepicker

generally the format should be everything except the AM part.

DateTime result = DateTime.Now;

if (DateTime.TryParse(this.label1.Text, out result))

{

this.dateTimePicker.Value = result;

}

We can also use DateTime.TryParseExact:
http://msdn2.microsoft.com/en-us/library/h9b85w22(VS.80).aspx

Saturday, November 04, 2006

WinXP folder/file sharing ------ create a hiden share

A> create a hiden share
1. right click disk or folder you want to share and click on Sharing and Security
2. specify a share name with 1 - 11 characters, end with a "$"
3. if want to give edit rights, click on "allow net work users to change my files


B> access the hiden share
1. My Computer --> Tools --> Map Netwok Drive
2. give an unused drive letter and network parth of the hiden share, make sure to include the "$"
3. if select "Reconnect at logon", connection will be connected everytime start the pc, otherwise have to manually map everytime

Thursday, November 02, 2006

inputbox in C#.net 2005 winform app

C# does not support inputbox, but C#.net2005 can have added reference to Microsoft.VisualBasic.dll

then in C#.net2005, you will be able to something like:

private void button3_Click(object sender, EventArgs e)
{
string userInput = Microsoft.VisualBasic.Interaction.InputBox("Give me something, I will tell you what you got?", "Question Popup", "Default Text",400,400).ToString();
MessageBox.Show(userInput);
}

use of YesNoCancel in messagebox.show() method in C#.net 2005 winform application

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = false;
DialogResult result = MessageBox.Show("Do you want to save any changes before closing this window? \n\n Click on 'Yes' to save; or \n Click on 'No' to close without saving; or \n Click on 'Cancel' to do nothing.", "Closing Window", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);

if (result == DialogResult.Yes)
{
MessageBox.Show("you clicked on YES, c u!");
}
else if (result == DialogResult.No)
{
MessageBox.Show("you clicked on NO, c u!");
}
else
{
e.Cancel = true;
}
}

Tuesday, October 31, 2006

detailed explanation of application.settings in C#.net 2005

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/SettingsCS_RL.asp

Monday, October 30, 2006

make databound checkbox checked in C#.net2005 winform application

normal checkbox can use checkbox1.checked = true;

databound checkbox needs to change the checkstate: checkbox1.checkstate = checkstate.checked;

make databound checkbox checked in C#.net2005 winform application

normal checkbox can use checkbox1.checked = true;

databound checkbox needs to change the checkstate: checkbox1.checkstate = checkstate.checked;

check whether form is already opened in C#.net 2k5 winform application

created the form object in application settings

Code to open the form:
private void button1_Click(object sender, EventArgs e) {

if ( Properties.Settings.Default.frmReg == null )
{
Properties.Settings.Default.frmReg = new frmRegistration();
Properties.Settings.Default.frmReg.Show( this );
}
else if ( Properties.Settings.Default.frmReg != null && Properties.Settings.Default.frmReg.WindowState ==

FormWindowState.Minimized)
{
Properties.Settings.Default.frmReg.WindowState = FormWindowState.Normal ;
}
else
{
Properties.Settings.Default.frmReg.Focus();
}
}

Code to close the opened form:
Properties.Settings.Default.frmReg = null;

something wrong with goggle?

what's happening with blogger.com? the whole weekend and this monday morning, sometimes can't create a new post, sometimes changes to the template are not reflected, come on!!!!!

Sunday, October 29, 2006

check whether form is already opened in C#.net 2k5 winform application

created the form object in application settings

Code to open the form:
private void button1_Click(object sender, EventArgs e) {

if ( Properties.Settings.Default.frmReg == null )
{
Properties.Settings.Default.frmReg = new frmRegistration();
Properties.Settings.Default.frmReg.Show( this );
}
else if ( Properties.Settings.Default.frmReg != null && Properties.Settings.Default.frmReg.WindowState == FormWindowState.Minimized)
{
Properties.Settings.Default.frmReg.WindowState = FormWindowState.Normal ;
}
else
{
Properties.Settings.Default.frmReg.Focus();
}
}

Code to close the opened form:
Properties.Settings.Default.frmReg = null;

testing post

testing post

Thursday, October 26, 2006

using OleDbCommand class to update a database table based on one or more variable values C#.net 2005 winform app

using ( OleDbConnection connection = new OleDbConnection( Properties.Settings.Default.HPMConnectionString ) )
{
connection.Open();
OleDbCommand changeActiveToNoCommand = new OleDbCommand( "UPDATE staff SET active = no WHERE staff_id = @staff_id" , connection );
changeActiveToNoCommand.Parameters.AddWithValue( "@staff_id" , Convert.ToInt64( staff_id ) );
changeActiveToNoCommand.ExecuteNonQuery();
}

using OleDbDataReader class to retrieve a value based on one or more variable values C#.net 2005 winform app

string staff_no = staffDataGridView.CurrentRow.Cells[0].Value.ToString();

string staff_id="";

using (OleDbConnection connection = new OleDbConnection(Properties.Settings.Default.HPMConnectionString))
{
//OleDbCommand findStaffIDCommand = new OleDbCommand("SELECT staff_id FROM staff WHERE staff_no = '" + staff_no + "'", connection);
OleDbCommand findStaffIDCommand = new OleDbCommand( "SELECT staff_id FROM staff WHERE staff_no = @staff_no",connection );
findStaffIDCommand.Parameters.AddWithValue( "@staff_no" , staff_no );

connection.Open();
OleDbDataReader reader = findStaffIDCommand.ExecuteReader();

while (reader.Read())
{
staff_id = (reader[0].ToString());
}
reader.Close();
}
MessageBox.Show( staff_id.ToString() );

Wednesday, October 25, 2006

dataGridView show value of any cell on selected row (multiRow select = false) C#.net 2005

MessageBox.Show(clientsDataGridView.CurrentRow.Cells[0].Value + "");

Tuesday, October 24, 2006

tooltip in VS2005 winform and asp applications

in winform applications, there is a component called "tooltip", which can be referenced by other visual components

in asp.net applications, there is a property called "tooltip" for most of the visual components, which can be used to enter single line tooltipss.
There is a third party tooltip utility called "cooltip", which is much more powerful:
http://www.acooltip.com/index.html#

Sunday, October 22, 2006

make combobox readonly in C#.net 2005 winform application

change the value of property "DropDownStyle" to "DropDownList)

referencing other classes C#.NET 2005

if you create a class within a subfolder of a winform project, you can't reference it within he code of a class in the root folder of project.
Solution:
to create the class in the same folder/rootlevel as the class that needs to have referencing code, and then move the class into anyother folder

access application settings in code C#.NET 2005

to access the values predefined in application settings, do this:
label1.Text = Properties.Settings.Default.aaa;
aaa is the name of the variable;

to persist the change, do this:
Properties.Settings.Default.aaa = "bbb";
Properties.Settings.Default.Save();

Wednesday, September 27, 2006

i am back

it's been a while.....
busy doing my masters and working at the same time. have to submit a report next monday, another one is coming soon.

feel lost.....