listbox refresh automatically using filewatcher C# - c#

Ok I have been at this for awhile. I have a program that monitors one file "lab.txt" for changes, and when it changes, I want it to reload the contents of the file into a listbox that is displayed. I can get it to display and tell me when there is a change, but I cant get the listbox to refresh. any help would be apprieciated. There is alot of code not being used now because I have been trying different methods so please disregard.
namespace FileChangeNotifier
{
public partial class frmNotifier : Form
{
private StringBuilder m_Sb;
private bool m_bDirty;
private System.IO.FileSystemWatcher m_Watcher;
private bool m_bIsWatching;
public static string txtPath = "E:/lab.txt";
List<string> list;
BindingList<string> bindingList;
public frmNotifier()
{
InitializeComponent();
m_Sb = new StringBuilder();
m_bDirty = false;
m_bIsWatching = false;
//BindingSource bindingSource = (BindingSource)listBox1.DataSource;
// List SourceList = (List)bindingSource.List;
list= new List<string>(File.ReadLines(txtPath));
bindingList = new BindingList<string>(list);
listBox1.DataSource = bindingList;
//listBox1.SelectedIndex = -1;
m_bIsWatching = true;
btnWatchFile.BackColor = Color.Red;
m_Watcher = new System.IO.FileSystemWatcher();
m_Watcher.Filter = "lab.txt";
m_Watcher.Path = "E:\\";
m_Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
m_Watcher.Changed += new FileSystemEventHandler(OnChanged);
m_Watcher.Created += new FileSystemEventHandler(OnChanged);
m_Watcher.Deleted += new FileSystemEventHandler(OnChanged);
m_Watcher.Renamed += new RenamedEventHandler(OnRenamed);
m_Watcher.EnableRaisingEvents = true;
}
private void btnWatchFile_Click(object sender, EventArgs e)
{
if (m_bIsWatching)
{
m_bIsWatching = false;
m_Watcher.EnableRaisingEvents = false;
m_Watcher.Dispose();
btnWatchFile.BackColor = Color.LightSkyBlue;
btnWatchFile.Text = "Start Watching";
}
else
{
m_bIsWatching = true;
btnWatchFile.BackColor = Color.Red;
btnWatchFile.Text = "Stop Watching";
m_Watcher = new System.IO.FileSystemWatcher();
//if (rdbDir.Checked)
//
m_Watcher.Filter = "lab.txt";
m_Watcher.Path = "E:\\";
//
/*lse
{
m_Watcher.Filter = txtFile.Text.Substring(txtFile.Text.LastIndexOf('\\') + 1);
m_Watcher.Path = txtFile.Text.Substring(0, txtFile.Text.Length - m_Watcher.Filter.Length);
}
if (chkSubFolder.Checked)
{
m_Watcher.IncludeSubdirectories = true;
}*/
m_Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
m_Watcher.Changed += new FileSystemEventHandler(OnChanged);
m_Watcher.Created += new FileSystemEventHandler(OnChanged);
m_Watcher.Deleted += new FileSystemEventHandler(OnChanged);
m_Watcher.Renamed += new RenamedEventHandler(OnRenamed);
m_Watcher.EnableRaisingEvents = true;
}
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
if (!m_bDirty)
{
m_Sb.Remove(0, m_Sb.Length);
m_Sb.Append(e.FullPath);
m_Sb.Append(" ");
m_Sb.Append(e.ChangeType.ToString());
m_Sb.Append(" ");
m_Sb.Append(DateTime.Now.ToString());
m_bDirty = true;
}
}
private void OnRenamed(object sender, RenamedEventArgs e)
{
if (!m_bDirty)
{
m_Sb.Remove(0, m_Sb.Length);
m_Sb.Append(e.OldFullPath);
m_Sb.Append(" ");
m_Sb.Append(e.ChangeType.ToString());
m_Sb.Append(" ");
m_Sb.Append("to ");
m_Sb.Append(e.Name);
m_Sb.Append(" ");
m_Sb.Append(DateTime.Now.ToString());
m_bDirty = true;
}
}
private void tmrEditNotify_Tick(object sender, EventArgs e)
{
if (m_bDirty)
{
lstNotification.BeginUpdate();
lstNotification.Items.Add(m_Sb.ToString());
lstNotification.EndUpdate();
m_bDirty = false;
}
}
private void btnBrowseFile_Click(object sender, EventArgs e)
{
}
private void btnLog_Click(object sender, EventArgs e)
{
DialogResult resDialog = dlgSaveFile.ShowDialog();
if (resDialog.ToString() == "OK")
{
FileInfo fi = new FileInfo(dlgSaveFile.FileName);
StreamWriter sw = fi.CreateText();
foreach (string sItem in lstNotification.Items)
{
sw.WriteLine(sItem);
}
sw.Close();
}
}
public void bindData()
{
listBox1.DataSource = null;
bindingList=null;
list = new List<string>(File.ReadLines(txtPath));
bindingList = new BindingList<string>(list);
listBox1.DataSource = bindingList;
}
private void Execute(object sender, EventArgs e)
{
string task = TaskQue.Pop();
//execute task;
listBox1.DataSource = TaskQue.GetTasks();
}
private void AddTask(object sender, EventArgs e)
{
TaskQue.Push(listBox1.Text);
listBox1.DataSource = TaskQue.GetTasks();
}
private void txtFile_TextChanged(object sender, EventArgs e)
{
}
private void lstNotification_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
public class TaskQue
{
public static string txtPath = "E:/lab.txt";
public static string Pop()
{
StreamReader sr = new StreamReader(txtPath);
string result = sr.ReadLine();
string remaining = sr.ReadToEnd();
sr.Close();
StreamWriter sw = new StreamWriter(txtPath, false);
sw.Write(remaining);
sw.Close();
return result;
}
public static void Push(string s)
{
StreamWriter sw = new StreamWriter(txtPath, true);
sw.WriteLine(s);
sw.Close();
}
public static IEnumerable<string> GetTasks()
{
return new List<string>(File.ReadLines(txtPath));
}
}
}
EDIT:
This is what I have changed and still no go
private void OnChanged(object sender, FileSystemEventArgs e)
{
if (!m_bDirty)
{
m_Sb.Remove(0, m_Sb.Length);
m_Sb.Append(e.FullPath);
m_Sb.Append(" ");
m_Sb.Append(e.ChangeType.ToString());
m_Sb.Append(" ");
m_Sb.Append(DateTime.Now.ToString());
m_bDirty = true;
list = new List<string>(File.ReadAllLines(txtPath));
bindingList = new BindingList<string>(list);
listBox1.DataSource = bindingList;
}
}
This is my Form.Designer.cs file
namespace FileChangeNotifier
{
partial class frmNotifier
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.btnWatchFile = new System.Windows.Forms.Button();
this.lstNotification = new System.Windows.Forms.ListBox();
this.label3 = new System.Windows.Forms.Label();
this.tmrEditNotify = new System.Windows.Forms.Timer(this.components);
this.dlgOpenFile = new System.Windows.Forms.OpenFileDialog();
this.dlgOpenDir = new System.Windows.Forms.FolderBrowserDialog();
this.btnLog = new System.Windows.Forms.Button();
this.dlgSaveFile = new System.Windows.Forms.SaveFileDialog();
this.listBox1 = new System.Windows.Forms.ListBox();
this.frmNotifierBindingSource = new System.Windows.Forms.BindingSource(this.components);
((System.ComponentModel.ISupportInitialize)(this.frmNotifierBindingSource)).BeginInit();
this.SuspendLayout();
//
// btnWatchFile
//
this.btnWatchFile.BackColor = System.Drawing.Color.LightSkyBlue;
this.btnWatchFile.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnWatchFile.ForeColor = System.Drawing.SystemColors.ControlText;
this.btnWatchFile.Location = new System.Drawing.Point(15, 165);
this.btnWatchFile.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnWatchFile.Name = "btnWatchFile";
this.btnWatchFile.Size = new System.Drawing.Size(159, 28);
this.btnWatchFile.TabIndex = 4;
this.btnWatchFile.Text = "Start Watching";
this.btnWatchFile.UseVisualStyleBackColor = false;
this.btnWatchFile.Click += new System.EventHandler(this.btnWatchFile_Click);
//
// lstNotification
//
this.lstNotification.FormattingEnabled = true;
this.lstNotification.ItemHeight = 16;
this.lstNotification.Location = new System.Drawing.Point(15, 228);
this.lstNotification.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.lstNotification.Name = "lstNotification";
this.lstNotification.Size = new System.Drawing.Size(613, 276);
this.lstNotification.TabIndex = 5;
this.lstNotification.SelectedIndexChanged += new System.EventHandler(this.lstNotification_SelectedIndexChanged);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label3.Location = new System.Drawing.Point(15, 208);
this.label3.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(158, 17);
this.label3.TabIndex = 6;
this.label3.Text = "Change Notifications";
//
// tmrEditNotify
//
this.tmrEditNotify.Enabled = true;
this.tmrEditNotify.Tick += new System.EventHandler(this.tmrEditNotify_Tick);
//
// dlgOpenDir
//
this.dlgOpenDir.RootFolder = System.Environment.SpecialFolder.MyComputer;
//
// btnLog
//
this.btnLog.BackColor = System.Drawing.SystemColors.Highlight;
this.btnLog.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnLog.Location = new System.Drawing.Point(15, 519);
this.btnLog.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.btnLog.Name = "btnLog";
this.btnLog.Size = new System.Drawing.Size(159, 28);
this.btnLog.TabIndex = 9;
this.btnLog.Text = "Dump To Log";
this.btnLog.UseVisualStyleBackColor = false;
this.btnLog.Click += new System.EventHandler(this.btnLog_Click);
//
// dlgSaveFile
//
this.dlgSaveFile.DefaultExt = "log";
this.dlgSaveFile.Filter = "LogFiles|*.log";
//
// listBox1
//
this.listBox1.DataSource = this.frmNotifierBindingSource;
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 16;
this.listBox1.Location = new System.Drawing.Point(807, 74);
this.listBox1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(277, 388);
this.listBox1.TabIndex = 10;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// frmNotifierBindingSource
//
this.frmNotifierBindingSource.DataSource = typeof(FileChangeNotifier.frmNotifier);
//
// frmNotifier
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1207, 562);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.btnLog);
this.Controls.Add(this.label3);
this.Controls.Add(this.lstNotification);
this.Controls.Add(this.btnWatchFile);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.MaximizeBox = false;
this.Name = "frmNotifier";
this.Text = "File/Directory Change Notifier";
((System.ComponentModel.ISupportInitialize)(this.frmNotifierBindingSource)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnWatchFile;
private System.Windows.Forms.ListBox lstNotification;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Timer tmrEditNotify;
private System.Windows.Forms.OpenFileDialog dlgOpenFile;
private System.Windows.Forms.FolderBrowserDialog dlgOpenDir;
private System.Windows.Forms.Button btnLog;
private System.Windows.Forms.SaveFileDialog dlgSaveFile;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.BindingSource frmNotifierBindingSource;
}
}

