I often would like to have ProgressBar (hidden and shown only while some background application work is doing) in my StatusStrip. My expectation is the ProgressBar should always fill entire available free space in StatusStrip, when form is Maximizing, Resizing etc. This kind of behaviour can’t be set in Visual Studio Designer with control properties settings. Below is my solution to solve this problem. If you know better, simplest way then please let me know by your comment :)
// Method to calculate suitable ToolStripProgressBar width
private int CalculateControlSizeInStatusStrip(StatusStrip statusStrip,
Control statusProgressBar)
{
int width = statusStrip.ClientSize.Width;
int gripWidth = 20;
if (statusStrip.Items.Count > 1)
{ // Calculate width for progress bar if more controls
// on StatusStrip
for (int i = 0; i < statusStrip.Items.Count - 1; i++)
{
// width calculation
width -= statusStrip.Items[i].Width;
}
}
return width - gripWidth;
}
on your StatusStrip Resize event:
private void statusStripMain_Resize(object sender, EventArgs e)
{
// Expand dynamically width of ToolStripProgressBar to
// fill entire free space in StatusStrip
toolStripStatusProgressBarMainTask.ProgressBar.Width = CalculateControlSizeInStatusStrip((StatusStrip)sender, toolStripStatusProgressBarMainTask);
}
I did something like that today so I thought to share. Example of use DataTable with DataGridView control. In addition HeaderText property of DataGridView is assigned based on DataTable DataColumn.Caption property value.
using System.Data;
using System;
private DataTable myDataTable = new DataTable();
DataColumn getNewColumn(string columnName, string columnCaption
, string columnType)
{
// Create a new column to be used in the DataTable
DataColumn dc =
new DataColumn(columnName, System.Type.GetType(columnType));
// Column caption can be used later for HeaderText prop
// in DataGridView
dc.Caption = columnCaption;
return dc;
}
void CreateNewRow(string[] rowData)
{
// Create a DataRow based on the DataTable
DataRow dr = myDataTable.NewRow();
// Add values to the DataRow columns
int i = 0;
foreach (string fieldData in rowData)
{
if (i < dataTable.Columns.Count)
// assign by index
dr[i] = fieldData;
else
break;
i++;
}
// Add the row to the DataTable
myDataTable.Rows.Add(dr);
}
private void FillDataGridViewFromTextFile(string filename)
{
// Clear last results from DataGridView
if (DataGridView1.RowCount > 0)
myDataTable.Rows.Clear();
if (myDataTable.Columns.Count == 0)
{ // if DataTable is not constructed than Add Columns
// to the datatable
myDataTable.Columns.Add(getNewColumn("ComputerName"
, "Computer name"
, "System.String"));
}
// Load data from text file into DataTable
using (System.IO.StreamReader reader
= new System.IO.StreamReader(filename))
{
string line = "";
while ((line = reader.ReadLine()) != null)
{
if (!String.IsNullOrEmpty(line))
{
// add row data to "columns array"
string[] rowData = new string[] { line };
// add new row to DataTable
CreateNewRow(rowData);
}
}
}
// Assign DataTable to DataGridView
DataGridView1.DataSource = myDataTable;
// Copy DataTable columns Caption property
// to DataGridView HeaderText property
foreach (DataColumn dc in myDataTable.Columns)
{
DataGridView1.Columns[dc.ColumnName].HeaderText = dc.Caption;
}
}
P.S. To get all the data OUT of the gridview and back INTO a datatable
DateTable tbl = Gridview1.DataSource as DataTable;
private const int WS_HSCROLL = 0x100000;
private const int WS_VSCROLL = 0x200000;
private const int GWL_STYLE = (-16);
[System.Runtime.InteropServices.DllImport("user32",
CharSet = System.Runtime.InteropServices.CharSet.Auto)]
private static extern int GetWindowLong(IntPtr hwnd, int nIndex);
internal static bool IsVerticalScrollBarVisible(Control ctrl)
{
if (!ctrl.IsHandleCreated)
return false;
return (GetWindowLong(ctrl.Handle, GWL_STYLE) & WS_VSCROLL) != 0;
}
internal static bool IsHorizontalScrollBarVisible(Control ctrl)
{
if (!ctrl.IsHandleCreated)
return false;
return (GetWindowLong(ctrl.Handle, GWL_STYLE) & WS_HSCROLL) != 0;
}
Usage example:
if (IsVerticalScrollBarVisible(ListView1))
DoSomething;
CAT.NET is a snap-in to the Visual Studio IDE that helps you identify security flaws within a managed code (C#, Visual Basic .NET, J#) application you are developing. It does so by scanning the binary and/or assembly of the application, and tracing the data flow among its statements, methods, and assemblies. This includes indirect data types such as property assignments and instance tainting operations. The engine works by reading the target assembly and all reference assemblies used in the application — module-by-module — and then analyzing all of the methods contained within each. It finally displays the issues its finds in a list that you can use to jump directly to the places in your application’s source code where those issues were found. The following rules are currently support by this version of the tool. – Cross Site Scripting – SQL Injection – Process Command Injection – File Canonicalization – Exception Information – LDAP Injection – XPATH Injection – Redirection to User Controlled Site
http://www.microsoft.com/downloads/details.aspx?FamilyId=0178e2ef-9da8-445e-9348-c93f24cc9f9d&displaylang=en
//For example, make a string with 32 “a” chars.
string test = new string('a', 32 );
Yesterday Microsoft has published security bulletin about vulnerability in SQL Server series. I had to again search how to find my SQL Server service pack number. To avoid this in the future I will post it here :)
SELECT
SERVERPROPERTY ('ProductVersion'),
SERVERPROPERTY ('ProductLevel'),
SERVERPROPERTY ('Edition'),
SERVERPROPERTY ('ServerName')
To insert into database NULL value from C# code we can’t use just simple declaration for empty string like string textToInsert = “”; This will insert into database just empty string instead NULL value.
To handle this case you have to use special SQL type: System.Data.SqlTypes.SqlString.Null
Dummy example (do nothing special but shows the way) below:
using (SqlConnection cn = new SqlConnection(connectionString))
{
string sqlText = "stored_procedure_name";
using (SqlCommand cmd = new SqlCommand(sqlText, cn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@TaskType", SqlDbType.NVarChar, 255).Value = System.Data.SqlTypes.SqlString.Null;
try
{
cn.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException sqlEx)
{
}
}
}
System.Data.SqlTypes namespace has all SQL Server types so if you need to insert NULL value then use suitable one for your column data type.
frmOptions formOptions = new frmOptions();
// open form only if is not open already
bool isFormOpen = false;
// iterate through all open forms
foreach (Form frm in Application.OpenForms)
{
if (frm is frmOptions)
{
// open already so just bring it to the front
frm.BringToFront();
isFormOpen = true;
break;
}
}
if (!isFormOpen)
// not open so show it
formOptions.Show();
else
formOptions.Dispose();
Source:
http://stackoverflow.com/questions/38039/how-can-i-get-the-datetime-for-the-start-of-the-week
If you have similiar solution than test it for 2009-02-01 (sunday) date (yyyy-mm-dd). Correct result should be 2009-01-26.
Extension method:
public static class DateTimeExtensions
{
public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek)
{
int diff = dt.DayOfWeek - startOfWeek;
if (diff < 0)
{
diff += 7;
}
return dt.AddDays(-1 * diff).Date;
}
}
Example of use
DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Monday);
DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Sunday);