WinForms tips and tricks
WinForms vs. WPF
WPF is newer and Windows Forms is still a perfectly valid choice.
WPF uses XAML, an XML file that you can edit to edit control design and style.
In WPF the ListView control has DataSource (not the case of WinForms).
WPF may need more memory than WinForms but it should run faster as it uses DirectX controls.
WinForms uses common Windows controls. WinForms cannot design web apps. WinForms is good for developing default Windows style applications.
WinForms controls
Following controls will be covered:
- ComboBox
- ListBox … (todo)
- ListView
- DataGridView
- TextBox … (todo)
- Image … (todo)
ComboBox control
Number of objects in combobox:
comboBox.Items.Count
To add an item just with text and without a hidden (key) value:
comboBox1.Items.Add("text");
To remove a single selected item:
comboBox1.Items.RemoveAt(0);
comboBox1.Items.Remove(comboBox1.SelectedItem);
comboBox1.Items.Remove("Tokyo");
To remove all items:
comboBox1.Items.Clear();
Problem with Items.Add()
is you cannot add both Value and Text for a single combobox item unless you create your special class:
public class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
However, the upper method may not be the simplest way to go since there is a way to fill combobox via DataSource where you provide the table as DataSource:
comboBox1.DataSource = dt; // DataTable
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "name";
In the previous example the table has columns id and name.
To read the selected value from combobox (both text and hidden key) you can use this code:
comboBox1.Text // selected text
comboBox1.SelectedValue // selected value
ListView control
ListView
controls a table of data. Each row inside ListView
is called ListViewItem
.
Sweet code for list view control:
listView1.FullRowSelect = true;
listView1.MultiSelect = false;
listView1.GridLines = true;
listView1.BackColor = System.Drawing.Color.Orange;
listView1.ForeColor = System.Drawing.Color.Black;
// adding data to the list view
string query = "select * from Reader";
DataTable dt = DB.ExecuteQuery(query);
// adding each line separately
foreach (DataRow row in dt.Rows)
{
Trace.WriteLine("row in foreach");
// ListViewItem means the single row
ListViewItem item = new ListViewItem(row[0].ToString());
item.SubItems.Add(row[1].ToString());
item.SubItems.Add(row[2].ToString());
item.SubItems.Add(row[3].ToString());
item.SubItems.Add(row[4].ToString());
item.SubItems.Add(row[5].ToString());
item.SubItems.Add(row[6].ToString());
listView1.Items.Add(item);
}
Alternative adding a new ListViewItem
:
string[] data = new string[7];
data[0] = tbxID.Text;
data[1] = tbxPIN.Text; // tbxSSN.Text
data[2] = tbxName.Text;
data[3] = tbxLastName.Text;
data[4] = tbxCity.Text;
data[5] = tbxAddress.Text;
data[6] = tbxPhone.Text;
ListViewItem lvItem = new ListViewItem(data);
listView1.Items.Add(lvItem);
DataGridView
Sweet code for DataGridView
class:
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
dataGridView1.MultiSelect = false;
dataGridView1.RowHeadersVisible = false;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
dataGridView1.AllowUserToResizeRows = false;
string query = "select BookID as Cipher, UDK, ISBN, Name from Books";
dataGridView1.DataSource = DB.ExecuteQuery(query);
dataGridView1.ClearSelection();
You may notice now the DataGridView
control looks the same as the default style for ListView
control.
Check if anything is selected:
if (dataGridView1.SelectedCells.Count > 0)
{
// do something with the selected row(s)
}
Get the index of currently selected row:
int index = dataGridView1.CurrentCell.RowIndex
index = dataGridView1.SelectedRows[0].Index;
Get selected row first cell value:
DataGridViewRow row = dg.CurrentRow;
string text = row.Cells[0].Value.ToString();
Alternative to the previous method:
int ind = dgv.CurrentRow.Index
string text = dgv.Rows[ind].Cells[0].Value.ToString;
Messaging in WinForms
MessageBox.Show("Message", "Title");
Trace.WriteLine("Message");
Trace
is the best way to print something to the output. using System.Diagnostics;
is needed when you use the Trace
class.
Adding .mdf
file to your project
MDF stands for Master Database File.
Few ways to navigate your data:
- SQL Server Object Explorer
- Server Explorer
- Data Sources (SHIFT + ALT + D)
To add .mdf
file to your solution:
-
Project > Add New Item > Service-based Database
-
Open Server Explorer and you will find new Database1.mdf file
-
Right click Tables and select New Query and then execute any SQL query from your SQL file
…
tags: winforms - controls - C# & category: csharp