Your current code is not compiled. ReadLine should be ReadAllLines.
And there is no code that will set the listbox value, so the listbox value is not changed.
Try to using this code:
private void OnChanged(object sender, FileSystemEventArgs e)
{
if (!m_bDirty)
{
m_Sb.Remove(0, m_Sb.Length);
m_Sb.Append(e.FullPath);
m_Sb.Append(" ");
m_Sb.Append(e.ChangeType.ToString());
m_Sb.Append(" ");
m_Sb.Append(DateTime.Now.ToString());
m_bDirty = true;
list = new List<string>(File.ReadAllLines(txtPath));
bindingList = new BindingList<string>(list);
listBox1.DataSource = bindingList;
}
}
UPDATE
After check your full code, seems you've wrong in this code (frmNotifier.cs):
m_bIsWatching = true;
That code will be cause your FileWatcher event handler not registered.
Try to debug it..

Related

Having trouble displaying a list of transactions to a list box in VisualStudios2019 winforms C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 13 days ago.
Improve this question
I know I'm butchering the transaction class. I just have no idea what to do. Nothing gets displayed when I run the form and hit add new transaction. I appreciate any help here's my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AccountBalance
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnNewTran_Click(object sender, EventArgs e)
{
string strTransactionType = "";
if (rbDeposit.Checked)
{
strTransactionType = rbDeposit.Text;
}
else
{
strTransactionType = rbWithdrawal.Text;
}
Transaction newTransaction = new Transaction();
newTransaction.Amount = decimal.Parse(txtTAmount.Text);
newTransaction.Type = strTransactionType;
newTransaction.Date = txtTDate.Text;
lstTransactions.Items.Add(newTransaction);
decimal decBalance = decimal.Parse(lblCBalance.Text);
if (newTransaction.Type == "Deposit")
{
decBalance += newTransaction.Amount;
lblCBalance.Text = decBalance.ToString("c");
}
else
{
decBalance -= newTransaction.Amount;
lblCBalance.Text = decBalance.ToString("c");
}
if (decBalance < 0)
{
lblCBalance.ForeColor = Color.Red;
}
rbDeposit.Checked = false;
rbWithdrawal.Checked = false;
txtTAmount.Clear();
txtTDate.Clear();
txtTAmount.Focus();
}
private void btnRemTran_Click(object sender, EventArgs e)
{
int intSelectedIndex = lstTransactions.SelectedIndex;
Transaction selectedTransaction = (Transaction)lstTransactions.SelectedItem;
lstTransactions.Items.RemoveAt(intSelectedIndex);
decimal decBalance = decimal.Parse(lblCBalance.Text);
if (selectedTransaction.Type == "Deposit")
{
decBalance -= selectedTransaction.Amount;
lblCBalance.Text = decBalance.ToString("c");
}
else
{
decBalance += selectedTransaction.Amount;
lblCBalance.Text = decBalance.ToString("c");
}
if (decBalance >= 0)
{
lblCBalance.ForeColor = Color.Black;
}
}
private void btnClear_Click(object sender, EventArgs e)
{
rbDeposit.Checked = false;
rbWithdrawal.Checked = false;
txtTAmount.Clear();
txtTDate.Clear();
txtTAmount.Focus();
}
private void lstTransactions_Click(object sender, EventArgs e)
{
Transaction selectedTransaction = (Transaction)lstTransactions.SelectedItem;
if (selectedTransaction.Type == "Deposit")
{
rbDeposit.Checked = true;
}
else
{
rbWithdrawal.Checked = true;
}
txtTAmount.Text = selectedTransaction.Amount.ToString("c");
txtTDate.Text = selectedTransaction.Date;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
public class Transaction
{
internal decimal Amount;
internal string Type;
internal string Date;
}
}
namespace AccountBalance
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lblTDate = new System.Windows.Forms.Label();
this.txtTDate = new System.Windows.Forms.TextBox();
this.lblTAmount = new System.Windows.Forms.Label();
this.txtTAmount = new System.Windows.Forms.TextBox();
this.lblTType = new System.Windows.Forms.Label();
this.rbDeposit = new System.Windows.Forms.RadioButton();
this.rbWithdrawal = new System.Windows.Forms.RadioButton();
this.rbServFee = new System.Windows.Forms.RadioButton();
this.lblPayee = new System.Windows.Forms.Label();
this.txtPayee = new System.Windows.Forms.TextBox();
this.lblCheckNum = new System.Windows.Forms.Label();
this.txtCheckNum = new System.Windows.Forms.TextBox();
this.lblCBalance = new System.Windows.Forms.Label();
this.txtCBalance = new System.Windows.Forms.TextBox();
this.lstTransactions = new System.Windows.Forms.ListBox();
this.btnNewTran = new System.Windows.Forms.Button();
this.btnRemTran = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblTDate
//
this.lblTDate.AutoSize = true;
this.lblTDate.Location = new System.Drawing.Point(12, 9);
this.lblTDate.Name = "lblTDate";
this.lblTDate.Size = new System.Drawing.Size(104, 13);
this.lblTDate.TabIndex = 0;
this.lblTDate.Text = "Date of Transaction:";
//
// txtTDate
//
this.txtTDate.Location = new System.Drawing.Point(15, 25);
this.txtTDate.Name = "txtTDate";
this.txtTDate.Size = new System.Drawing.Size(100, 20);
this.txtTDate.TabIndex = 1;
//
// lblTAmount
//
this.lblTAmount.AutoSize = true;
this.lblTAmount.Location = new System.Drawing.Point(263, 9);
this.lblTAmount.Name = "lblTAmount";
this.lblTAmount.Size = new System.Drawing.Size(105, 13);
this.lblTAmount.TabIndex = 0;
this.lblTAmount.Text = "Transaction Amount:";
//
// txtTAmount
//
this.txtTAmount.Location = new System.Drawing.Point(266, 25);
this.txtTAmount.Name = "txtTAmount";
this.txtTAmount.Size = new System.Drawing.Size(100, 20);
this.txtTAmount.TabIndex = 2;
//
// lblTType
//
this.lblTType.AutoSize = true;
this.lblTType.Location = new System.Drawing.Point(473, 9);
this.lblTType.Name = "lblTType";
this.lblTType.Size = new System.Drawing.Size(93, 13);
this.lblTType.TabIndex = 0;
this.lblTType.Text = "Transaction Type:";
//
// rbDeposit
//
this.rbDeposit.AutoSize = true;
this.rbDeposit.Location = new System.Drawing.Point(481, 28);
this.rbDeposit.Name = "rbDeposit";
this.rbDeposit.Size = new System.Drawing.Size(61, 17);
this.rbDeposit.TabIndex = 3;
this.rbDeposit.TabStop = true;
this.rbDeposit.Text = "Deposit";
this.rbDeposit.UseVisualStyleBackColor = true;
//
// rbWithdrawal
//
this.rbWithdrawal.AutoSize = true;
this.rbWithdrawal.Location = new System.Drawing.Point(481, 51);
this.rbWithdrawal.Name = "rbWithdrawal";
this.rbWithdrawal.Size = new System.Drawing.Size(78, 17);
this.rbWithdrawal.TabIndex = 4;
this.rbWithdrawal.TabStop = true;
this.rbWithdrawal.Text = "Withdrawal";
this.rbWithdrawal.UseVisualStyleBackColor = true;
//
// rbServFee
//
this.rbServFee.AutoSize = true;
this.rbServFee.Location = new System.Drawing.Point(481, 74);
this.rbServFee.Name = "rbServFee";
this.rbServFee.Size = new System.Drawing.Size(82, 17);
this.rbServFee.TabIndex = 5;
this.rbServFee.TabStop = true;
this.rbServFee.Text = "Service Fee";
this.rbServFee.UseVisualStyleBackColor = true;
//
// lblPayee
//
this.lblPayee.AutoSize = true;
this.lblPayee.Location = new System.Drawing.Point(12, 53);
this.lblPayee.Name = "lblPayee";
this.lblPayee.Size = new System.Drawing.Size(40, 13);
this.lblPayee.TabIndex = 0;
this.lblPayee.Text = "Payee:";
//
// txtPayee
//
this.txtPayee.Location = new System.Drawing.Point(15, 71);
this.txtPayee.Name = "txtPayee";
this.txtPayee.Size = new System.Drawing.Size(230, 20);
this.txtPayee.TabIndex = 6;
//
// lblCheckNum
//
this.lblCheckNum.AutoSize = true;
this.lblCheckNum.Location = new System.Drawing.Point(263, 53);
this.lblCheckNum.Name = "lblCheckNum";
this.lblCheckNum.Size = new System.Drawing.Size(81, 13);
this.lblCheckNum.TabIndex = 0;
this.lblCheckNum.Text = "Check Number:";
//
// txtCheckNum
//
this.txtCheckNum.Location = new System.Drawing.Point(266, 71);
this.txtCheckNum.Name = "txtCheckNum";
this.txtCheckNum.Size = new System.Drawing.Size(100, 20);
this.txtCheckNum.TabIndex = 7;
//
// lblCBalance
//
this.lblCBalance.AutoSize = true;
this.lblCBalance.Location = new System.Drawing.Point(12, 109);
this.lblCBalance.Name = "lblCBalance";
this.lblCBalance.Size = new System.Drawing.Size(86, 13);
this.lblCBalance.TabIndex = 0;
this.lblCBalance.Text = "Current Balance:";
//
// txtCBalance
//
this.txtCBalance.Location = new System.Drawing.Point(145, 102);
this.txtCBalance.Name = "txtCBalance";
this.txtCBalance.ReadOnly = true;
this.txtCBalance.Size = new System.Drawing.Size(100, 20);
this.txtCBalance.TabIndex = 0;
//
// lstTransactions
//
this.lstTransactions.FormattingEnabled = true;
this.lstTransactions.Location = new System.Drawing.Point(15, 125);
this.lstTransactions.Name = "lstTransactions";
this.lstTransactions.Size = new System.Drawing.Size(439, 316);
this.lstTransactions.TabIndex = 0;
//
// btnNewTran
//
this.btnNewTran.Location = new System.Drawing.Point(460, 171);
this.btnNewTran.Name = "btnNewTran";
this.btnNewTran.Size = new System.Drawing.Size(99, 23);
this.btnNewTran.TabIndex = 8;
this.btnNewTran.Text = "New Transaction";
this.btnNewTran.UseVisualStyleBackColor = true;
//
// btnRemTran
//
this.btnRemTran.Location = new System.Drawing.Point(460, 252);
this.btnRemTran.Name = "btnRemTran";
this.btnRemTran.Size = new System.Drawing.Size(99, 23);
this.btnRemTran.TabIndex = 9;
this.btnRemTran.Text = "Remove";
this.btnRemTran.UseVisualStyleBackColor = true;
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(460, 333);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(99, 23);
this.btnClear.TabIndex = 10;
this.btnClear.Text = "Clear";
this.btnClear.UseVisualStyleBackColor = true;
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(460, 414);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(99, 23);
this.btnExit.TabIndex = 11;
this.btnExit.Text = "Exit";
this.btnExit.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(578, 450);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnRemTran);
this.Controls.Add(this.btnNewTran);
this.Controls.Add(this.lstTransactions);
this.Controls.Add(this.txtCBalance);
this.Controls.Add(this.lblCBalance);
this.Controls.Add(this.txtCheckNum);
this.Controls.Add(this.lblCheckNum);
this.Controls.Add(this.txtPayee);
this.Controls.Add(this.lblPayee);
this.Controls.Add(this.rbServFee);
this.Controls.Add(this.rbWithdrawal);
this.Controls.Add(this.rbDeposit);
this.Controls.Add(this.lblTType);
this.Controls.Add(this.txtTAmount);
this.Controls.Add(this.lblTAmount);
this.Controls.Add(this.txtTDate);
this.Controls.Add(this.lblTDate);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label lblTDate;
private System.Windows.Forms.TextBox txtTDate;
private System.Windows.Forms.Label lblTAmount;
private System.Windows.Forms.TextBox txtTAmount;
private System.Windows.Forms.Label lblTType;
private System.Windows.Forms.RadioButton rbDeposit;
private System.Windows.Forms.RadioButton rbWithdrawal;
private System.Windows.Forms.RadioButton rbServFee;
private System.Windows.Forms.Label lblPayee;
private System.Windows.Forms.TextBox txtPayee;
private System.Windows.Forms.Label lblCheckNum;
private System.Windows.Forms.TextBox txtCheckNum;
private System.Windows.Forms.Label lblCBalance;
private System.Windows.Forms.TextBox txtCBalance;
private System.Windows.Forms.ListBox lstTransactions;
private System.Windows.Forms.Button btnNewTran;
private System.Windows.Forms.Button btnRemTran;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.Button btnExit;
}
}
I pretty much tried to use quick fixes and watched some random videos and came up with nothing.
Don't add your items to the ListBox directly. Create an appropriate list and bind that to the ListBox, then add your items to that list. If you use a BindingList<Transaction> then adding an item to the list will automatically update the bound control, which will not happen with a List<Transaction>.
var transactions = new BindingList<Transaction>();
myListBox.DataSource = transactions;
By default, when you add an item to a ListBox, ToString is called on that object and the result displayed. If you don't override the ToString method, that will just be the name of the type. You can set the DisplayMember of the ListBox and the specified property value will be displayed, e.g.
var transactions = new BindingList<Transaction>();
myListBox.DisplayMember = nameof(Transaction.Amount);
myListBox.DataSource = transactions;
Note that only properties can be specified as the DisplayMember or ValueMember. Fields will not work.
If you want some custom text displayed then you need to override the ToString method and have it return the desired text.

