September 23, 2007
Using PInvoke
Check out the PInvoke website. Greate Site for finding PInvoke syntax for c# Calls to the WIN 32 API.
A Winforms CheckList
- Check the StartPosition property is CenterParent
- Check the Text property (the Title) is set to something sensible
- IF it is a child window set the 'ShowInTaskbar' property to 'False'
- Check FormBorderStyle property is set to 'FixedDialog' on the Form if it is non-resizable.
- Check MaximiseBox property is set to 'True' on the Form if you want it
- Check MinimiseBox property is set to 'True' on the Form if you want it
- Check AcceptButton property is set on the Form to the 'default' accept/OK button
- Check CancelButton property is set on the Form to the 'default' cancel changes button
- IF you want the OK and cancel buttons to work automatically then set the DialogResult property of the button to Accept or Cancel etc.
- Check the TAB order is set.
this.AcceptButton = this.NAME_OF_ACCEPT_OK_BUTTON;
this.CancelButton = this.NAME_OF_CANCEL_BUTTON;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "FORM TITLE";
Modal/Modeless Dialogs
Modal Dialog
SomeForm sf = new SomeForm();
DialogResult res = sf.ShowDialog(this);
To close a form
... // within the form class itself
DialogResult = DialogResult.OK;
Close();
...
Set the AcceptButton and CancelButton to the correct buttons if required
Modeless Dialog
// Declare the modeless dialog
private MagnifyingGlassForm m_MagFrm = null;
// Creating and showing the modeless dialog
if (m_MagFrm == null)
{
m_MagFrm = new MagnifyingGlassForm();
m_MagFrm.Owner = this; // IMPORTANT Set the owner of the modeless dialog
m_MagFrm.Show(); // Show the modeless dialog
}
...
// To just hide the modeless dialog use Hide() method
m_MagFrm.Hide()
...
// Terminating the Modeless Dialog
if (m_MagFrm != null)
{
m_MagFrm.Close();
m_MagFrm.Dispose();
m_MagFrm = null;
}
May 9, 2007
Using DataTables, DataSets and DataAdaptors
Why use a Dataset?
Using summary
ADO.NET Tutorial
// Use Visual Studio to generate the data set classes. Use 'Typed' datasets //XXXDataSet m_XXXDataSet = new XXXDataSet(); // Create the data table and data adaptors XXXDataSet.XXXDataTable m_XXXDataTable; XXXDataAdapter m_XXXDataAdaptor = new XXXDataAdapter(); // Filling the dataset/datatable m_XXXDataTable = m_XXXDataAdaptor.GetData(); //m_DataAdaptor.Fill(m_XXXDataSet.XXX); //Dont forget to Dispose() //m_XXXDataSet.Dispose(); m_XXXDataTable.Dispose(); m_XXXDataAdaptor.Dispose(); // Finding a record //XXXDataSet.XXXRow lir = m_XXXDataSet.XXX.FindByName(requestor.ToLower()); XXXDataSet.XXXRow lir = m_XXXDataTable.FindByName(requestor.ToLower()); // ADD //m_XXXDataSet.XXX.AddXXXRow(userName, password); m_XXXDataTable.AddXXXRow(userName, password); m_Dirty = true; // DELETE //m_XXXDataSet.XXX.RemoveXXXRow(lir); m_XXXDataTable.RemoveXXXRow(lir); lir.Delete(); // UPDATING the database m_DataAdaptor.Update(m_XXXDataTable); //m_DataAdaptor.Update(m_XXXDataSet);
Double Buffering in C# .Net
Double Buffering in Dot Net 2.0
More Double Buffering in Dot Net 2.0 - Check this out as well
More Double Buffering in Dot Net 2.0 - Check this out as well
bool m_Dirty = true;
Bitmap m_Bitmap;
~XXXCtrl()
{
Dispose(false);
}
public void ChangeSomething(int xmin, int xmax)
{
...
Dirty = true; // Regenerate the bitmap (during the paint cycle)
Invalidate(); // Repaint
}
protected override void OnResize(EventArgs e)
{
_DisposeBitmap();
if ((ClientSize.Width > 0) && (ClientSize.Height > 0))
{
m_Bitmap = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
}
else
{
m_Bitmap = null;
}
Dirty = true;
base.OnResize(e);
Invalidate();
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Graphics gfx = e.Graphics;
if (Dirty)
{ // Only regenerate the bitmap when something has changed
_RenderToBitmap();
Dirty = false;
}
// Option 1: Redraw entire bitmap
//gfx.DrawRectangle(Pens.Red, e.ClipRectangle);
//_DisplayBitmap(gfx, BitmapRect()); //
// OR
// Option 2: Redraw just the area that has changed
//Debug.WriteLine("Paint: e.ClipRectangle=" +
e.ClipRectangle.ToString());
_DisplayBitmap(gfx, e.ClipRectangle);
}
private void _DisplayBitmap(Graphics gfx, Rectangle clipRectangle)
{
gfx.DrawImage(m_Bitmap, clipRectangle, clipRectangle, GraphicsUnit.Pixel);
}
// Draw the graphics onto a bitmap
private void _RenderToBitmap()
{
using (Graphics gs = Graphics.FromImage(m_Bitmap))
{
_Render(gs, BitmapRect());
}
}
private Rectangle BitmapRect()
{
return new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height);
}
// Here is the main graphics routine
private void _Render(Graphics ds, Rectangle area)
{
// Draw rectangle around client rectangle and text in centre
Rectangle rect = area;
rect.Height -= 1;
rect.Width -= 1;
using (Brush brush = new SolidBrush(this.BackColor))
{
ds.FillRectangle(brush, rect);
}
ds.DrawRectangle(Pens.Black, rect);
if ((area.Width > 25) && (area.Height > 10))
{
int xpos = 0;
// Draw axis
int x1 = area.Left + 20;
int x2 = area.Right - 20;
int ymid = (area.Height/2);
ds.DrawLine(Pens.Black, new Point(x1, ymid), new Point(x2, ymid));
...
}
}
private bool Dirty
{
get { return m_Dirty; }
set { m_Dirty = value; }
}
private void _DisposeBitmap()
{
if (m_Bitmap != null)
{
m_Bitmap.Dispose();
}
}
and in the designer class
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
_DisposeBitmap();
base.Dispose(disposing);
}
Subscribe to:
Comments (Atom)