Monday, April 28, 2014

How to record a custom event upon button click?

I am working on a small binary converter program, and I am adding a feature to it that will record the decimal that they put into a text box before conversion and then record the output of the binary/hex code that it converts to.


I want to use the StreamWriter from System.IO to record the data into a text file that the user can open from the program based upon button click. Sadly, I have no idea how to use the StreamWriter. I will post the code i have below.



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;
using System.IO;

namespace Aspire_Binary_Converter
{
public partial class mainUI : Form
{
public mainUI()
{
InitializeComponent();
}

private void convertBtn_Click(object sender, EventArgs e)
{
string convertingNumTxt = convertNumTxt.Text;

if (convertingNumTxt.Contains("-"))
{
MessageBox.Show("ERROR: Type - SYNTX; The number to be converted may not contain the dash or hyphen.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
if (convertingNumTxt.Contains("."))
{
MessageBox.Show("ERROR: Type - SYNTX; The number to be converted may not contain the period or decimal.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
if (convertingNumTxt.Contains("()"))
{
MessageBox.Show("ERROR: Type - SYNTX; The number to be converted may not contain any type of parenthesis or bracket.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
//RECORD KEEPING

if (enableRecordCheck.Checked == true)
{
viewRecordsBtn.Visible = true;
this.Update();
}

if (enableRecordCheck.Checked == false)
{
viewRecordsBtn.Visible = false;
this.Update();
}

//HEXADECIMAL / BINARY CONVERTER

if (hexadecimalCheck.Checked)
{
int number = int.Parse(convertingNumTxt);
string hex = number.ToString("x");
MessageBox.Show("Conversion to hexadecimal was sucessful. Your code is: " + hex, "Hex Conversion Sucessful", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (convertNumTxt.Text == null || convertNumTxt.Text == "")
{//

MessageBox.Show("ERROR: Type - DATA; The conversion box must contain a number or numerical character before the conversion process can begin.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;

}

int decimalNumber;

bool blnresult = Int32.TryParse(convertingNumTxt, out decimalNumber);
if (!blnresult)
{
MessageBox.Show("ERROR: Type - SYNTX; The number to be converted may not contain any type of letter or alphabetical character.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{

int remainder;
string result = string.Empty;
while (decimalNumber > 0)
{
remainder = decimalNumber % 2;
decimalNumber /= 2;
result = remainder.ToString() + result;
}
MessageBox.Show("Conversion was sucessful. Your binary code is: " + result, "Conversion Sucessful");
}
}
}
}
}
}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
ABC_Usage frm = new ABC_Usage();
frm.Show();
}
}
}

Any Help towards this project will be greatly appreciated! Thanks for any help you guys can provide. :)


-Whutchison




Whutchison


No comments:

Post a Comment