Get items from a hosted windows form designer in c#

I was trying to create a custom form designer with this article:
http://www.developerfusion.com/article/4351/hosting-windows-forms-designers/
that lets you create a custom form, visual studio alike.
but I can't figure out how to get all the controls in the created form, does someone know how to do this?
I used this code from the article:
using System;
using System.Drawing;
using System.Drawing.Design;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Data;
namespace Hosting
{
public class frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.PropertyGrid propertyGrid;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel pnlViewHost;
private System.Windows.Forms.Splitter splitter2;
private ToolboxService lstToolbox;
private System.Windows.Forms.Label lblSelectedComponent;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem mnuDelete;
private MenuItem menuItem2;
private IContainer components;
public frmMain()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
Initialize();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.propertyGrid = new System.Windows.Forms.PropertyGrid();
this.splitter1 = new System.Windows.Forms.Splitter();
this.panel1 = new System.Windows.Forms.Panel();
this.splitter2 = new System.Windows.Forms.Splitter();
this.lblSelectedComponent = new System.Windows.Forms.Label();
this.pnlViewHost = new System.Windows.Forms.Panel();
this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.mnuDelete = new System.Windows.Forms.MenuItem();
this.lstToolbox = new Hosting.ToolboxService();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// propertyGrid
//
this.propertyGrid.Dock = System.Windows.Forms.DockStyle.Bottom;
this.propertyGrid.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid.Location = new System.Drawing.Point(0, 182);
this.propertyGrid.Name = "propertyGrid";
this.propertyGrid.Size = new System.Drawing.Size(224, 312);
this.propertyGrid.TabIndex = 0;
//
// splitter1
//
this.splitter1.Dock = System.Windows.Forms.DockStyle.Right;
this.splitter1.Location = new System.Drawing.Point(596, 0);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(4, 518);
this.splitter1.TabIndex = 1;
this.splitter1.TabStop = false;
//
// panel1
//
this.panel1.Controls.Add(this.lstToolbox);
this.panel1.Controls.Add(this.splitter2);
this.panel1.Controls.Add(this.propertyGrid);
this.panel1.Controls.Add(this.lblSelectedComponent);
this.panel1.Dock = System.Windows.Forms.DockStyle.Right;
this.panel1.Location = new System.Drawing.Point(600, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(224, 518);
this.panel1.TabIndex = 2;
//
// splitter2
//
this.splitter2.Dock = System.Windows.Forms.DockStyle.Bottom;
this.splitter2.Location = new System.Drawing.Point(0, 178);
this.splitter2.Name = "splitter2";
this.splitter2.Size = new System.Drawing.Size(224, 4);
this.splitter2.TabIndex = 1;
this.splitter2.TabStop = false;
//
// lblSelectedComponent
//
this.lblSelectedComponent.Dock = System.Windows.Forms.DockStyle.Bottom;
this.lblSelectedComponent.Location = new System.Drawing.Point(0, 494);
this.lblSelectedComponent.Name = "lblSelectedComponent";
this.lblSelectedComponent.Size = new System.Drawing.Size(224, 24);
this.lblSelectedComponent.TabIndex = 3;
this.lblSelectedComponent.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// pnlViewHost
//
this.pnlViewHost.BackColor = System.Drawing.SystemColors.Window;
this.pnlViewHost.Dock = System.Windows.Forms.DockStyle.Fill;
this.pnlViewHost.Location = new System.Drawing.Point(0, 0);
this.pnlViewHost.Name = "pnlViewHost";
this.pnlViewHost.Size = new System.Drawing.Size(596, 518);
this.pnlViewHost.TabIndex = 3;
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuDelete,
this.menuItem2});
this.menuItem1.Text = "&Edit";
//
// mnuDelete
//
this.mnuDelete.Index = 0;
this.mnuDelete.Shortcut = System.Windows.Forms.Shortcut.Del;
this.mnuDelete.Text = "&Delete";
this.mnuDelete.Click += new System.EventHandler(this.mnuDelete_Click);
//
// lstToolbox
//
this.lstToolbox.BackColor = System.Drawing.SystemColors.Control;
this.lstToolbox.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.lstToolbox.Dock = System.Windows.Forms.DockStyle.Fill;
this.lstToolbox.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lstToolbox.IntegralHeight = false;
this.lstToolbox.ItemHeight = 16;
this.lstToolbox.Location = new System.Drawing.Point(0, 0);
this.lstToolbox.Name = "lstToolbox";
this.lstToolbox.SelectedCategory = null;
this.lstToolbox.Size = new System.Drawing.Size(224, 178);
this.lstToolbox.Sorted = true;
this.lstToolbox.TabIndex = 2;
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "Save";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
this.ClientSize = new System.Drawing.Size(824, 518);
this.Controls.Add(this.pnlViewHost);
this.Controls.Add(this.splitter1);
this.Controls.Add(this.panel1);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Menu = this.mainMenu1;
this.Name = "frmMain";
this.Text = "Designer Hosting Sample";
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
private ServiceContainer serviceContainer = null;
private MenuCommandService menuService = null;
Control view;
private void Initialize()
{
IDesignerHost host;
Form form;
IRootDesigner rootDesigner;
// Initialise service container and designer host
serviceContainer = new ServiceContainer();
serviceContainer.AddService(typeof(INameCreationService), new NameCreationService());
serviceContainer.AddService(typeof(IUIService), new UIService(this));
host = new DesignerHost(serviceContainer);
// Add toolbox service
serviceContainer.AddService(typeof(IToolboxService), lstToolbox);
lstToolbox.designPanel = pnlViewHost;
PopulateToolbox(lstToolbox);
// Add menu command service
menuService = new MenuCommandService();
serviceContainer.AddService(typeof(IMenuCommandService), menuService);
// Start the designer host off with a Form to design
form = (Form)host.CreateComponent(typeof(Form));
form.TopLevel = false;
form.Text = "Form1";
// Get the root designer for the form and add its design view to this form
rootDesigner = (IRootDesigner)host.GetDesigner(form);
view = (Control)rootDesigner.GetView(ViewTechnology.Default);
view.Dock = DockStyle.Fill;
pnlViewHost.Controls.Add(view);
// Subscribe to the selectionchanged event and activate the designer
ISelectionService s = (ISelectionService)serviceContainer.GetService(typeof(ISelectionService));
s.SelectionChanged += new EventHandler(OnSelectionChanged);
host.Activate();
}
private void PopulateToolbox(IToolboxService toolbox)
{
toolbox.AddToolboxItem(new ToolboxItem(typeof(Button)));
toolbox.AddToolboxItem(new ToolboxItem(typeof(ListView)));
toolbox.AddToolboxItem(new ToolboxItem(typeof(TreeView)));
toolbox.AddToolboxItem(new ToolboxItem(typeof(TextBox)));
toolbox.AddToolboxItem(new ToolboxItem(typeof(Label)));
toolbox.AddToolboxItem(new ToolboxItem(typeof(TabControl)));
toolbox.AddToolboxItem(new ToolboxItem(typeof(OpenFileDialog)));
toolbox.AddToolboxItem(new ToolboxItem(typeof(CheckBox)));
toolbox.AddToolboxItem(new ToolboxItem(typeof(ComboBox)));
toolbox.AddToolboxItem(new ToolboxItem(typeof(GroupBox)));
toolbox.AddToolboxItem(new ToolboxItem(typeof(ImageList)));
toolbox.AddToolboxItem(new ToolboxItem(typeof(Panel)));
toolbox.AddToolboxItem(new ToolboxItem(typeof(ProgressBar)));
toolbox.AddToolboxItem(new ToolboxItem(typeof(ToolBar)));
toolbox.AddToolboxItem(new ToolboxItem(typeof(ToolTip)));
toolbox.AddToolboxItem(new ToolboxItem(typeof(StatusBar)));
}
private void OnSelectionChanged(object sender, System.EventArgs e)
{
ISelectionService s = (ISelectionService)serviceContainer.GetService(typeof(ISelectionService));
object[] selection;
if (s.SelectionCount == 0)
propertyGrid.SelectedObject = null;
else
{
selection = new object[s.SelectionCount];
s.GetSelectedComponents().CopyTo(selection, 0);
propertyGrid.SelectedObjects = selection;
}
if (s.PrimarySelection == null)
lblSelectedComponent.Text = "";
else
{
IComponent component = (IComponent)s.PrimarySelection;
lblSelectedComponent.Text = component.Site.Name + " (" + component.GetType().Name + ")";
}
}
private void mnuDelete_Click(object sender, System.EventArgs e)
{
menuService.GlobalInvoke(StandardCommands.Delete);
}
}
}
Here is what I tried:
// And then i tried this to get all the controls and write them to a python tkinter file:
private void menuItem2_Click(object sender, EventArgs e)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter("output.py");
sw.WriteLine("from tkinter import *\n");
sw.WriteLine("# Main form");
sw.WriteLine(this.Name + " = Tk()\n");
foreach (IComponent b in components.Components)
{
MessageBox.Show(b.GetType().ToString());
if (b.GetType() == typeof(Button))
{
Button but = (Button)b;
sw.WriteLine("# " + but.Name);
sw.WriteLine(but.Name + " = Button(" + this.Name + #"fg=""" + but.ForeColor.Name + #"""bg=""" + but.BackColor.Name + #"""" + ")");
}
}
sw.Close();
}
thanks in advance.
You get add control by use for loop.
foreach(Control c in form.Controls)
{
//You can save all attribute for each control on file or database
//c.Left, c.Top, c.Name,...
}
When you reload control in to form, use by code:
TextBox textbox = (TextBox)host.CreateComponent(typeof(TextBox));
textbox.Left = your value save before;
textbox.Top = your value save before;
textbox.Text = your text save before;
form.Controls.Add(textbox);
..Similar for orther controls

