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

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;
}
}

Related

A new message box appears with each second after it appears

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.

if statements in C# for a textbox to change back to normal when i press a button for the second time

This is the code that i used to change the text in the text box from "Livre" to "Ocupado"
What code should i use to change it from "Ocupado" to "Livre"
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text="Ocupado";
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "Livre")
{
textBox1.Text = "Ocupado";
}
else
{
textBox1.Text = "Livre";
}
}
you can add a variable to your class
e.g.:
bool livre = true;
private void button1_Click(object sender, EventArgs e)
{
if (livre)
{
textBox1.Text="Ocupado";
}
else
{
textBox1.Text="Livre";
}
livre = !livre;
}

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;
}
}

Make Button Enabled after bools are true

I need make Button active if theese three bools are true
public bool isFileOpened = false;
public bool isDrive = false;
public bool isPrice = false;
They are becoming true after two textboxes are filled and
filePath string is not empty
private void textBox1_TextChanged(object sender, EventArgs e) {
drive = CheckIntInput(sender, "not valid");
if (drive != 0) {
isDrive = true;
}
}
private void textBox2_TextChanged(object sender, EventArgs e) {
price = CheckIntInput(sender, "not valid");
if (price != 0) {
isPrice = true;
}
}
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e) {
filePath = openFileDialog1.FileName;
label1.Text = filePath;
isFileOpened = true;
}
CheckIntInput method returning number from textBox or 0 if can`t convert string to number
And how i can make something like this:
if (isFileOpened && isDrive && isPrice) {
showButton.Enabled = true;
}
I want to make button enabled immediately after all three bools becomes true, and theese three fields can be inputted in different ways, like
textbox1
textbox2
openfiledialog1
or
textbox1
openfiledialog1
textbox2
There are multiple ways to do this, I'd use a property with a backing field, like this:
public bool IsFileOpened
{
get { return _isFileOpened; }
set
{
_isFileOpened = value;
UpdateShowButton();
}
}
public bool IsDrive
{
get { return _isDrive; }
set
{
_isDrive = value;
UpdateShowButton();
}
}
public bool IsPrice
{
get { return _isPrice; }
set
{
_isPrice = value;
UpdateShowButton();
}
}
private void UpdateShowButton()
{
if (IsPrice && IsDrive && IsFileOpened)
showButton.Enabled = true;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
drive = CheckIntInput(sender, "not valid");
if (drive != 0)
{
IsDrive = true;
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
price = CheckIntInput(sender, "not valid");
if (price != 0)
{
IsPrice = true;
}
}
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
filePath = openFileDialog1.FileName;
label1.Text = filePath;
IsFileOpened = true;
}
Actually I renamed it as well, so you have to use the properties with the capitalized starting letter. Now, everytime a property is updated, it checks wether to set the showButton enabled or not.
Here you can read more about fields and properties (with backing fields as well).

How to popup login form at start up to take password (it do not popup in constructor of MainPage)

I am working in silverlight and i have created a childwindow in which i have user name and passwrord.
private void OKButton_Click(object sender, RoutedEventArgs e)
{
if (txtUsrname.Text == "Username" && txtPassword.Password == "Password")
{
this.DialogResult = true;
}
else
{
MessageBox.Show("Incorrect username and/orpassword","Error",MessageBoxButton.OK);
}
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
And after i have my mainpage which contains the whole project GUI.Like this: (Herei also try to opopp a ChildWindow but it don't do so but when i put a MessageBox.Show("Hello"); then it is popuped)
public partial class MainPage : UserControl
{
public MainPage()
{
loginChildWindow obj = new loginChildWindow();
obj.Show(); //It do not pop up actually
MessageBox.Show("hello"); //whereas thsi messagebox popups
InitializeComponent();
}
}
So to set the login ChildWindow at startup time for my project i think i should do InitializeComponent(); only if my password and userName are matched. so this InitializeComponent(); must be called inside the
if (txtUsrname.Text == "Username" && txtPassword.Password == "Password")
{
InitializeComponent();
}
But when i do this it will give error (its obvious that i cannot do that in constructor):
public partial MainPage : UserControl
{
{
public MainPage()
{
private void OKButton_Click(object sender, RoutedEventArgs e)
{
if (txtUsrname.Text == "Username" && txtPassword.Password == "Password")
{
this.DialogResult = true;
InitializeComponent();
}
else
{
MessageBox.Show("Incorrect username and/orpassword","Error",MessageBoxButton.OK);
}
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
}
But how to solve it is my question ?
EDIT AFTER User1 comments:
the two classes are:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
Visibility = Visibility.Collapsed;
loginChildWindow log = new loginChildWindow();
log.Show();
}
}
and
public partial class loginChildWindow : ChildWindow
{
public loginChildWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (txtUsrname.Text == "Username" && txtPassword.Password == "Password")
{
MainPage obj = new MainPage();
obj.Visibility = Visibility.Visible;
obj.InitializeComponent();
this.DialogResult = true;
}
else
{
MessageBox.Show("Incorrect username and/orpassword", "Error", MessageBoxButton.OK);
}
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
private void Login_Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
}
}
try:
public MainPage()
{
InitializeComponent();
LoginWindow loginWnd=new LoginWindow();
loginWnd.Closed+= new EventHandler(loginWnd_Closed);
loginWnd.Show();
}
void loginWnd_Closed(object sender, EventArgs e)
{
LoginWindow lw = (LoginWindow)sender;
if (lw.DialogResult == true && lw.nameBox.Text != string.Empty)
{
this.textBlock1.Text = "Hello " + lw.nameBox.Text;
}
else if (lw.DialogResult == false)
{
this.textBlock1.Text = "Login canceled.";
}
}
where LoginWindow is:
public LoginWindow()
{
InitializeComponent();
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
private void LoginWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (this.DialogResult == true && (this.nameBox.Text == string.Empty || this.passwordBox.Password == string.Empty))
{
e.Cancel = true;
ChildWindow cw = new ChildWindow();
cw.Content = "Please Enter your name and password or click Cancel.";
cw.Show();
}
}
Please See the link below for more information
how-to-work-with-LoginWindows

Categories

Resources