I want that when you press a button, it adds 30 seconds to the date timer picker. but what happens to me is that they do not add up.
This is my code:
public partial class Form1 : Form
{
double seg;
public void Form1_Load(object sender, EventArgs e)
{
this.dateTimePicker1.Value = this.dateTimePicker1.Value.AddSeconds(seg);
}
private void s30_Click(object sender, EventArgs e)
{
seg = 30;
}
}
Update the control's value in the click event:
private void s30_Click(object sender, EventArgs e)
{
seg = 30;
this.dateTimePicker1.Value = this.dateTimePicker1.Value.AddSeconds(seg);
}
The Form_Load event only happens when the form is first loaded. (Note that this is specific to Windows Forms. In Web Forms you'll need to take this a step further and persist the value somewhere, because every post-back would load a new instance of the form and the value would start over.)
Of course, since seg never changes to anything but its hard-coded value, you don't really need it. Just increment the seconds by that hard-coded value:
private void s30_Click(object sender, EventArgs e)
{
this.dateTimePicker1.Value = this.dateTimePicker1.Value.AddSeconds(30);
}
Then you can remove the seg variable from your code entirely.
Related
What I'm trying to do is get user input from a form (IncomeForm) by using a textbox (TextBoxIncomePrice) and after pressing the button (ButtonConfirmIncome), the LabelIncome label in the form MainPage should change to the value input by the user.
Everything works as intented, except for when I try to reopen the IncomeForm by clicking the AddIncomeButton. I get the error shown in the title. It should be able to reopen and accept a new value no matter how many times you close the IncomeForm.
Main Form (MainPage):
IncomeForm incomeForm = new IncomeForm();
private void incomeForm_FormClosed(object sender, FormClosedEventArgs e)
{
LabelIncome.Text = incomeForm.TextBoxIncomePrice.Text;
}
private void AddIncomeButton_Click(object sender, EventArgs e)
{
incomeForm.FormClosed += new FormClosedEventHandler(incomeForm_FormClosed);
incomeForm.Show();
}
Add Income Form (IncomeForm):
private void ButtonConfirmIncome_Click(object sender, EventArgs e)
{
this.Close();
}
I know, i know there are lots and lots of questions asking on here about this error, each with their own response, but its easier to work off a response regarding your own code rather than someone else's
I have been working on this program for some time for a college assignment, and as soon as i started putting in the class to calculate the totals of things it now crashes,
I don't know where to look so i'll post my main code
enter code here
namespace Till
public partial class MainWindow : Window
{
Calculator calc = new Calculator();
public MainWindow()
{
InitializeComponent();
}
public bool User;
public bool tillopen = false;
private void button_Click(object sender, RoutedEventArgs e)
{
//button clone thingy
Button btn = (Button)sender;
label.Content = label.Content + btn.Content.ToString();
Console.Beep(); // makes the buttons beep
}
private void clear_Click(object sender, RoutedEventArgs e)
{
// Clear
label.Content = "";
}
private void Button_Submit_Click(object sender, RoutedEventArgs e)
{
// submit
listView.Items.Add(label.Content);
label.Content = "";
calc.setSoldItems(Convert.ToInt32(label.Content)); /// it breaks on this line///
}
private void button13_Click(object sender, RoutedEventArgs e)
{
//void sale
label.Content = "";
listView.Items.Clear();
}
private void button15_Click(object sender, RoutedEventArgs e)
{
//pound
label.Content = "1.00";
}
private void button12_Click(object sender, RoutedEventArgs e)
{
//till open close
tillopen = true;
}
private void button16_Click(object sender, RoutedEventArgs e)
{
Login m = new Login();
m.Show();
this.Close();
}
private void button14_Click(object sender, RoutedEventArgs e)
{
label.Content = "2.00"; // 2 pound
}
private void button17_Click(object sender, RoutedEventArgs e)
{
label.Content = calc.finish();
}
}
I have tried to re-create the error in another WPF (converting a to an int32) and it works fine, i know this is an issue with my code itself, i have tried using other machine and using different versions of visual studio itself, so we came to the assumption its this code itself and not a broken dll file
So before I sit down with my Teacher and spend all day going though my code step by step im asking around for help in order to save both of our time, This assignment is due in in 3 weeks, and it decides to break on me now.
thankies
To replicate this error, i press a number button on my Windows form, them hit the submit button i created (which starts the conversion) If a copy of my class which handles all of this is needed im happy to post it
In the method button_click, you have assigned value as
label.Content = label.Content + btn.Content.ToString();
which is a string value to the label and the values are concatenated instead of add.
and when you are reading it, you are converting it in Int32. which will give exception as it will not contain any integer value to it.
You can add the value like this:
label.Content = (Convert.ToInt32(label.Content) + Convert.ToInt32(btn.Content)).ToString();
and check before converting if the label has blank values in it, if it has do not convert them, and only convert the value if it has some values it will not give any error. Also do not assign any values other that numerical digits.
You are calculating:
Convert.ToInt32(label.Content)
but on the line before you set:
label.Content = "";
so this means you are calculating
Convert.ToInt32("")
which gives you a FormatException.
Perhaps you should use the value of label.Content before you overwrite it?
From the selectedindexchanged event, my variable has a value, but when it reaches the btn_click() event, the variable no longer has a value. Why is that?
public partial class TestingDatapass
{
private string item = null;
private string itemprice = null;
private int totalprice = 0;
protected void item_selectedindexchanged(object sender, EventArgs e)
{
//Both have a value here
item = item.SelectedValue;
itemprice = item.SelectedValue.Text;
}
protected void btn_click(object sender, EventArgs e)
{
//no value here
totalprice = Convert.ToInt32(itemprice)*Convert.ToInt32(item);
MessageBox.Show(totalprice);
}
}
EDIT
And to answer the ? posed in comments, the order of occurrence is the selectedindexchange THEN the btn_click()
EDIT REGARDING View State
So then would this be a proper way to set up what I am trying to achieve?
public partial class TestingDatapass
{
private int totalprice = 0;
protected void item_selectedindexchanged(object sender, EventArgs e)
{
ViewState["item"] = item.SelectedValue;
ViewState["itemprice"] = item.SelectedValue.Text;
}
protected void btn_click(object sender, EventArgs e)
{
totalprice = Convert.ToInt32(ViewState["item"])*Convert.ToInt32(ViewState["itemprice"]);
}
}
When a page is requested, ASP.NET creates an instance of TestingDatapass class and initialize itemprice,totalprice etc. fields. Now when you change your dropdown from client (which I assume looking at your item_selectedindexchanged method), Postback happens and it assign values you have mentioned in item_selectedindexchanged. Finally it destroys the instance, generates the html and sends it back to browser.
Now, when you press the button in your page then a new instance is created, your fields are re-initialized and you don't see the changed values in btn_click. This is how it works.
Thus if you want to preserve any data across postback, Consider using any State Management technique like ViewState, HiddenField etc.
Also, as a side note, MessageBox.Show is not available in ASP.NET.
Update:
I answered in the context of why it is not retaining the value in button click event, there are many ways to do it. But to answer your question, I don't see any reason to store the values in item_selectedindexchanged event as you are not doing anything there. You can directly access the dropdown selected values in button click handler like this:-
protected void btn_click(object sender, EventArgs e)
{
totalprice = Convert.ToInt32(item.SelectedValue) *
Convert.ToInt32(item.SelectedItem.Text);
}
Also, please note it's item.SelectedItem.Text and not SelectedValue.Text.
I have a tabbed form with a StatusStrip at the bottom, which includes a StatusLabel. I want to use this status label for various actions ("1 record updated" etc). It is simple enough to create specific events to set the label's text property.
But how best to reset the status to blank? The user could perform any number of other operations where the status is no longer meaningful (going to another tab, clicking other buttons etc.).
It is not feasible to create all the possible events to reset the status message. Is there a way to incorporate some type of timer so that the message fades out after several seconds? Has anyone else found a good solution for this?
Is it truly important to clear the status though? There are plenty of products which will keep their status label unchanged until the next status event occurs. Visual Studio is a good example of this. It may be worth simplifying your scenario and taking this approach.
If you do want to clear the status after an event I think the most maintainable way to do this is with a Timer. Essentially clear after a few seconds when the status is set
Timer m_timer;
void SetStatus(string text) {
m_statusLabel.Text = text;
m_timer.Reset();
}
void OnTimerTick(object sender, EventArgs e) {
m_statusLabel.Text = "";
m_timer.Stop();
}
Yes a timer would work for this to clear it. Here is an example of one I've knocked together.
public partial class Form1 : Form
{
private System.Timers.Timer _systemTimer = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
_systemTimer = new System.Timers.Timer(500);
_systemTimer.Elapsed += _systemTimer_Elapsed;
}
void _systemTimer_Elapsed(object sender, ElapsedEventArgs e)
{
toolStripStatusLabel1.Text = string.Empty;
_systemTimer.Stop(); // stop it if you don't want it repeating
}
private void button1_Click(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "random text just as an example";
}
private void button2_Click(object sender, EventArgs e)
{
_systemTimer.Start();
}
}
Assume button1 is your action to update the status, and button2 is just a random way to start the timer (this can be however you want to start it, I've only used another button click as an example). After the set amount of time passes the status label will be cleared.
I have a user control on an aspx page that contains filter fields and return a WhereClause. The user conrol will raise an event called FilterApplied when Search is clicked.
On my main form, I add the control with:
<uc1:Filter runat="server" ID="Filter" />
<br />
In my code behind I have:
protected void Page_Load(object sender, EventArgs e)
{
//Register event when filter is changed.
this.Filter.FilterApplied += new EventHandler(this.FilterApplied);
if (Page.IsPostBack)
{ //Do some things that take long
}
}
protected void FilterApplied(object sender, EventArgs e)
{
//Reload the page to refresh the graphs.
Page_Load(sender, e);
}
Problem is:
When I click Search on my user control, the Form_Load runs twice. Once because it is reloaded and then another time because I call it from FilterApplied. If I don't call it from FilterApplied, then the whereclause is still empty.
How can I ensure the Page_Load only run once when Search is clicked?
Your problem lays in multiple registering for FilterApplied event. Each time you call the Page_Load method, you subscribe to this event again. Here is a really simple example of what you are doing, written in WinForms with one button on the form, just to point out your problem:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int numClicks = 0;
private void Form1_Load(object sender, EventArgs e)
{
button1.Click += button1_Click;
this.Text = numClicks.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
numClicks++;
//try uncommenting and commenting next line of code, and observe the difference:
//button1.Click -= button1_Click;
Form1_Load(sender, e);
}
}