Getting Error 24 'CapSample.MW' does not have a suitable static Main method

I am working on a C# winforms app where I have a solution that contains three projects. When build the solution I face the above specified error. I googled it and found that there should be a main method which is the entry point for every program. However i included program.cs file in my solution and it does have main method. How can I add main method for all three projects in the solution. currently its giving error only for one project why don't it throw errors for other two projects. Any suggestions to fix this will be really appreciated. Here is the code of the project:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using DirectX.Capture;
namespace CapSample
{
public partial class MW : System.Windows.Forms.Form
{
#region " Código generado por el Diseñador de Windows Forms "
public MW() : base()
{
//El Diseñador de Windows Forms requiere esta llamada.
InitializeComponent();
//Call to AddCam to select an available camera
AddCam AddCamera = new AddCam();
AddCamera.ShowDialog(this);
ModCap.CaptureInformation.CaptureInfo.PreviewWindow = this.videoBoard;
//Define RefreshImage as event handler of FrameCaptureComplete
ModCap.CaptureInformation.CaptureInfo.FrameCaptureComplete += RefreshImage;
ModCap.CaptureInformation.Counter = 1;
ModCap.CaptureInformation.CounterFrames = 1;
this.Show();
//Initialization of ConfWindow
ModCap.CaptureInformation.ConfWindow = new CW();
ModCap.CaptureInformation.ConfWindow.Refresh();
ModCap.CaptureInformation.ConfWindow.Show();
}
//Form reemplaza a Dispose para limpiar la lista de componentes.
protected override void Dispose(bool disposing)
{
if (disposing) {
if ((components != null)) {
components.Dispose();
}
}
base.Dispose(disposing);
}
//Requerido por el Diseñador de Windows Forms
private System.ComponentModel.IContainer components;
//NOTA: el Diseñador de Windows Forms requiere el siguiente procedimiento
//Puede modificarse utilizando el Diseñador de Windows Forms.
//No lo modifique con el editor de código.
internal System.Windows.Forms.Panel videoBoard;
private System.Windows.Forms.Button withEventsField_cmdFrame;
/* internal System.Windows.Forms.Button cmdFrame {
get { return withEventsField_cmdFrame; }
set {
if (withEventsField_cmdFrame != null) {
withEventsField_cmdFrame.Click -= cmdFrame_Click;
}
withEventsField_cmdFrame = value;
if (withEventsField_cmdFrame != null) {
withEventsField_cmdFrame.Click += cmdFrame_Click;
}
}
}*/
private System.Windows.Forms.Button withEventsField_cmdStart;
internal System.Windows.Forms.Button cmdStart {
get { return withEventsField_cmdStart; }
set {
if (withEventsField_cmdStart != null) {
withEventsField_cmdStart.Click -= cmdStart_Click;
}
withEventsField_cmdStart = value;
if (withEventsField_cmdStart != null) {
withEventsField_cmdStart.Click += cmdStart_Click;
}
}
}
private Button btnStart;
private Button btnFrame;
private Button cmdFrame;
private System.Windows.Forms.Button withEventsField_cmdStop;
private Button btnStop;
// private Button cmdStart;
// private Button cmdFrame;
//private Button cmdStart;
/*internal System.Windows.Forms.Button cmdStop {
get { return withEventsField_cmdStop; }
set {
if (withEventsField_cmdStop != null) {
withEventsField_cmdStop.Click -= cmdStop_Click;
}
withEventsField_cmdStop = value;
if (withEventsField_cmdStop != null) {
withEventsField_cmdStop.Click += cmdStop_Click;
}
}
}*/
internal System.Windows.Forms.PictureBox pcbFrame;
[System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
this.videoBoard = new System.Windows.Forms.Panel();
this.pcbFrame = new System.Windows.Forms.PictureBox();
this.btnStop = new System.Windows.Forms.Button();
this.btnStart = new System.Windows.Forms.Button();
this.btnFrame = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pcbFrame)).BeginInit();
this.SuspendLayout();
//
// videoBoard
//
this.videoBoard.Location = new System.Drawing.Point(2, 0);
this.videoBoard.Name = "videoBoard";
this.videoBoard.Size = new System.Drawing.Size(320, 240);
this.videoBoard.TabIndex = 0;
//
// pcbFrame
//
this.pcbFrame.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.pcbFrame.Location = new System.Drawing.Point(334, 0);
this.pcbFrame.Name = "pcbFrame";
this.pcbFrame.Size = new System.Drawing.Size(320, 240);
this.pcbFrame.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pcbFrame.TabIndex = 4;
this.pcbFrame.TabStop = false;
//
// btnStop
//
this.btnStop.Location = new System.Drawing.Point(134, 246);
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(75, 23);
this.btnStop.TabIndex = 6;
this.btnStop.Text = "Stop";
this.btnStop.UseVisualStyleBackColor = true;
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(34, 246);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 23);
this.btnStart.TabIndex = 7;
this.btnStart.Text = "Start";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// btnFrame
//
this.btnFrame.Location = new System.Drawing.Point(376, 246);
this.btnFrame.Name = "btnFrame";
this.btnFrame.Size = new System.Drawing.Size(75, 23);
this.btnFrame.TabIndex = 8;
this.btnFrame.Text = "Frame";
this.btnFrame.UseVisualStyleBackColor = true;
this.btnFrame.Click += new System.EventHandler(this.btnFrame_Click);
//
// MW
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(656, 289);
this.Controls.Add(this.btnFrame);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.pcbFrame);
this.Controls.Add(this.videoBoard);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "MW";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Main Window";
((System.ComponentModel.ISupportInitialize)(this.pcbFrame)).EndInit();
this.ResumeLayout(false);
}
#endregion
public void RefreshImage(System.Windows.Forms.PictureBox Frame)
{
string[] s = null;
s = ModCap.CaptureInformation.PathVideo.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
this.pcbFrame.Image = Frame.Image;
this.pcbFrame.Image.Save(s[0] + Convert.ToString(ModCap.CaptureInformation.CounterFrames) + ".png");
ModCap.CaptureInformation.CounterFrames += 1;
this.pcbFrame.Refresh();
}
private void btnStart_Click(object sender, EventArgs e)
{
}
private void btnStop_Click(object sender, EventArgs e)
{
ModCap.CaptureInformation.CaptureInfo.Start();
btnStart.Enabled = false;
btnStop.Enabled = true;
}
private void btnFrame_Click(object sender, EventArgs e)
{
ModCap.CaptureInformation.CaptureInfo.Stop();
ModCap.ConfParamCam();
ModCap.PrepareCam(ModCap.CaptureInformation.PathVideo);
btnStart.Enabled = true;
btnStop.Enabled = false;
}
/* private void cmdFrame_Click(System.Object sender, System.EventArgs e)
{
ModCap.CaptureInformation.CaptureInfo.CaptureFrame();
}
private void cmdStart_Click(System.Object sender, System.EventArgs e)
{
ModCap.CaptureInformation.CaptureInfo.Start();
cmdStart.Enabled = false;
cmdStop.Enabled = true;
}
private void cmdStop_Click(System.Object sender, System.EventArgs e)
{
ModCap.CaptureInformation.CaptureInfo.Stop();
ModCap.ConfParamCam();
ModCap.PrepareCam(ModCap.CaptureInformation.PathVideo);
cmdStart.Enabled = true;
cmdStop.Enabled = false;
}*/
}
}

