Thursday, October 8, 2009

Prevent a user from moving a form at run time

Protected Overrides Sub WndProc(ByRef m As Message)
const Integer WM_NCLBUTTONDOWN = 161
const Integer WM_SYSCOMMAND = 274
const Integer HTCAPTION = 2
const Integer SC_MOVE = 61456

If (m.Msg = WM_SYSCOMMAND) &&(m.WParam.ToInt32() = SC_MOVE) Then
Return
End If

If (m.Msg = WM_NCLBUTTONDOWN) &&(m.WParam.ToInt32() = HTCAPTION) Then
Return
End If

MyBase.WndProc( m)
End Sub

Prevent a user from moving a form at run time

protected override void WndProc(ref Message m)
{
const int WM_NCLBUTTONDOWN = 161;
const int WM_SYSCOMMAND = 274;
const int HTCAPTION = 2;
const int SC_MOVE = 61456;

if((m.Msg == WM_SYSCOMMAND) && (m.WParam.ToInt32() == SC_MOVE))
{
return;
}

if((m.Msg == WM_NCLBUTTONDOWN) && (m.WParam.ToInt32() == HTCAPTION))
{
return;
}

base.WndProc (ref m);
}

Tuesday, October 6, 2009

Adding Dynamic Menus from a Database

Fetch the Records from database which has Page Name and URL,


MenuItem mnuItem;
for (int intRwcnt = 0; intRwcnt < dtMenu.Rows.Count; intRwcnt++)
{
mnuItem = new MenuItem(dtMenu.Rows[intRwcnt]["PageName"].ToString(), dtMenu.Rows[intRwcnt]["PageName"].ToString(), "", dtMenu.Rows[intRwcnt]["PageUrl"].ToString());
mnuCommon.Items.Add(mnuItem);
}

Opening a PDF file from ASP.Net

Use this single line of Code instead of going for Process to start Adobe Reader. This opens in a New Tab or in new window leaving your existing project to work.

ScriptManager.RegisterStartupScript(this, this.GetType(), "", "window.open('../PDF/Sample.pdf','_blank');", true);

Creating a PDF from Asp.Net

We need to have IText Sharp dll for this Code to work.. You can make a search in Google to get the Dll file. And after importing the dll you can use this code to create a PDF in specified path.

protected void btnPDF_Click(object sender, EventArgs e)
{
//ScriptManager.RegisterStartupScript(this, this.GetType(), "", "window.open('../PDFTest.aspx?PDFUrl=StatusLog.pdf','_blank');", true);


Response.Write("");
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=StatusLog.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
grdStatus.AllowPaging = false;
grdStatus.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}

To get the Grid Edit Index from a Template column Event

protected void imgButton_Click(object sender, ImageClickEventArgs e)
{
ImageButton imgBtn = (ImageButton)sender;
GridViewRow row = (GridViewRow)imgBtn.NamingContainer;
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "Selected Index Is: " + grdRequests.Rows[row.RowIndex].Cells[1].Text.ToString() + ", true);
}

How can I show vertical text in the cell?

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)

{

if (e.ColumnIndex == 0 && e.RowIndex > -1 && e.Value != null)

{

e.Paint(e.CellBounds, DataGridViewPaintParts.All

& ~DataGridViewPaintParts.ContentForeground);

StringFormat sf = new StringFormat();

sf.Alignment = StringAlignment.Center;

sf.LineAlignment = StringAlignment.Center;

sf.FormatFlags = StringFormatFlags.DirectionVertical;

e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,

new SolidBrush(e.CellStyle.ForeColor), e.CellBounds, sf);

e.Handled = true;

}

}