A new message box appears with each second after it appears - c#

I ran into an issue where i tried displaying a display box after a countdown reached a certain time but for some odd reason it replicates with each second despite it having already passed the initial time it was supposed to appear. This is what i tried to do but now the timer has stopped and the time remaining column has stopped runng.
public partial class Form1 : Form
{
private List<CSession> sessionlist = new List<CSession>();
private TimeSpan workingTimeSpan = new TimeSpan();
private TimeSpan fiveMinutes = new TimeSpan(0,1,0);
private TimeSpan oneSecond = new TimeSpan(0,0,1);
public Form1()
{
InitializeComponent();
timer1.Enabled = true;
timer1.Start();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void AddTime_Click(object sender, EventArgs e)
{
workingTimeSpan += fiveMinutes;
DisplayWorkingTimeSpan();
}
private void DisplayWorkingTimeSpan()
{
TimerLabel.Text = workingTimeSpan.ToString();
}
private void DecreaseTime_Click(object sender, EventArgs e)
{
TimeSpan fiveMinutes = new TimeSpan(0,5,0);
workingTimeSpan -= fiveMinutes;
DisplayWorkingTimeSpan();
}
private void Confirm_Click(object sender, EventArgs e)
{
CSession newSession = new CSession();
if(PasswordText.Text == "")
{
MessageBox.Show("Password not entered");
return;
}
newSession.password = PasswordText.Text;
newSession.purchased_time = workingTimeSpan;
newSession.remaining_time = workingTimeSpan;
newSession.status = "Online";
sessionlist.Add(newSession);
PasswordText.Text = "";
TimerLabel.Text = "";
workingTimeSpan = new TimeSpan();
}
private void DisplayAllSessions()
{
listView1.Items.Clear();
foreach(CSession c in sessionlist)
{
string[] row = { c.password, c.purchased_time.ToString(), c.remaining_time.ToString(), c.status };
ListViewItem i = new ListViewItem(row);
listView1.Items.Add(i);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
foreach(CSession c in sessionlist)
{
if (c.remaining_time.TotalMinutes == 5)
{
timer1.Stop();
MessageBox.Show("Time almost up for client.");
}
if (c.remaining_time.TotalSeconds < 1)
{
c.status = "Offline";
}
if(c.status == "Online")
{
c.remaining_time -= oneSecond;
}
}
DisplayAllSessions();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
}

Add a flag that gets toggled when you display the message, so you won't display it again:
private bool MessageDisplayed = false;
private void timer1_Tick(object sender, EventArgs e)
{
foreach(CSession c in sessionlist)
{
if (c.remaining_time.TotalMinutes == 5 && !MessageDisplayed) // <-- check the flag
{
MessageDisplayed = true;
MessageBox.Show("Time almost up for client.");
}
if (c.remaining_time.TotalSeconds < 1)
{
c.status = "Offline";
}
if(c.status == "Online")
{
c.remaining_time -= oneSecond;
}
}
DisplayAllSessions();
}
Now you can leave the timer running and your message will only appear once.

Related

Can't hide labels and text boxes when button is checked

When salaried button is checked, change label "Hour Pay: " to "Salary" and hide the labels and text boxes below it. When Hourly button is checked, return everything to its initial form.
My main issue is when i execute code it does not hide labels and text boxes.
private void addButton_Click(object sender, EventArgs e)
{
}
private void icontype_CheckedChanged(object sender, EventArgs e)
{
if (salariedRadioButton.Checked == true)
{
hourLabel.Visible = false;
}
else if (hourlyRadioButton.Checked == true)
{
hourLabel.Visible = true;
}
}
private void hourTextBox_TextChanged(object sender, EventArgs e)
{
try
{
weekTextBox.Text =(float.Parse(hourTextBox.Text)40).ToString();
}
catch
{
}
try
{
yearTextBox.Text = (float.Parse(weekTextBox.Text) 52).ToString();
}
catch
{
}
}
I'm not sure what's your expected result, for the hide part, I think you can try this, thanks.
public Form1()
{
InitializeComponent();
this.salariedRadioButton.CheckedChanged += icontype_CheckedChanged;
this.hourlyRadioButton.CheckedChanged += icontype_CheckedChanged;
}
private void icontype_CheckedChanged(object sender, EventArgs e)
{
if (salariedRadioButton.Checked == true)
{
hourLabel.Visible = false;
weekTextBox.Visible = false;
}
else if(hourlyRadioButton.Checked == true)
{
hourLabel.Visible = true;
weekTextBox.Visible = true;
}
}

when opening a new form 2, it shows the settings that i have set

enter image description here
In this section is all of the Form 2 code.Please help me
public void Setting_Load(object sender, EventArgs e)
{
DefaulSetting();
GetDefaulSetting();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
Button Save Setting
public void btnSave_Click(object sender, EventArgs e)
{
GetDefaulSetting();
MessageBox.Show("Setting save successfully");
this.Close();
}
radio button
public void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (ReportEnable.Checked)
{
(this.Owner as Form1).tabControl1.TabPages[4].Enabled = true;
ReportDisable.Enabled = true;
}
else
{
ReportDisable.Enabled = true;
}
}
public void radioButton1_CheckedChanged_1(object sender, EventArgs e)
{
if (ReportDisable.Checked)
{
(this.Owner as Form1).tabControl1.TabPages[4].Enabled = false;
ReportEnable.Enabled = true;
}
else
{
(this.Owner as Form1).tabControl1.TabPages[4].Enabled = true;
ReportEnable.Enabled = true;
}
}
public void EnableDataTable_CheckedChanged(object sender, EventArgs e)
{
if (EnableDataTable.Checked)
{
(this.Owner as Form1).tabControl1.TabPages[3].Enabled = true;
EnableDataTable.Enabled = true;
}
else
{
EnableDataTable.Enabled = true;
}
}
public void DisableDataTable_CheckedChanged(object sender, EventArgs e)
{
if (DisableDataTable.Checked)
{
(this.Owner as Form1).tabControl1.TabPages[3].Enabled = false;
DisableDataTable.Enabled = true;
}
else
{
(this.Owner as Form1).tabControl1.TabPages[3].Enabled = true;
DisableDataTable.Enabled = true;
}
}
public void EnableSerialWrite_CheckedChanged(object sender, EventArgs e)
{
if (EnableSerialWrite.Checked)
{
(this.Owner as Form1).tabControl1.TabPages[1].Enabled = true;
EnableSerialWrite.Enabled = true;
}
else
{
EnableSerialWrite.Enabled = true;
}
}
public void DisableSerialWrite_CheckedChanged(object sender, EventArgs e)
{
if (DisableSerialWrite.Checked)
{
(this.Owner as Form1).tabControl1.TabPages[1].Enabled = false;
DisableSerialWrite.Enabled = true;
}
else
{
DisableSerialWrite.Enabled = true;
}
}
public void EnableSerialChart_CheckedChanged(object sender, EventArgs e)
{
if (EnableSerialChart.Checked)
{
(this.Owner as Form1).tabControl1.TabPages[2].Enabled = true;
(this.Owner as Form1).chart1.Show();
EnableSerialChart.Enabled = true;
}
else
{
EnableSerialChart.Enabled = true;
}
}
public void DisableSerialChart_CheckedChanged(object sender, EventArgs e)
{
if (DisableSerialChart.Checked)
{
(this.Owner as Form1).tabControl1.TabPages[2].Enabled = false;
(this.Owner as Form1).chart1.Hide();
DisableSerialChart.Enabled = true;
}
else
{
(this.Owner as Form1).tabControl1.TabPages[2].Enabled = true;
(this.Owner as Form1).chart1.Show();
(this.Owner as Form1).chart1.Enabled = true;
DisableSerialChart.Enabled = true;
}
}
public void EnableSerialRead_CheckedChanged(object sender, EventArgs e)
{
if (EnableSerialRead.Checked)
{
(this.Owner as Form1).tabControl1.TabPages[0].Enabled = true;
EnableSerialRead.Enabled = true;
}
else
{
EnableSerialRead.Enabled = true;
}
}
public void DisableSerialRead_CheckedChanged(object sender, EventArgs e)
{
if (DisableSerialRead.Checked)
{
(this.Owner as Form1).tabControl1.TabPages[0].Enabled = false;
DisableSerialRead.Enabled = true;
}
else
{
(this.Owner as Form1).tabControl1.TabPages[0].Enabled = true;
DisableSerialRead.Enabled = true;
}
}
public void btnCancel_Click(object sender, EventArgs e)
{
EnableSerialChart.Checked = true;
EnableDataTable.Checked = true;
EnableSerialRead.Checked = true;
EnableSerialWrite.Checked = true;
ReportEnable.Checked = true;
}
private void Setting_FormClosed(object sender, FormClosedEventArgs e)
{
}
public void DefaulSetting()
{
Settings1.Default.SerialRead = EnableSerialRead.Text;
Settings1.Default.SerialWrtite = EnableSerialWrite.Text;
Settings1.Default.SerialChart = EnableSerialChart.Text;
Settings1.Default.DataTable = EnableDataTable.Text;
Settings1.Default.Report = ReportEnable.Text;
}
public void GetDefaulSetting()
{
EnableSerialRead.Text = Settings1.Default.SerialRead;
EnableSerialWrite.Text = Settings1.Default.SerialWrtite;
EnableSerialChart.Text = Settings1.Default.SerialChart;
EnableDataTable.Text = Settings1.Default.DataTable;
ReportEnable.Text = Settings1.Default.Report;
Settings1.Default.Save();
}
}
And this is my form 1 code when hitting the Setting button.
public void btnSetting_Click(object sender, EventArgs e)
{
Setting frm = new Setting();
frm.Owner = this;
frm.ShowDialog();
}
Move Settings1.Default.Save(); to the DefaulSetting method, call GetDefaulSetting in the form load event, and call DefaulSetting in the form close event.
You might also consider renaming the methods ReadSettings and WriteSettings so you don't confuse yourself.

How to avoid user being able to create a Double Booking in a database?

Basically I would like to stop my system from allowing the user to be able to create multiple bookings under the same time slot for a booking if that makes sense. My system basically allows a user to make a booking for a walk, and ideally each walk should only be able to be booked from Monday to Sunday during working hours, but a slot should be 1.5 hours each but at the moment, there is no slots, just the date time pickers for the time and date of the booking and for multiple bookings the user can just pick the same time so i do want to get that fixed.
Hopefully this helps- I have attached my code for my "add booking" form so hopefully thats of some use.
public partial class AddBooking : Form
{
private int count;
private Boolean IsEmpty = false;
private static string _connectionstring = ConfigurationManager.ConnectionStrings["DoggieConnectionString"].ConnectionString;
public AddBooking()
{
InitializeComponent();
CenterToScreen();
GenerateBookingNumber();
IDDisplay.Text = "" + count;
dateTimePicker2.Format = DateTimePickerFormat.Custom;
dateTimePicker2.CustomFormat = "HH:mm tt";
dateTimePicker2.ShowUpDown = true;
DateTime now = DateTime.Now;
}
private void AddBooking_Load(object sender, EventArgs e)
{
}
private int GenerateBookingNumber()
{
string smt = "SELECT COUNT(*) FROM dbo.Booking";
count = 0;
using (SqlConnection connection = new SqlConnection(_connectionstring))
{
using (SqlCommand cmdCount = new SqlCommand(smt, connection))
{
connection.Open();
count = (int)Convert.ToInt32(cmdCount.ExecuteScalar());
}
}
count = count + 4;
return count;
}
private void PresenceCheck()
{
if (string.IsNullOrEmpty(WalkLocationtxt.Text) || string.IsNullOrEmpty(StaffIDtxt.Text))
{
IsEmpty = true;
}
else
{
IsEmpty = false;
}
}
private void SubmitInfobtn_Click(object sender, EventArgs e)
{
// MessageBox.Show("welcome " + dateTimePicker1.Value.ToShortDateString());
// MessageBox.Show("Goodddd" + dateTimePicker2.Value.ToShortTimeString());
PresenceCheck();
if (IsEmpty == false)
{
int rowsareaffected = ClassDatabase.AddBookingDetails(Convert.ToInt32(IDDisplay.Text), dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortTimeString(), Convert.ToInt32(StaffIDtxt.Text), WalkLocationtxt.Text);
if (rowsareaffected > 0)
{
MessageBox.Show("New Booking Added Sucessfully", "Sucess!", MessageBoxButtons.OK, MessageBoxIcon.Information);
StaffIDtxt.Clear();
WalkLocationtxt.Clear();
GenerateBookingNumber();
IDDisplay.Text = "" + count;
}
else
{
MessageBox.Show("An Error Occurred", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
{
}
private void mENUToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void returnToMenuToolStripMenuItem_Click(object sender, EventArgs e)
{
new MenuScreen().Show();
this.Close();
}
private void logOutToolStripMenuItem_Click(object sender, EventArgs e)
{
new LoginScreen().Show();
this.Close();
}
private void exitSystemToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult Result = MessageBox.Show("Are you sure you want to Exit the JD Dog Care Program?", "Are You Sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Hand);
if (Result == DialogResult.Yes)
{
Application.Exit();
}
else
{
}
}
private void addClientToolStripMenuItem_Click(object sender, EventArgs e)
{
new AddClient().Show();
this.Close();
}
private void manageClientsToolStripMenuItem_Click(object sender, EventArgs e)
{
new ViewClient().Show();
this.Close();
}
private void addDogsToolStripMenuItem_Click(object sender, EventArgs e)
{
new AddDog().Show();
this.Close();
}
private void manageDogsToolStripMenuItem_Click(object sender, EventArgs e)
{
new ViewDog().Show();
this.Close();
}
private void addStaffToolStripMenuItem_Click(object sender, EventArgs e)
{
new AddStaff().Show();
this.Close();
}
private void manageStaffToolStripMenuItem_Click(object sender, EventArgs e)
{
new ViewStaff().Show();
this.Close();
}
private void addBookingToolStripMenuItem_Click(object sender, EventArgs e)
{
new AddBooking().Show();
this.Close();
}
private void manageBookingToolStripMenuItem_Click(object sender, EventArgs e)
{
new ViewBooking().Show();
this.Close();
}
private void addDogToBookingToolStripMenuItem_Click(object sender, EventArgs e)
{
new DogToWalk().Show();
this.Close();
}
}
}

How to conbine two diffrence label's number to a new label?

I need to calculate the Lb1SumF plus Lb2SumF equal Lb3SumF.
I ran it, and somehow the label3 does not display the expected result.
Here is a screenshot from result.
Here is my code.
private void Form1_Load(object sender, EventArgs e)
{
TX1.TabIndex=0;
}
private void TX1_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb1PriceF.Text) * Convert.ToInt32(TX1.Text);
Lb1SumF.Text = Convert.ToString(sumF); //Label1 sum
}
catch
{
Lb1SumF.Text = "0";
}
}
private void TX2_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb2PriceF.Text) * Convert.ToInt32(TX2.Text);
Lb2SumF.Text = Convert.ToString(sumF); //Label2 sum
}
catch
{
Lb2SumF.Text = "0";
}
}
private void Lb3_TextChanged(object sender, EventArgs e)
{
int i = Convert.ToInt32(Lb1SumF.Text);
int j = Convert.ToInt32(Lb2SumF.Text);
Lb3.Text = Convert.ToString(i+j); // Label3 sum
}
Lb3_TextChanged might never be invoked as you are not changing the text of the label. I would suggest to change it to a private method and not an event handler. Here is what the code could be like:
private void TX1_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb1PriceF.Text) * Convert.ToInt32(TX1.Text);
Lb1SumF.Text = Convert.ToString(sumF); //Label1 sum
// Call to update sum
UpdateSum();
}
catch
{
Lb1SumF.Text = "0";
}
}
private void TX2_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb2PriceF.Text) * Convert.ToInt32(TX2.Text);
Lb2SumF.Text = Convert.ToString(sumF); //Label2 sum
// Call to update sum
UpdateSum();
}
catch
{
Lb2SumF.Text = "0";
}
}
// private void Lb3_TextChanged(object sender, EventArgs e)
private void UpdateSum()
{
int sum = 0;
if(!string.IsNullOrEmpty(Lb1SumF.Text) && !string.IsNullOrEmpty(Lb2SumF.Text))
{
sum = Convert.ToInt32(Lb1SumF.Text) + Convert.ToInt32(Lb2SumF.Text);
}
Lb3.Text = Convert.ToString(sum);
}
Replace you code with this it will work, you are using * operator where you have to use + operator, I have commented that lines in your code and replaced it for better understanding.
Happy Coding
namespace WindowsFormsApp8
{
public partial class Form1 : Form
{
private void Lb1SumF_Click(object sender, EventArgs e)
{
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TX1.TabIndex=0;
}
private void label4_Click(object sender, EventArgs e)
{
}
private void TX1_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
//sumF = Convert.ToInt32(Lb1PriceF.Text) * Convert.ToInt32(TX1.Text); // You were doing wrong here , you were multiplying these values
sumF = Convert.ToInt32(Lb1PriceF.Text) + Convert.ToInt32(TX1.Text);
Lb1SumF.Text = Convert.ToString(sumF); //Label1 sum
}
catch
{
Lb1SumF.Text = "0";
}
}
private void TX2_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
//sumF = Convert.ToInt32(Lb2PriceF.Text) * Convert.ToInt32(TX2.Text); //you are doing it wrong here , you are multiplying
sumF = Convert.ToInt32(Lb2PriceF.Text) + Convert.ToInt32(TX2.Text);
Lb2SumF.Text = Convert.ToString(sumF); //Label2 sum
}
catch
{
Lb2SumF.Text = "0";
}
}
private void Lb3_TextChanged(object sender, EventArgs e)
{
int i = Convert.ToInt32(Lb1SumF.Text);
int j = Convert.ToInt32(Lb2SumF.Text);
Lb3.Text = Convert.ToString(i+j); // Label3 sum
}
private void Lb3SumF_Click(object sender, EventArgs e)
{
}
}
}

