I have a main form with a progress bar on it, and I would like to update the progress bar from an external class called "Logic"... however, Logic is already being referenced to on the main form. If I try to reference the main form in the Logic to update the progress bar, I just get stack overflows.
While searching around, I came across a lot of topics about a BackgroundWorker... but that's not what I'm trying to use. I have specific places in my Logic class where I want to update the progress bar on the main form by using progressbar.PerformStep(). I've tried creating a method on the main form to update the progress bar and calling that from the Logic class, but once again it's lacking a reference... and I can't just use MainForm frm1 = new MainForm() without causing errors everywhere else. I'm feeling pretty stumped here.
[edit]
Here is the code with the solution (thanks to you guys)----
Main Form:
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 Natural_Language_Processor
{
public partial class frm_Main : Form
{
Logic logic = new Logic();
public frm_Main()
{
InitializeComponent();
}
private void frm_Main_Load(object sender, EventArgs e)
{
Timer.Start();
}
private void btn_Enter_Click(object sender, EventArgs e)
{
logic.Progress += new Logic.ProgressDelegate(DisplayProgess);
logic.RaiseProgress(0);
logic.str_Input = txt_Input.Text;
logic.Prep_Input();
txt_Input.Text = "";
logic.RaiseProgress(100);
System.Threading.Thread.Sleep(100);
logic.RaiseProgress(0);
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void eraseToolStripMenuItem_Click(object sender, EventArgs e)
{
logic.EraseMemory();
}
public void DisplayProgess(int percent)
{
if (this.InvokeRequired)
{
this.Invoke(new Logic.ProgressDelegate(DisplayProgess), new Object[] { percent });
}
else
{
this.progbar.Value = percent;
}
}
}
Logic:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace Natural_Language_Processor
{
class Logic
{
Data oData = new Data();
public List<string> Words = new List<string>();
private System.Threading.Thread T = null;
public delegate void ProgressDelegate(int percent);
public event ProgressDelegate Progress;
#region Variables
public string str_Input;
public string[] WordArray;
#endregion
public void RaiseProgress(int percent)
{
if (Progress != null)
{
Progress(percent);
}
}
public void Prep_Input()
{
//Check for Input
if (String.IsNullOrEmpty(str_Input))
{
}
else
{
//Set everything to lower-case
str_Input = str_Input.ToLower();
RaiseProgress(10);
//Remove all punctuation
if (str_Input.Contains(","))
{
while (str_Input.Contains(","))
{
int int_index = str_Input.IndexOf(",");
str_Input = str_Input.Remove(int_index, 1);
}
}
if (str_Input.EndsWith("."))
{
str_Input = str_Input.Trim('.');
}
else if (str_Input.EndsWith("?"))
{
str_Input = str_Input.Trim('?');
}
RaiseProgress(20);
//Split the sentence into an array of individual words
WordArray = str_Input.Split(' ');
RaiseProgress(30);
//Get current words (and max ID) from the database
int max_index = 0;
oData.GetWords();
Words.Clear();
if (oData.WordDataSet.Count > 0)
{
for (int i = 0; i < oData.WordDataSet.Count; i++)
{
max_index = oData.WordDataSet[i].ID;
Words.Add(oData.WordDataSet[i].Word);
}
}
RaiseProgress(40);
//Check each word in the sentence
for (int i = 0; i < WordArray.Length; i++)
{
//Update the frequency of an existing word in the database
if (Words.Contains(WordArray[i].ToString()))
{
oData.UpdateWords(WordArray[i].ToString());
}
else
{
//Or add the word
max_index = max_index + 1;
oData.InsertWordsTable(max_index, WordArray[i].ToString(), 1);
//And create its pre/pro word tables
oData.NewPreWordTable(WordArray[i].ToString());
oData.NewProWordTable(WordArray[i].ToString());
}
}
RaiseProgress(50);
//Check each word in the sentence after we have possibly created new pre/pro word tables in the previous code
for (int i = 1; i < WordArray.Length; i++)
{
oData.GetPreWords(WordArray[i].ToString());
Words.Clear();
//Get current pre_words from the database
for (int a = 0; a < oData.WordDataSet.Count; a++)
{
Words.Add(oData.WordDataSet[a].Word);
}
//Update the frequency of an existing word in the database
if (Words.Contains(WordArray[i - 1].ToString()))
{
oData.UpdatePreWords(WordArray[i].ToString(), WordArray[i - 1].ToString());
}
else
{
//Or add the word
oData.InsertPreWord(WordArray[i].ToString(), oData.GetPreWordIndex(WordArray[i].ToString()), WordArray[i - 1].ToString(), 1);
}
if (i == WordArray.Length - 1)
{
}
else
{
oData.GetProWords(WordArray[i].ToString());
Words.Clear();
//Get current pro_words from the database
for (int b = 0; b < oData.WordDataSet.Count; b++)
{
Words.Add(oData.WordDataSet[b].Word);
}
//Update the frequency of an existing word in the database
if (Words.Contains(WordArray[i + 1].ToString()))
{
oData.UpdateProWords(WordArray[i].ToString(), WordArray[i + 1].ToString());
}
else
{
//Or add the word
oData.InsertProWord(WordArray[i].ToString(), oData.GetProWordIndex(WordArray[i].ToString()), WordArray[i + 1].ToString(), 1);
}
}
}
RaiseProgress(60);
}
}
public void Respond()
{
RaiseProgress(70);
}
public void EraseMemory()
{
oData.GetWords();
Words.Clear();
for (int i = 0; i < oData.WordDataSet.Count; i++)
{
oData.DeletePreTable(oData.WordDataSet[i].Word);
oData.DeleteProTable(oData.WordDataSet[i].Word);
}
oData.DeleteWordsTable();
MessageBox.Show("Memory has been erased.");
}
}
}
Here's a loosely coupled approach, with the Logic() class raising a custom event instead of directly referencing the Form:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Logic logic = new Logic();
logic.Progress += new Logic.ProgressDelegate(DisplayProgess);
logic.Start();
}
public void DisplayProgess(string message, int percent)
{
if (this.InvokeRequired)
{
this.Invoke(new Logic.ProgressDelegate(DisplayProgess), new Object[] { message, percent });
}
else
{
this.label1.Text = message;
this.progressBar1.Value = percent;
}
}
}
public class Logic
{
private System.Threading.Thread T = null;
public delegate void ProgressDelegate(string message, int percent);
public event ProgressDelegate Progress;
public void Start()
{
if (T == null)
{
T = new System.Threading.Thread(new System.Threading.ThreadStart(Worker));
T.Start();
}
}
private void Worker()
{
RaiseProgress("Initializing...", 0);
System.Threading.Thread.Sleep(1000); // simulated work
RaiseProgress("Loading Map...", 25);
System.Threading.Thread.Sleep(1500); // simulated work
RaiseProgress("Loading Sprites...", 50);
System.Threading.Thread.Sleep(1200); // simulated work
RaiseProgress("Loading Sound Effects...", 75);
System.Threading.Thread.Sleep(1700);
RaiseProgress("Loading Music...", 85);
System.Threading.Thread.Sleep(1100); // simulated work
RaiseProgress("Done!", 100);
}
private void RaiseProgress(string message, int percent)
{
if (Progress != null)
{
Progress(message, percent);
}
}
}
Related
I want to show how many times I open and close the valve in the text box in the application I control, but I can only see the result when the process is finished, I cannot see it in real time.
private void button6_Click(object sender, EventArgs e)
{
sayacValue = 0;
int LoopCount = Convert.ToInt32(textBox_send.Text);
for (int s = 0; s < LoopCount; s++)
{
OpenValf();
IncreaseValfValue();
System.Threading.Thread.Sleep(400);
CloseValf();
System.Threading.Thread.Sleep(400);
}
}
public int IncreaseValfValue() //Counter Control Function
{
sayacValue++;
sayac.Text = sayacValue.ToString();
return sayacValue;
}
How can I do it using Thread or any other method?
You should use Microsoft's Reactive Framework (aka Rx) - NuGet System.Reactive.Windows.Forms - then you can do this:
using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reactive.Linq;
using System.Reactive.Disposables;
namespace WindowsFormsApp
{
public partial class ExampleForm : Form
{
public ExampleForm()
{
InitializeComponent();
}
private SerialDisposable _subscription = new SerialDisposable();
private void button6_Click(object sender, EventArgs e)
{
if (int.TryParse(textBox_send.Text, out int LoopCount))
{
if (LoopCount > 0)
{
_subscription.Disposable =
Observable
.Interval(TimeSpan.FromMilliseconds(400.0))
.Take(LoopCount + 1)
.ObserveOn(this)
.Subscribe(
x => sayac.Text = $"{x}",
() => sayac.Text = "Done.");
}
}
}
}
}
I've been trying to get undo and redo working in my text editor for about 3 days now. It's doing my head in.
I have a text box (named richTextBoxPrintCtrl1), which I would like to be able to undo and *redo *(word by word).
So if I click the undo button, it undoes the last word. And if I then clicked the redo button, it redoes the last word.
Could somebody help me get this working?
richTextBoxPrintCtrl1.Undo(); doesn't work very well. It deletes everything typed in the text box.
and if you type too much this error will appear
Thanks in advance for your help.
I know this question has been asked many times before, but I can't get it working using the information from the questions I've browsed here on SO. if it helps here my code
private string[] temp = new string[100];
private int index;
private int currentpostion;
public Undo()
{
index = 0;
currentpostion = 0;
}
public void Set_Text(string s)
{
temp[index] = s;
currentpostion = index;
++index;
}
public string UndoCons()
{
if (currentpostion > 0)
{
return temp[--currentpostion];
}
return null;
}
public string RedoCosns()
{
if (currentpostion < index)
{
return temp[++currentpostion];
}
return null;
}
Adding to #piedpiper's comment, you can look at the Memento design pattern (https://refactoring.guru/design-patterns/memento) which is a behavioral pattern and deals with this type of functionality.
But one way to approach this is to push every word to an array/List. You then have a with a position counter which you decrement on the undo, and then concatenate all the string in your array up to your position counter and replace all the text in your textbox with the concatenated string. The same logic applies for the redo, where you only increment the position counter.
Here is an example of how this can work.
I've used a Windows Form, RichEditBox and I disabled the 'ShortcutsEnabled' property on the RichEditBox to False.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private IList<Keys> ignoreKeys = new List<Keys> { Keys.Control, Keys.Tab, Keys.Shift, Keys.ControlKey, Keys.ShiftKey, Keys.LShiftKey, Keys.RShiftKey };
private IList<string> Words { get; set; }
private int counter = 0;
public Form1()
{
InitializeComponent();
Words = new List<string>();
}
private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.Control)
{
if (e.KeyCode == Keys.Z)
{
counter--;
}
if (e.KeyCode == Keys.Y)
{
counter++;
}
richTextBox1.Text = String.Join(" ", Words.Take(counter));
}
else if (!ignoreKeys.Any(ignoreKey => ignoreKey == e.KeyCode))
{
Words = richTextBox1.Text.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
counter = Words.Count;
}
}
}
}
I have found a way. I hope this end all the other questions. the code that I
have now is working :
public class Operation
{
private static List<string> _txt_list = new List<string>() { "" };
private static int Position { get; set; }
public void ChangeText(string txt)
{
if (_txt_list.Count < 100)
{
_txt_list.Add(txt);
//a 0
//al 1
//ali 2
}
else
{
for (int i = 0; i < 100; i++)
{
if (i == 99)
{
_txt_list[i] = txt;
}
else
{
_txt_list[i] = _txt_list[i + 1];
}
}
}
if (Position < 99)
{
Position = _txt_list.Count -1;
}
}
public string Undo()
{
if (Position > 0)
{
Position--;
}
return _txt_list[Position];
}
public string Redo()
{
if (Position < 99)
{
Position++;
}
return _txt_list[Position];
}
}
events:
private void Menu_Edit_Undo_Click(object sender, EventArgs e)
{
txt_Notepad.Text = _operationObj.Undo();
txt_Notepad.SelectionStart = txt_Notepad.Text.Length;
txt_Notepad.SelectionLength = 0;
}
private void Menu_Edit_Redo_Click(object sender, EventArgs e)
{
txt_Notepad.Text = _operationObj.Redo();
txt_Notepad.SelectionStart = txt_Notepad.Text.Length;
txt_Notepad.SelectionLength = 0;
}
textbox change
private void txt_keyUp(object sender, KeyEventArgs e)
{
_operationObj.ChangeText(txt_Notepad.Text);
}
i am currently learning on QR code webcam decoder. i have taken an example from https://zxingnet.svn.codeplex.com/svn/trunk/Clients/AForgeDemo/ and have succesfully build it without error. however when i run it with my webcam connected, no input or it wont switch on the webcam. based on my understanding, the webcam would be switch on when user select it at combobox. well, since there is no error at build, i cant pinpoint what went wrong. i have also taken a look at a project which switch on the webcam when user press a button and i plan to implement it to the current project. i have already insert the button but i do not know what should i program at the button to switch on the webcam instead on having to choose at combobox
would someone kindly advise or guide me through it.
below is the main program, and 2 class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Video;
using ZXing;
using System.Threading;
namespace AForgeDemo
{
public partial class AForgeDemoForm : Form
{
private struct Device
{
public int Index;
public string Name;
public override string ToString()
{
return Name;
}
}
private readonly CameraDevices camDevices;
private Bitmap currentBitmapForDecoding;
private readonly Thread decodingThread;
private Result currentResult;
private readonly Pen resultRectPen;
public AForgeDemoForm()
{
InitializeComponent();
camDevices = new CameraDevices();
decodingThread = new Thread(DecodeBarcode);
decodingThread.Start();
pictureBox1.Paint += pictureBox1_Paint;
resultRectPen = new Pen(Color.Green, 10);
}
void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (currentResult == null)
return;
if (currentResult.ResultPoints != null && currentResult.ResultPoints.Length > 0)
{
var resultPoints = currentResult.ResultPoints;
var rect = new Rectangle((int)resultPoints[0].X, (int)resultPoints[0].Y, 1, 1);
foreach (var point in resultPoints)
{
if (point.X < rect.Left)
rect = new Rectangle((int)point.X, rect.Y, rect.Width + rect.X - (int)point.X, rect.Height);
if (point.X > rect.Right)
rect = new Rectangle(rect.X, rect.Y, rect.Width + (int)point.X - rect.X, rect.Height);
if (point.Y < rect.Top)
rect = new Rectangle(rect.X, (int)point.Y, rect.Width, rect.Height + rect.Y - (int)point.Y);
if (point.Y > rect.Bottom)
rect = new Rectangle(rect.X, rect.Y, rect.Width, rect.Height + (int)point.Y - rect.Y);
}
using (var g = pictureBox1.CreateGraphics())
{
g.DrawRectangle(resultRectPen, rect);
}
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
LoadDevicesToCombobox();
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
if (!e.Cancel)
{
decodingThread.Abort();
if (camDevices.Current != null)
{
camDevices.Current.NewFrame -= Current_NewFrame;
if (camDevices.Current.IsRunning)
{
camDevices.Current.SignalToStop();
}
}
}
}
private void LoadDevicesToCombobox()
{
cmbDevice.Items.Clear();
for (var index = 0; index < camDevices.Devices.Count; index++)
{
cmbDevice.Items.Add(new Device { Index = index, Name = camDevices.Devices[index].Name });
}
}
private void cmbDevice_SelectedIndexChanged(object sender, EventArgs e)
{
if (camDevices.Current != null)
{
camDevices.Current.NewFrame -= Current_NewFrame;
if (camDevices.Current.IsRunning)
{
camDevices.Current.SignalToStop();
}
}
camDevices.SelectCamera(((Device)(cmbDevice.SelectedItem)).Index);
camDevices.Current.NewFrame += Current_NewFrame;
camDevices.Current.Start();
}
private void Current_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
if (IsDisposed)
{
return;
}
try
{
if (currentBitmapForDecoding == null)
{
currentBitmapForDecoding = (Bitmap)eventArgs.Frame.Clone();
}
Invoke(new Action<Bitmap>(ShowFrame), eventArgs.Frame.Clone());
}
catch (ObjectDisposedException)
{
// not sure, why....
}
}
private void ShowFrame(Bitmap frame)
{
if (pictureBox1.Width < frame.Width)
{
pictureBox1.Width = frame.Width;
}
if (pictureBox1.Height < frame.Height)
{
pictureBox1.Height = frame.Height;
}
pictureBox1.Image = frame;
}
private void DecodeBarcode()
{
var reader = new BarcodeReader();
while (true)
{
if (currentBitmapForDecoding != null)
{
var result = reader.Decode(currentBitmapForDecoding);
if (result != null)
{
Invoke(new Action<Result>(ShowResult), result);
}
currentBitmapForDecoding.Dispose();
currentBitmapForDecoding = null;
}
Thread.Sleep(200);
}
}
private void ShowResult(Result result)
{
currentResult = result;
txtBarcodeFormat.Text = result.BarcodeFormat.ToString();
txtContent.Text = result.Text;
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
the class for cameradevice
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AForge.Video.DirectShow;
namespace AForgeDemo
{
internal class CameraDevices
{
public FilterInfoCollection Devices { get; private set; }
public VideoCaptureDevice Current { get; private set; }
public CameraDevices()
{
Devices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
}
public void SelectCamera(int index)
{
if (index >= Devices.Count)
{
throw new ArgumentOutOfRangeException("index");
}
Current = new VideoCaptureDevice(Devices[index].MonikerString);
}
}
}
again, i kindly ask for anybody help on what i shall put at the button command to activate the webcam directly instead of choosing from combobox
thanks a million
The code which you need for your event handler behind the button is similar to the code within the method cmbDevice_SelectedIndexChanged.
I think it should look like the following
// select the first available camera and start capturing
camDevices.SelectCamera(0);
camDevices.Current.NewFrame += Current_NewFrame;
camDevices.Current.Start();
But I think the main challenge is to find out why the original example doesn't work as expected. It makes no difference if the same code is called in the handler of the combobox or in the handler of the button.
I'm new here, and also new in C# programming in Visual Studio.
Currently I have an assignment about C# refactoring.
This is the original class
calculator.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HUANG_Kai_30077528_Assign1
{
public partial class calculator : Form
{
public calculator()
{
InitializeComponent();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void calorieCalculation()
{
if (rbtnMale.Checked)
{
txtCalories.Text = (66
+ (6.3 * Convert.ToDouble(txtWeight.Text))
+ (12.9 * ((Convert.ToDouble(txtFeet.Text) * 12)
+ Convert.ToDouble(txtInches.Text)))
- (6.8 * Convert.ToDouble(txtAge.Text))).ToString();
}
else
{
txtCalories.Text = (655
+ (4.3 * Convert.ToDouble(txtWeight.Text))
+ (4.7 * ((Convert.ToDouble(txtFeet.Text) * 12)
+ Convert.ToDouble(txtInches.Text)))
- (4.7 * Convert.ToDouble(txtAge.Text))).ToString();
}
}
private void weightCalculation()
{
double maleVariable = 50;
double femaleVariable = 45.5;
double Formula = (2.3 * (((Convert.ToDouble(txtFeet.Text) - 5) * 12) + Convert.ToDouble(txtInches.Text)));
if (rbtnMale.Checked)
{
txtIdealWeight.Text = ((maleVariable + Formula) * 2.2046).ToString();
}
else
{
txtIdealWeight.Text = ((femaleVariable + Formula) * 2.2046).ToString();
}
}
private void txtFeetValidation()
{
double feet;
if (!double.TryParse(txtFeet.Text, out feet))
{
MessageBox.Show("Feet must be a numeric value.");
txtFeet.Select();
if (!(Convert.ToDouble(txtFeet.Text) >= 5))
{
MessageBox.Show("Height has to be equal to or greater than 5 feet!");
txtFeet.Select();
return;
}
}
}
private void txtInchesValidation()
{
double inches;
if (!double.TryParse(txtInches.Text, out inches))
{
MessageBox.Show("Inches must be a numeric value.");
txtInches.Select();
return;
}
}
private void txtWeightValidation()
{
double weight;
if (!double.TryParse(txtWeight.Text, out weight))
{
MessageBox.Show("Weight must be a numeric value.");
txtWeight.Select();
return;
}
}
private void txtAgeValication()
{
double age;
if (!double.TryParse(txtAge.Text, out age))
{
MessageBox.Show("Age must be a numeric value.");
txtAge.Select();
return;
}
}
private void btnCalculate_Click(object sender, EventArgs e)
{
txtFeetValidation();
txtInchesValidation();
txtWeightValidation();
txtAgeValication();
//Clear old results
txtIdealWeight.Text = "";
txtCalories.Text = "";
calorieCalculation();
weightCalculation();
}
private void label8_Click(object sender, EventArgs e)
{
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnAddPatient_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtIdealWeight.Text))
{
Form secondForm = new patientInfo(this);
secondForm.Show();
}
else
{
MessageBox.Show("Please enter all datas and click on 'Add Patient' to add patient's records");
}
}
private void btnPatientList_Click(object sender, EventArgs e)
{
Form secondForm = new patientList(this);
secondForm.Show();
}
private void btnClear_Click(object sender, EventArgs e)
{
rbtnMale.Checked = false;
rbtnFemale.Checked = false;
txtFeet.Text = "";
txtInches.Text = "";
txtWeight.Text = "";
txtAge.Text = "";
txtCalories.Text = "";
txtIdealWeight.Text = "";
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
}
}
The following classed are the parent class and sub-classes I would like to setup.
parent: calculator.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HUANG_Kai_30077528_Assign1
{
public partial class calculator : Form
{
//private string maleCaloriesCalculation();
//private string maleWeightCalculation();
//private string femaleCaloriesCalculation();
//private string femaleWeightCalculation();
public calculator()
{
InitializeComponent();
}
private void rbtnMale_CheckedChanged(object sender, EventArgs e)
{
}
private void btnCalculate_Click(object sender, EventArgs e)
{
//Clear old results
txtIdealWeight.Text = "";
txtCalories.Text = "";
/* Validate User Input: */
//Validate height (feet) is numeric value
double result;
if (!double.TryParse(txtFeet.Text, out result))
{
MessageBox.Show("Feet must be a numeric value.");
txtFeet.Select();
return;
}
//Validate height (inches) is numeric value
if (!double.TryParse(txtInches.Text, out result))
{
MessageBox.Show("Inches must be a numeric value.");
txtInches.Select();
return;
}
//Validate weight is numeric value
if (!double.TryParse(txtWeight.Text, out result))
{
MessageBox.Show("Weight must be a numeric value.");
txtWeight.Select();
return;
}
//Validate age is numeric value
if (!double.TryParse(txtAge.Text, out result))
{
MessageBox.Show("Age must be a numeric value.");
txtAge.Select();
return;
}
if (!(Convert.ToDouble(txtFeet.Text) >= 5))
{
MessageBox.Show("Height has to be equal to or greater than 5 feet!");
txtFeet.Select();
return;
}
/*End validation*/
calculation();
}
private void calculation()
{
if (rbtnMale.Checked)
{
txtCalories.Text = maleCalculator.maleCalories().ToString();
//txtCalories.Text = maleCalculator.maleCaloriesCalculation();
//txtIdealWeight.Text = maleCalculator.maleWeightCalculation();
}
else
{
txtCalories.Text = femaleCalculator.femaleCaloriesCalculation();
txtIdealWeight.Text = femaleCalculator.femaleWeightCalculation();
}
}
private void btnClear_Click(object sender, EventArgs e)
{
rbtnMale.Checked = false;
rbtnFemale.Checked = false;
txtFeet.Text = "";
txtInches.Text = "";
txtWeight.Text = "";
txtAge.Text = "";
txtCalories.Text = "";
txtIdealWeight.Text = "";
}
}
}
subclass: maleCalculation.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HUANG_Kai_30077528_Assign1
{
class maleCalculator: calculator
{
//private string maleCaloriesCalculation;
//private string maleWeightCalculation;
public maleCalculator maleCalories()
{
(66 + (6.3 * Convert.ToDouble(txtWeight.Text))
+ (12.9 * ((Convert.ToDouble(txtFeet.Text) * 12)
+ Convert.ToDouble(txtInches.Text)))
- (6.8 * Convert.ToDouble(txtAge.Text))).ToString();
return maleCalories();
//this.txtCalories.Text = new maleCalculator.maleCalories;
}
public maleCalculator maleWeight()
{
((50 + (2.3 * (((Convert.ToDouble(txtFeet.Text) - 5) * 12)
+ Convert.ToDouble(txtInches.Text)))) * 2.2046).ToString();
return maleWeight();
}
}
}
subclass: femaleCalculation.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HUANG_Kai_30077528_Assign1
{
class femaleCalculator : calculator
{
public double femaleCaloriesCalculation()
{
txtCalories.Text = (655 + (4.3 * Convert.ToDouble(txtWeight.Text))
+ (4.7 * ((Convert.ToDouble(txtFeet.Text) * 12)
+ Convert.ToDouble(txtInches.Text)))
- (4.7 * Convert.ToDouble(txtAge.Text))).ToString();
return femaleCaloriesCalculation();
}
public double femaleWeightCalculation()
{
txtIdealWeight.Text = ((45.5 + (2.3 * (((Convert.ToDouble(txtFeet.Text) - 5) * 12)
+ Convert.ToDouble(txtInches.Text)))) * 2.2046).ToString();
return femaleWeightCalculation();
}
}
}
As we can see, these two sub-classes are use to do the calculation for the calories and ideal weight. They are plan to take place of private void calorieCalculation() and private void weightCalculation() in the original class calculator.cs.
The function I need is like this:
When I execute the program and need to calculate the idealWeight and calories, the parent class calculator.cs will get the result from the formula contain in the sub-class, and ToString in the text box. That's why there are txtCalories and txtIdealWeight inside the calculator.cs.
So the question is how to get the results in the sub-class, and fill in the text boxes?
Guys, please help me with it as this is really important to me!!
Thank you all!!
Do you mean a virtual function? If so,
class Ancestor
{
public virtual int DoSomething()
{
// execute commands here.
// for now just throw exception.
throw new NotImplementedException();
}
}
class Derived_A : Ancestor
{
public override int DoSomething()
{
return 1 + 1;
}
}
class Derived_B : Ancestor
{
public override int DoSomething()
{
return 1 + 2;
}
}
This is ancestry, with virtual functions. For more on this:
Practical usage of virtual functions in c#
http://msdn.microsoft.com/en-us/library/9fkccyh4(v=vs.80).aspx
This type of code can also be done with interfaces. See http://msdn.microsoft.com/en-us/library/vstudio/ms173156.aspx.
Im trying to emulate the console in a windows forms applicaton. I have made it possible by using two extra threads and a delegate to be able to interact with my multiline textbox.
This somehow seems like I complicate things to much. So my questions.
Is there a better way of doing this?
When i press enter the command does not get sent, first if i press again it get sent? WHy is that? I ahve treid to debug it but failed to find the solution.
EDIT! Im using CsharpSSH, to do the SSH connection. Also I have included my full code now!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tamir.SharpSsh;
using System.IO;
using System.Threading;
using System.Timers;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public string mHost;
SshShell mShell;
public string mInput;
string pattern = "";
bool mInputHolder = false;
string mPattern = "";
int mValue = 0;
bool mStatus = false;
private Thread thrdtwo = null;
private Thread thrdone = null;
public string mKorv;
string mString = "";
delegate void SetTextCallback(string text);
bool clientopen = true;
public Form1()
{
InitializeComponent();
txthost.Text = "sdf.org";
txtuser.Text = "kalle82";
txtpass.Text = "kattsand";
string pattern = "sdf:";
mPattern = pattern;
}
public void button1_Click(object sender, EventArgs e)
{
mShell = new SshShell(Host, User);
mShell.Password = Pass;
//WRITING USER MESSAGE
txtOutput.AppendText("Connecting...");
mShell.Connect();
txtOutput.AppendText("OK");
mShell.ExpectPattern = mPattern;
mShell.RemoveTerminalEmulationCharacters = true;
this.SetText(mShell.Expect(pattern));
txtInput.Focus();
thrdone = new Thread(new ThreadStart(appengine));
thrdone.Start();
}
private void appengine()
{
this.txtInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(checkforenter);
// MessageBox.Show("Appengine started");
while (mShell.ShellOpened)
{
thrdtwo = new Thread(new ThreadStart(startthread2));
thrdtwo.Start();
thrdtwo.Join();
// this.SetText(mShell.Expect(pattern));
if (clientopen == false) break;
}
// MessageBox.Show("Appengine stopped");
}
private void startthread2()
{
//Wait for answer
while (mStatus == false)
{
}
}
//Recieves keypressevent
public void checkforenter(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
mStatus = true;
mString = txtInput.Text;
mShell.WriteLine(mString);
this.SetText(mShell.Expect(pattern));
txtOutput.AppendText(txtInput.Text + "\n");
}
mStatus = false;
}
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.txtOutput.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.txtOutput.Text = text.ToString();
}
}
public int checkfortrue()
{
if (mInputHolder != true)
{
mValue = 0;
}
if (mInputHolder == false)
{
mValue = -1;
}
return mValue;
}
public string userInput()
{
while (mInputHolder == true)
{
}
mInputHolder = true;
return txtInput.Text;
}
//Properties
public string Host
{
get
{
return txthost.Text;
}
set
{
txthost.Text = value;
}
}
public string User
{
get
{
return txtuser.Text;
}
set
{
txtuser.Text = value;
}
}
public string Pass
{
get
{
return txtpass.Text;
}
set
{
txtpass.Text = value;
}
}
public string Pattern
{
get
{
return pattern;
}
set
{
pattern = value;
}
}
private void button2_Click(object sender, EventArgs e)
{
clientopen = false;
}
}
}