Custom button doesn't work

I want to make a custom button in an UserControl... Like with a hover animation. But in the usercontrol it won't let me execute the events?!
EDIT: I tried taking it out of the user control but that doesn't change anything...
My code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Title_Bar : UserControl
{
public Title_Bar()
{
InitializeComponent();
}
private void cross_idle_MouseEnter(object sender, EventArgs e)
{
cross_hover.Show();
}
private void cross_hover_MouseLeave(object sender, EventArgs e)
{
cross_hover.Hide();
}
private void max_idle_MouseEnter(object sender, EventArgs e)
{
max_hover.Show();
}
private void max_hover_MouseLeave(object sender, EventArgs e)
{
max_hover.Hide();
}
private void min_idle_MouseEnter(object sender, EventArgs e)
{
min_hover.Show();
}
private void min_hover_MouseLeave(object sender, EventArgs e)
{
min_hover.Hide();
}
private void cross_hover_Click(object sender, EventArgs e)
{
this.ParentForm.Close();
}
}
}
Designer Code:
namespace WindowsFormsApplication1
{
partial class Title_Bar
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Title_Bar));
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.pictureBox3 = new System.Windows.Forms.PictureBox();
this.cross_idle = new System.Windows.Forms.PictureBox();
this.max_idle = new System.Windows.Forms.PictureBox();
this.max_hover = new System.Windows.Forms.PictureBox();
this.min_idle = new System.Windows.Forms.PictureBox();
this.min_hover = new System.Windows.Forms.PictureBox();
this.cross_hover = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.cross_idle)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.max_idle)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.max_hover)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.min_idle)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.min_hover)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.cross_hover)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(49, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(272, 10);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// pictureBox2
//
this.pictureBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
this.pictureBox2.Location = new System.Drawing.Point(318, 0);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(50, 10);
this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox2.TabIndex = 1;
this.pictureBox2.TabStop = false;
//
// pictureBox3
//
this.pictureBox3.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox3.Image")));
this.pictureBox3.Location = new System.Drawing.Point(0, 0);
this.pictureBox3.Name = "pictureBox3";
this.pictureBox3.Size = new System.Drawing.Size(50, 10);
this.pictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox3.TabIndex = 2;
this.pictureBox3.TabStop = false;
//
// cross_idle
//
this.cross_idle.Image = ((System.Drawing.Image)(resources.GetObject("cross_idle.Image")));
this.cross_idle.Location = new System.Drawing.Point(358, 0);
this.cross_idle.Name = "cross_idle";
this.cross_idle.Size = new System.Drawing.Size(10, 10);
this.cross_idle.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.cross_idle.TabIndex = 3;
this.cross_idle.TabStop = false;
this.cross_idle.MouseEnter += new System.EventHandler(this.cross_idle_MouseEnter);
//
// max_idle
//
this.max_idle.Image = ((System.Drawing.Image)(resources.GetObject("max_idle.Image")));
this.max_idle.Location = new System.Drawing.Point(349, 0);
this.max_idle.Name = "max_idle";
this.max_idle.Size = new System.Drawing.Size(10, 10);
this.max_idle.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.max_idle.TabIndex = 4;
this.max_idle.TabStop = false;
this.max_idle.MouseEnter += new System.EventHandler(this.max_idle_MouseEnter);
//
// max_hover
//
this.max_hover.Image = ((System.Drawing.Image)(resources.GetObject("max_hover.Image")));
this.max_hover.Location = new System.Drawing.Point(267, 0);
this.max_hover.Name = "max_hover";
this.max_hover.Size = new System.Drawing.Size(10, 10);
this.max_hover.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.max_hover.TabIndex = 5;
this.max_hover.TabStop = false;
this.max_hover.Visible = false;
this.max_hover.MouseLeave += new System.EventHandler(this.max_hover_MouseLeave);
//
// min_idle
//
this.min_idle.Image = ((System.Drawing.Image)(resources.GetObject("min_idle.Image")));
this.min_idle.Location = new System.Drawing.Point(340, 0);
this.min_idle.Name = "min_idle";
this.min_idle.Size = new System.Drawing.Size(10, 10);
this.min_idle.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.min_idle.TabIndex = 6;
this.min_idle.TabStop = false;
this.min_idle.MouseEnter += new System.EventHandler(this.min_idle_MouseEnter);
//
// min_hover
//
this.min_hover.Image = ((System.Drawing.Image)(resources.GetObject("min_hover.Image")));
this.min_hover.Location = new System.Drawing.Point(237, 0);
this.min_hover.Name = "min_hover";
this.min_hover.Size = new System.Drawing.Size(10, 10);
this.min_hover.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.min_hover.TabIndex = 7;
this.min_hover.TabStop = false;
this.min_hover.Visible = false;
this.min_hover.MouseLeave += new System.EventHandler(this.min_hover_MouseLeave);
//
// cross_hover
//
this.cross_hover.Image = ((System.Drawing.Image)(resources.GetObject("cross_hover.Image")));
this.cross_hover.Location = new System.Drawing.Point(302, 0);
this.cross_hover.Name = "cross_hover";
this.cross_hover.Size = new System.Drawing.Size(10, 10);
this.cross_hover.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.cross_hover.TabIndex = 8;
this.cross_hover.TabStop = false;
this.cross_hover.Visible = false;
this.cross_hover.Click += new System.EventHandler(this.cross_hover_Click);
this.cross_hover.MouseLeave += new System.EventHandler(this.cross_hover_MouseLeave);
//
// Title_Bar
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.cross_hover);
this.Controls.Add(this.min_hover);
this.Controls.Add(this.min_idle);
this.Controls.Add(this.max_hover);
this.Controls.Add(this.max_idle);
this.Controls.Add(this.cross_idle);
this.Controls.Add(this.pictureBox3);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.pictureBox1);
this.Name = "Title_Bar";
this.Size = new System.Drawing.Size(368, 10);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.cross_idle)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.max_idle)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.max_hover)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.min_idle)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.min_hover)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.cross_hover)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.PictureBox pictureBox3;
private System.Windows.Forms.PictureBox cross_idle;
private System.Windows.Forms.PictureBox max_idle;
private System.Windows.Forms.PictureBox max_hover;
private System.Windows.Forms.PictureBox min_idle;
private System.Windows.Forms.PictureBox min_hover;
private System.Windows.Forms.PictureBox cross_hover;
}
}
I have put in all the picture boxes, but nothing happens!
this.cross_hover.Visible is set to false in the designer file, and calling Show is not going to change that value. Hence, the PictureBox is never shown.
Instead of calling Show(), set Visible to true:
private void cross_idle_MouseEnter(object sender, EventArgs e)
{
this.cross_hover.Visible = true;
}

