|
It was late last night when I was coding that up, but I noticed a lot that could be broken down into that subclass. I also agree with earlier responses... some of this calculation logic could really be in another project altogether, separating the algorithm from the UI wiring. However, the original question asked how to make it modular... I believe that question has to do with the repetition in control logic. I assume the controls look like they're in a grid, having three columns or rows, each of which containing StartTimes, EndTimes, and the like. That's what the subclass was for.
I worked a lot more on the logic this morning, and this is what I have now:
public class UIForm
{
public class TimeSet
{
private TextBox _txtAllottedTime;
private TextBox _txtStartTime;
private TextBox _txtEndTime;
private Label _lblOverTime;
private Label _lblOverTimePercentage;
private int _overTime;
private int _completeTime;
public TimeSpan AllottedTime =>
TimeSpan.Parse(_txtAllottedTime.Text);
private DateTime StartTime =>
DateTime.Parse(_txtStartTime.Text);
private DateTime EndTime =>
DateTime.Parse(_txtEndTime.Text);
public TimeSet(
TextBox txtAllottedTime,
TextBox txtStartTime,
TextBox txtEndTime,
Label lblOverTime,
Label lblOverTimePercentage,
int overTime,
int completeTime)
{
_txtAllottedTime = txtAllottedTime;
_txtStartTime = txtStartTime;
_txtEndTime = txtEndTime;
_lblOverTime = lblOverTime;
_lblOverTimePercentage = lblOverTimePercentage;
OverTime = overTime;
_completeTime = completeTime;
}
public int OverTime
{
get => _overTime;
private set
{
_overTime = value;
_lblOverTime.Text = value.ToString();
}
}
public bool TextWasEnteredByUser =>
string.IsNullOrEmpty(AllottedTime.Text) ||
string.IsNullOrEmpty(StartTime.Text) ||
string.IsNullOrEmpty(EndTime.Text);
public TimeSpan Duration
{
get
{
DateTime end = EndTime;
if (StartTime > end)
{
end = end.AddDays(1);
}
return end.Subtract(StartTime);
}
}
private int Percentage
{
get
{
_completeTime++;
int percentage = (OverTime * 100) / _completeTime;
_lblOverTimePercentage.Text = percentage.ToString();
return percentage;
}
}
public bool ShouldWarnWarden() =>
(Percentage > 50);
public void CheckForOverTime()
{
if (TimeSpan.Compare(AllottedTime, Duration) == -1)
{
OverTime++;
}
}
}
private readonly List<TimeSet> TimeSets = new List<TimeSet>
{
new TimeSet(txtTimeLimit1, txtEntryTime1, txtExitTime1, lblOverTime1, _lblOverTimePercentage1, 0, 0),
new TimeSet(txtTimeLimit2, txtEntryTime2, txtExitTime2, lblOverTime2, _lblOverTimePercentage2, 0, 0),
new TimeSet(txtTimeLimit3, txtEntryTime3, txtExitTime3, lblOverTime3, _lblOverTimePercentage3, 0, 0)
};
public void park1() =>
park(TimeSets[0]);
public void park2() =>
park(TimeSets[1]);
public void park3() =>
park(TimeSets[2]);
private void park(TimeSet timeSet)
{
if (!timeSet.TextWasEnteredByUser)
{
MessageBox.Show("Enter all time values");
}
else
{
timeSet.CheckForOverTime();
if (timeSet.ShouldWarnWarden())
{
warnWarden();
}
}
}
}
So the idea is to break up that if/else block all the way down to a simple method, instead of having park() do all of the work. TimeSet does most of the heavy lifting, but in small chunks. It'd probably be easier to debug. But as others have stated, that percentage logic really should be in another layer altogether if you're making this a full blown program. Also if making this a full-blown program, I'd suggest making park() and its counterparts look like Park() as a matter of convention, just to keep new developers on the same page.
I hope that helps.
|
|
|
|
|
When i have a Tabpage with Imagelist:
Generated by this code:
ListView lstView = new ListView();
lstView.LargeImageList = ListaImagenes[indice];
lstView.Dock = DockStyle.Fill;
for (int j = 0; j < this.ListaImagenes[indice].Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
lstView.Items.Add(item);
}
TabPage tab = new TabPage(Procedimiento);
tab.Controls.Add(lstView);
this.ImagesTabControl.TabPages.Add(tab);
What is the code to obtain a list of images selected?
Thanks
|
|
|
|
|
Create a list of selected image indexes; add a SelectionChanged EventHandler to the ListView after you instantiate it.
List<int> SelectedImagesIndexes = new List<int>();
private void LstViewOnItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
int ndx = e.Item.ImageIndex;
if(ndx == -1) return;
bool inlist = SelectedImagesIndexes.Contains(ndx);
if (e.Item.Selected)
{
if (! inlist)
{
SelectedImagesIndexes.Add(ndx);
}
}
else
{
if (inlist)
{
SelectedImagesIndexes.Remove(ndx);
}
}
}
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Hi All,
I am new to C#,I have built a scanning application using C# in visual studio which scans images in black and white but I would like to add a comobox fpr colormode which has options such as Grayscale and color
Basically I am wanting to scan images in color How do I Implement it?
Thanks in Advance
I have used the following code
namespace WindowsApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ListScanners();
// Set start output folder TMP
textBox1.Text = Path.GetTempPath();
// Set JPEG as default
comboBox1.SelectedIndex = 1;
}
private void ListScanners()
{
// Clear the ListBox.
listBox1.Items.Clear();
// Create a DeviceManager instance
var deviceManager = new DeviceManager();
// Loop through the list of devices and add the name to the listbox
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
// Add the device only if it's a scanner
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
{
continue;
}
// Add the Scanner device to the listbox (the entire DeviceInfos object)
// Important: we store an object of type scanner (which ToString method returns the name of the scanner)
listBox1.Items.Add(
new Scanner(deviceManager.DeviceInfos[i])
);
}
}
private void button1_Click(object sender, EventArgs e)
{
Task.Factory.StartNew(StartScanning).ContinueWith(result => TriggerScan());
}
private void TriggerScan()
{
Console.WriteLine("Image succesfully scanned");
}
public void StartScanning()
{
Scanner device = null;
this.Invoke(new MethodInvoker(delegate()
{
device = listBox1.SelectedItem as Scanner;
}));
if (device == null)
{
MessageBox.Show("You need to select first an scanner device from the list",
"Warning",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
else if (String.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("Provide a filename",
"Warning",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
ImageFile image = new ImageFile();
string imageExtension = "";
this.Invoke(new MethodInvoker(delegate()
{
switch (comboBox1.SelectedIndex)
{
case 0:
image = device.ScanPNG();
imageExtension = ".png";
break;
case 1:
image = device.ScanJPEG();
imageExtension = ".jpeg";
break;
case 2:
image = device.ScanTIFF();
imageExtension = ".tiff";
break;
}
}));
var path = Path.Combine(textBox1.Text, string.Format(@"{0}", Guid.NewGuid()).Replace("-", "").ToUpper() + imageExtension);
if (File.Exists(path))
{
File.Delete(path);
}
image.SaveFile(path);
}
}
modified 14-Oct-18 20:42pm.
|
|
|
|
|
Without knowing anything about what you're using to do the black and white scan, and it's capabilities, it's pretty much impossible to tell you what is going on.
|
|
|
|
|
Thank you for feedback I have included the code which in my post which I have implemented
|
|
|
|
|
|
Can anybody help me uploading images (also other files) like Profile Photo (as in Skype, WhatsApp-Web, TeamViewer) through a Windows application written in C#.NET.
Note that, user want to upload photographs from his Desktop application to his own web-hosting and the files should be saved in a special folder inside the hosting. Later, these photographs should be accessed through his web application
|
|
|
|
|
And?
What have you tried?
Where are you stuck?
What help do you need?
From your recent questions, you don't seem to have done anything towards your projects, and we aren't here to do all your work for you ... you are the one getting paid for this, remember?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
You seem to have the wrong idea as to what this site is all about, it is a developer site, for the betterment of developers not a supply code site. If you are building the application then we can help with specific coding problems.
As for uploading files there are many articles here that will show you how it is done. You question on free SMS, how do you expect to get the data onto the mobile network, magic!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I am trying to develop a solution for small business house. I am want to integrate something such that the user of the application software can send SMS to any number for absolutely free without using any API or SMS Gateway.
If any solution available to send SMS to WhatsApp or Telegram or to any Android App is also ok. Can anybody please help me on this.
|
|
|
|
|
SMS messages use the mobile telephone networks and all of them charge for it.
|
|
|
|
|
PKSubudhi wrote: without using any API or SMS Gateway
Well then, I guess you're not going to be sending any SMS messages to anyone.
|
|
|
|
|
All options have some cost associated with them.
Even if you tethered a cell phone, you would still need to pay for the phone line and SMS service.
Director of Transmogrification Services
Shinobi of Query Language
Master of Yoda Conditional
|
|
|
|
|
I love getting down-voted for telling the truth.
Director of Transmogrification Services
Shinobi of Query Language
Master of Yoda Conditional
|
|
|
|
|
I have a program that does color tracking from webcam, my question is it possible to do this tracking also in a previously recorded video.
the code uses a combo box to tell which webcam to use the image:
VideoCaptureDevices videosource = new VideoCaptureDevices (videoDevices [cameraCombobox.selectedIndex] .MonikerSting);
the webcam image is played in the VideoSourcePlayer and a "newframe" event has the color tracking code.
but I wanted to play a video saved on the computer in VideoSourcePlayer, so yes activate the event "newframe", so I tried to put a url
VideoCaptureDevices videosource = new VideoCaptureDevices (@ "url");
the program did not raise an exception, however, nothing is played in VideoSourcePlayer and therefore, nothing happens.
Is there any way to play a video from the computer in VideoSourcePlayer? What can I do?
OBS: I'm sorry for the English of google translator.
CODE:
videoSourcePlayer1.SignalToStop();
videoSourcePlayer1.WaitForStop();
videoSourcePlayer2.SignalToStop();
videoSourcePlayer2.WaitForStop();
videoSourcePlayer3.SignalToStop();
videoSourcePlayer3.WaitForStop();
VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices(videoDevices[camerasCombo.SelectedIndex].MonikerString));
videoSource.DesiredFrameSize = new Size(400, 300);
videoSource.DesiredFrameRate = 12;
videoSourcePlayer1.VideoSource = videoSource;
videoSourcePlayer1.Start();
videoSourcePlayer2.VideoSource = videoSource;
videoSourcePlayer2.Start();
videoSourcePlayer3.VideoSource = videoSource;
videoSourcePlayer3.Start();
|
|
|
|
|
You need some class for reading video files, e.g. Accord.Video.FFMPEG.VideoFileReader of the Accord framework. Then you can get the individual frames of the video.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
How to connect a mobile phone camera into asp.net c# webapplication without using a usb cable. the mobile camera visual streaming into the c# webapplication lively.
Thanks in Advance.
|
|
|
|
|
Bluetooth.
"Never attribute to malice that which can be explained by stupidity."
- Hanlon's Razor
|
|
|
|
|
Probably, you can't.
The C# code of a webapp ruins on the server, never on the client - the client code runs on the browser, so it is probably in Javascript. And you can't connect the client mobile phone directly to your server because it is quite possibly thousands of miles away.
You can use the camera in HTML5, provided it is already connected to the client computer, but treh client will have to deal with that bit.
Capturing Audio & Video in HTML5 - HTML5 Rocks[^] may help, if not - google.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: The C# code of a webapp ruins on the server I see, it is by design then.
|
|
|
|
|
A touch Freudian, that...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I would like to mantain a certain solution as it is and to work on successive version of the same in different folders with different names. I tried but I found this very complicated and mainly annoying very much. For sure I did the wrong way.
Is there a procedure?
|
|
|
|
|
The normal way to work with multiple versions of the same codebase is to use a source control management system such as GIT[^].
This space for rent
|
|
|
|
|
Pete is absolutely right - GIT is the way to go.
You may find this helpful: Getting started with GIT, Visual Studio, and BitBucket[^] - most of it should be very similar for GitHub if you use that instead.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|