ArgumentOutOfRangeException on an object listview when hitting tab

I have an object list view that has two text columns. When I edit the left column and hit tab to go to the right I receive a "ArgumentOutOfRangeException" with an index of -1. Looks like the index is something internal to the list view because I debugged my application and found no errors. Here is the code :
public partial class SummaryOverviewSettingsDlg : Form
{
private List<SummaryDataset> _localSummaryDatasets = new List<SummaryDataset>();
private bool _includeLimits;
private SummaryOverviewSettings _summaryOverviewSettings;
public bool IncludeLimits { get { return _includeLimits; } }
public SummaryOverviewSettingsDlg(SummaryOverviewSettings summaryOverviewSettings)
{
InitializeComponent();
if (summaryOverviewSettings.Datasets != null)
{
_localSummaryDatasets.AddRange(summaryOverviewSettings.Datasets);
}
_summaryOverviewSettings = summaryOverviewSettings;
}
private void DataFilesListDlg_Load(object sender, EventArgs e)
{
foreach(var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
LimitsCheckbox.Checked = _summaryOverviewSettings.ShowLimits;
}
private void OlvFilePaths_CellRightClick(object sender, CellRightClickEventArgs e)
{
var contextMenuSymbol = new ContextMenuStrip();
ToolStripItem item;
item = contextMenuSymbol.Items.Add("Add sample");
item.Click += ContextMenuAddFilePath;
if (e.Model != null)
{
contextMenuSymbol.Items.Add("-");
item = contextMenuSymbol.Items.Add("Delete sample");
item.Click += ContextMenuDeleteFilePath;
}
olvFilePaths.ContextMenuStrip = contextMenuSymbol;
}
private void ContextMenuAddFilePath(object sender, EventArgs e)
{
var item = new SummaryDataset()
{
SampleName = "Sample",
Path = "Path"
};
_localSummaryDatasets.Add(item);
// Rebuild the list in the GUI
olvFilePaths.ClearObjects();
foreach (var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
olvFilePaths.AutoResizeColumns();
}
private void ContextMenuDeleteFilePath(object sender, EventArgs e)
{
if (olvFilePaths.SelectedObject != null)
{
var item = (SummaryDataset)olvFilePaths.SelectedObject;
olvFilePaths.RemoveObject(item);
_localSummaryDatasets.Remove(item);
}
}
private void OlvFilePaths_CellEditFinished(object sender, CellEditEventArgs e)
{
if (e.Control is TextBox textBox)
{
var oldValue = (string)e.Value;
var newValue = (string)e.NewValue;
var col = e.Column.AspectName;
var index = e.ListViewItem.Index;
if (newValue != oldValue)
{
if (col == "SampleName")
{
_localSummaryDatasets[index].SampleName = newValue;
}
else
{
_localSummaryDatasets[index].Path = newValue;
}
}
}
// Rebuild the list in the GUI
olvFilePaths.ClearObjects();
foreach (var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
olvFilePaths.AutoResizeColumns();
}
private void OkButton_Click(object sender, EventArgs e)
{
_summaryOverviewSettings.Datasets.Clear();
_summaryOverviewSettings.Datasets.AddRange(_localSummaryDatasets);
_summaryOverviewSettings.ShowLimits = _includeLimits;
DialogResult = DialogResult.OK;
Close();
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void LimitsCheckbox_CheckedChanged(object sender, EventArgs e)
{
_includeLimits = LimitsCheckbox.Checked;
}
}

Categories

Resources