The project compiles perfectly, but I get an unhandled exception and have no idea why

I get an unhandled exception that says:
System.NullReferenceException: Object reference not set to an instance of an object.
at Eagle_Eye_Class_Finder.GetSchedule.GetDataFromNumber(String ID) in C:\Users\Joshua Banks\Desktop\EET Project\Eagle Eye Class Finder\GetSchedule.cs:line 24
at Eagle_Eye_Class_Finder.Form1.button2_Click(Object sender, EventArgs e) in C:\Users\Joshua Banks\Desktop\EET Project\Eagle Eye Class Finder\Form1.cs:line 196
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.PerformClick()
at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.PreProcessMessage(Message& msg)
at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
Here is my get schedule class:
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing;
using System.Data.OleDb;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Collections.Generic;
namespace Eagle_Eye_Class_Finder
{
public class GetSchedule
{
public string GetDataFromNumber(string ID)
{
IDnumber[] IDnumbers = new IDnumber[3];
foreach (IDnumber IDCandidateMatch in IDnumbers)
{
if (IDCandidateMatch.ID == ID)
{
StringBuilder myData = new StringBuilder();
myData.AppendLine(IDCandidateMatch.Name);
myData.AppendLine(": ");
myData.AppendLine(IDCandidateMatch.ID);
myData.AppendLine(IDCandidateMatch.year);
myData.AppendLine(IDCandidateMatch.class1);
myData.AppendLine(IDCandidateMatch.class2);
myData.AppendLine(IDCandidateMatch.class3);
myData.AppendLine(IDCandidateMatch.class4);
//return myData;
return myData.ToString();
}
}
return "";
}
public GetSchedule()
{
IDnumber[] IDnumbers = new IDnumber[3];
IDnumbers[0] = new IDnumber() { Name = "Joshua Banks", ID = "900456317", year = "Senior", class1 = "TEET 4090", class2 = "TEET 3020", class3 = "TEET 3090", class4 = "TEET 4290" };
IDnumbers[1] = new IDnumber() { Name = "Sean Ward", ID = "900456318", year = "Junior", class1 = "ENGNR 4090", class2 = "ENGNR 3020", class3 = "ENGNR 3090", class4 = "ENGNR 4290" };
IDnumbers[2] = new IDnumber() { Name = "Terrell Johnson", ID = "900456319", year = "Sophomore", class1 = "BUS 4090", class2 = "BUS 3020", class3 = "BUS 3090", class4 = "BUS 4290" };
}
public class IDnumber
{
public string Name { get; set; }
public string ID { get; set; }
public string year { get; set; }
public string class1 { get; set; }
public string class2 { get; set; }
public string class3 { get; set; }
public string class4 { get; set; }
public static void ProcessNumber(IDnumber myNum)
{
StringBuilder myData = new StringBuilder();
myData.AppendLine(myNum.Name);
myData.AppendLine(": ");
myData.AppendLine(myNum.ID);
myData.AppendLine(myNum.year);
myData.AppendLine(myNum.class1);
myData.AppendLine(myNum.class2);
myData.AppendLine(myNum.class3);
myData.AppendLine(myNum.class4);
MessageBox.Show(myData.ToString());
}
}
}
}
Here is my Form 1 class:
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing;
using System.Data.OleDb;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing.Printing;
using System.Collections.Generic;
namespace Eagle_Eye_Class_Finder
{
/// This form is the entry form, it is the first form the user will see when the app is run.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.DateTimePicker dateTimePicker1;
private IContainer components;
private Timer timer1;
private BindingSource form1BindingSource;
public static Form Mainform = null;
// creates new instance of second form
YOURCLASSSCHEDULE SecondForm = new YOURCLASSSCHEDULE();
public Form1()
{
InitializeComponent();
// TODO: Add any constructor code after InitializeComponent call
}
/// Clean up any resources being used.
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.textBox1 = new System.Windows.Forms.TextBox();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button2 = new System.Windows.Forms.Button();
this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.form1BindingSource = new System.Windows.Forms.BindingSource(this.components);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.form1BindingSource)).BeginInit();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.form1BindingSource, "Text", true, System.Windows.Forms.DataSourceUpdateMode.OnValidation, null, "900456317"));
this.textBox1.Location = new System.Drawing.Point(328, 280);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(208, 20);
this.textBox1.TabIndex = 2;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(258, 410);
this.progressBar1.MarqueeAnimationSpeed = 10;
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(344, 8);
this.progressBar1.TabIndex = 3;
this.progressBar1.Click += new System.EventHandler(this.progressBar1_Click);
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(680, 400);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(120, 112);
this.pictureBox1.TabIndex = 4;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// button2
//
this.button2.Font = new System.Drawing.Font("Mistral", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button2.Image = ((System.Drawing.Image)(resources.GetObject("button2.Image")));
this.button2.Location = new System.Drawing.Point(699, 442);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(78, 28);
this.button2.TabIndex = 5;
this.button2.Text = "OK";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// dateTimePicker1
//
this.dateTimePicker1.Location = new System.Drawing.Point(336, 104);
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.Size = new System.Drawing.Size(200, 20);
this.dateTimePicker1.TabIndex = 6;
this.dateTimePicker1.ValueChanged += new System.EventHandler(this.dateTimePicker1_ValueChanged);
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// form1BindingSource
//
this.form1BindingSource.DataSource = typeof(Eagle_Eye_Class_Finder.Form1);
//
// Form1
//
this.AcceptButton = this.button2;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ClientSize = new System.Drawing.Size(856, 556);
this.Controls.Add(this.dateTimePicker1);
this.Controls.Add(this.button2);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Eagle Eye Class Finder";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.form1BindingSource)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// The main entry point for the application.
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public void Form1_Load(object sender, System.EventArgs e)
{
}
public void textBox1_TextChanged(object sender, System.EventArgs e)
{
//allows only numbers to be entered in textbox
string Str = textBox1.Text.Trim();
double Num;
bool isNum = double.TryParse(Str, out Num);
if (isNum)
Console.ReadLine();
else
MessageBox.Show("Enter A Valid ID Number!");
}
public void button2_Click(object sender, System.EventArgs e)
{
string text = textBox1.Text;
Mainform = this;
this.Hide();
GetSchedule myScheduleFinder = new GetSchedule();
string result = myScheduleFinder.GetDataFromNumber(text);
if (!string.IsNullOrEmpty(result))
{
MessageBox.Show(result);
}
else
{
MessageBox.Show("Enter A Valid ID Number!");
}
}
public void dateTimePicker1_ValueChanged(object sender, System.EventArgs e)
{
}
public void pictureBox1_Click(object sender, System.EventArgs e)
{
}
public void progressBar1_Click(object sender, EventArgs e)
{
//this.progressBar1 = new System.progressBar1();
//progressBar1.Maximum = 200;
//progressBar1.Minimum = 0;
//progressBar1.Step = 20;
}
private void timer1_Tick(object sender, EventArgs e)
{
//if (progressBar1.Value >= 200 )
//{
//progressBar1.Value = 0;
//}
//return;
//}
//progressBar1.Value != 20;
}
}
}
Here is my form 2 class:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Eagle_Eye_Class_Finder
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class YOURCLASSSCHEDULE : System.Windows.Forms.Form
{
public System.Windows.Forms.LinkLabel linkLabel1;
public System.Windows.Forms.LinkLabel linkLabel2;
public System.Windows.Forms.LinkLabel linkLabel3;
public System.Windows.Forms.LinkLabel linkLabel4;
private Button button1;
/// Required designer variable.
public System.ComponentModel.Container components = null;
public YOURCLASSSCHEDULE()
{
//
InitializeComponent();
// TODO: Add any constructor code after InitializeComponent call
}
/// Clean up any resources being used.
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(YOURCLASSSCHEDULE));
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
this.linkLabel2 = new System.Windows.Forms.LinkLabel();
this.linkLabel3 = new System.Windows.Forms.LinkLabel();
this.linkLabel4 = new System.Windows.Forms.LinkLabel();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// linkLabel1
//
this.linkLabel1.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.linkLabel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.linkLabel1.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.linkLabel1.LinkArea = new System.Windows.Forms.LinkArea(0, 7);
this.linkLabel1.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
this.linkLabel1.Location = new System.Drawing.Point(41, 123);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.Size = new System.Drawing.Size(288, 32);
this.linkLabel1.TabIndex = 1;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "Class 1";
this.linkLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
//
// linkLabel2
//
this.linkLabel2.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.linkLabel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.linkLabel2.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.linkLabel2.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
this.linkLabel2.Location = new System.Drawing.Point(467, 123);
this.linkLabel2.Name = "linkLabel2";
this.linkLabel2.Size = new System.Drawing.Size(288, 32);
this.linkLabel2.TabIndex = 2;
this.linkLabel2.TabStop = true;
this.linkLabel2.Text = "Class 2";
this.linkLabel2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.linkLabel2.VisitedLinkColor = System.Drawing.Color.Navy;
this.linkLabel2.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel2_LinkClicked);
//
// linkLabel3
//
this.linkLabel3.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.linkLabel3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.linkLabel3.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.linkLabel3.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
this.linkLabel3.Location = new System.Drawing.Point(41, 311);
this.linkLabel3.Name = "linkLabel3";
this.linkLabel3.Size = new System.Drawing.Size(288, 32);
this.linkLabel3.TabIndex = 3;
this.linkLabel3.TabStop = true;
this.linkLabel3.Text = "Class 3";
this.linkLabel3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.linkLabel3.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel3_LinkClicked);
//
// linkLabel4
//
this.linkLabel4.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.linkLabel4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.linkLabel4.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.linkLabel4.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
this.linkLabel4.Location = new System.Drawing.Point(467, 311);
this.linkLabel4.Name = "linkLabel4";
this.linkLabel4.Size = new System.Drawing.Size(288, 32);
this.linkLabel4.TabIndex = 4;
this.linkLabel4.TabStop = true;
this.linkLabel4.Text = "Class 4";
this.linkLabel4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.linkLabel4.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel4_LinkClicked);
//
// button1
//
this.button1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.button1.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(64)))));
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button1.Font = new System.Drawing.Font("Pristina", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button1.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.button1.ImageAlign = System.Drawing.ContentAlignment.TopCenter;
this.button1.Location = new System.Drawing.Point(358, 206);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(101, 25);
this.button1.TabIndex = 5;
this.button1.Text = "Go Back";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// YOURCLASSSCHEDULE
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ClientSize = new System.Drawing.Size(790, 482);
this.Controls.Add(this.button1);
this.Controls.Add(this.linkLabel4);
this.Controls.Add(this.linkLabel3);
this.Controls.Add(this.linkLabel2);
this.Controls.Add(this.linkLabel1);
this.Font = new System.Drawing.Font("OldDreadfulNo7 BT", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "YOURCLASSSCHEDULE";
this.Text = "Your Classes";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);
}
#endregion
public void Form2_Load(object sender, System.EventArgs e)
{
// if (text == "900456317")
// {
//}
}
public void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://www.georgiasouthern.edu/map/");
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://www.georgiasouthern.edu/map/");
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://www.georgiasouthern.edu/map/");
}
private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://www.georgiasouthern.edu/map/");
}
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.Show();
this.Hide();
}
}
}
The problem is on this if statement:
IDnumber[] IDnumbers = new IDnumber[3];
foreach (IDnumber IDCandidateMatch in IDnumbers)
{
if (IDCandidateMatch.ID == ID)
You created array with three elements, but all elements are null. When you loop through them, the reference to IDCandidateMatch.ID generates the NullReferenceException.
Compiling perfectly has nothing to do with Exceptions. I would suggest looking at the stacktrace on the Exception, trying to find the line where the error is occuring, and going from there.
It looks like line 24 of GetSchedule.cs is a good place to start!
You're creating 2 different arrays, one in the constructor and another one in the method. if you move the line
IDnumber[] IDnumbers = new IDnumber[3];
outside of the method, and delete the identical one in the constructor it should work

Categories

Resources