Posts filed under 'c-sharp'
function checkitems combolbox list c-sharp
public void CheckItem(string sItem)
{
for (int i = 0; i < cblPinakes.Items.Count; i++)
{
if (cblPinakes.Items[i].ToString() == sItem)
{
cblPinakes.SetItemChecked(i, true);
cblPinakes.SetSelected(i, true);
}
}
}
Add comment October 18, 2008
Function c# Load tables from sql 2000 -2005
public void Load_Tables()
{
dataSetPinakes.Tables[0].Clear();
System.Data.SqlClient.SqlCommand sqlcmdselect = new SqlCommand();
sqlcmdselect.CommandText = “select name, ‘0′ as see from sysobjects where xtype=’U'” ;
sqlcmdselect.Connection = MSSqlcon;
System.Data.SqlClient.SqlDataAdapter sqlDataAdapter_kos = new SqlDataAdapter();
sqlDataAdapter_kos.SelectCommand = sqlcmdselect;
try
{
sqlDataAdapter_kos.Fill(dataSetPinakes.Tables[0]);
DataRow[] row_select = dataSetPinakes.Tables[0].Select();
cblPinakes.Items.Clear();
for (int bb = 0; bb < row_select.Length; bb++)
{
object obj = row_select[bb][0].ToString();
cblPinakes.Items.Add(obj);
}
DataTable dt = dataSetPinakes.Tables[0].Copy();
dt.Clear();
dt.ReadXml(“Tables.xml”);
DataRow[] rowsel = dt.Select();
for (int ii = 0; ii < rowsel.Length; ii++)
{
if (rowsel[ii][1].ToString() == “1″)
{
CheckItem(rowsel[ii][0].ToString());
}
}
}
catch (Exception ex)
{ MessageBox.Show(ex.ToString()); }
}
Add comment October 18, 2008
Function c# run sql destination
public void runsqldestination(string query)
{
string sqlupd = query;
SqlCommand cmd6 = new SqlCommand(sqlupd, MSSqlcon2);
cmd6.CommandTimeout = 99920;
try
{
cmd6.ExecuteNonQuery();
}
catch
{ MessageBox.Show(” Error ! “); }
}
Add comment October 18, 2008
Convert dataset to sql query insert
public void GetDataset(DataSet ds, string TableName)
{
string squeryInsert = “insert into ” + TableName + ” (“;
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
if (i == 0)
{
squeryInsert = squeryInsert + ds.Tables[0].Columns[i].ToString();
}
else
{
squeryInsert = squeryInsert + “,” + ds.Tables[0].Columns[i].ToString();
}
}
squeryInsert = squeryInsert + “) values ( “;
DataRow[] row1 = ds.Tables[0].Select();
string squery = “”;
for (int ii = 0; ii < row1.Length; ii++)//pernoume ta rows
{
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)// pernoume ta column
{
if (i == 0)
{
squery = squery + “N’” + row1[ii][i].ToString().Replace(“‘”, “””).Replace(@”\”, @”\\”) + “‘”;
}
else
{
squery = squery + “,N” + “‘” + row1[ii][i].ToString().Replace(“‘”, “””).Replace(@”\”, @”\\”) + “‘”;
}
}
squery = squeryInsert + squery + ” )”;
RunSql(squery);
runsqldestination(squery);
lblProgressBar.Text = “Progess ” + TableName.ToUpper() + ” :”;
progressBar1.Maximum = row1.Length;
progressBar1.Minimum = 0;
progressBar1.Value = ii;
Application.DoEvents();
squery = “”;
}
}
Add comment October 18, 2008
Janus gridEX click event c#
Useful query c# adonet
private void gridEX1_Click(object sender, EventArgs e)
{
//get position
int i = this.gridEX1.SelectedItems[0].Position;
//get id from column(0) and row position ( dataset)
string id= gridEX1.GetRow(i).Cells[0].Text;
}
Add comment September 22, 2008
Function field value is egual in .net c#
private static bool fieldValuesAreEqual(object[] lastValues, DataRow currentRow, string[] fieldNames)
{
bool areEqual = true;
for (int i = 0; i < fieldNames.Length; i++)
{
if (lastValues[i] == null || !lastValues[i].Equals(currentRow[fieldNames[i]]))
{
areEqual = false;
break;
}
}
return areEqual;
}
Add comment September 22, 2008
Function Crop image
static Image Crop(Image imgPhoto, int Width, int Height, AnchorPosition Anchor)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width/(float)sourceWidth);
nPercentH = ((float)Height/(float)sourceHeight);
if(nPercentH < nPercentW)
{
nPercent = nPercentW;
switch(Anchor)
{
case AnchorPosition.Top:
destY = 0;
break;
case AnchorPosition.Bottom:
destY = (int)(Height – (sourceHeight * nPercent));
break;
default:
destY = (int)((Height – (sourceHeight * nPercent))/2);
break;
}
}
else
{
nPercent = nPercentH;
switch(Anchor)
{
case AnchorPosition.Left:
destX = 0;
break;
case AnchorPosition.Right:
destX = (int)(Width – (sourceWidth * nPercent));
break;
default:
destX = (int)((Width – (sourceWidth * nPercent))/2);
break;
}
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX,destY,destWidth,destHeight),
new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
Add comment September 14, 2008
Function fix size image
public Image FixedSize(Image imgPhoto, int Width, int Height)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)Width/(float)sourceWidth);
nPercentH = ((float)Height/(float)sourceHeight);
//if we have to pad the height pad both the top and the bottom
//with the difference between the scaled height and the desired height
if(nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = (int)((Width – (sourceWidth * nPercent))/2);
}
else
{
nPercent = nPercentW;
destY = (int)((Height – (sourceHeight * nPercent))/2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.Clear(Color.Red);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX,destY,destWidth,destHeight),
new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
Add comment September 14, 2008
Function for resize image with proportion
public Image ConstrainProportions(Image imgPhoto, int Size, Dimensions Dimension)
{
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
float nPercent = 0;
switch(Dimension)
{
case Dimensions.Width:
nPercent = ((float)Size/(float)sourceWidth);
break;
default:
nPercent = ((float)Size/(float)sourceHeight);
break;
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX,destY,destWidth,destHeight),
new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
Add comment September 14, 2008
Function for resize image By Percent
static Image ScaleByPercent(Image imgPhoto, int Percent)
{
float nPercent = ((float)Percent/100);
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
grPhoto.DrawImage(imgPhoto,
new Rectangle(destX,destY,destWidth,destHeight),
new Rectangle(sourceX,sourceY,sourceWidth,sourceHeight),
GraphicsUnit.Pixel);
grPhoto.Dispose();
return bmPhoto;
}
Add comment September 14, 2008