I have a windows form app that display restaurant orders. I want to load the code every 5 seconds to check if there is a new order to display.
I have a timer created in the form designer:
public void timer1_Tick(object sender, EventArgs e)
{
}
public void DisplayRestaurantOrder()
{
//Display restaurant order here
}
private void Form1_Load(object sender, EventArgs e)
{
DisplayRestaurantOrder();
timer1.Interval = 5000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (true)//check new order
{
DisplayRestaurantOrder();
}
}
1) Set the timer's Interval property to 5000 (milliseconds)
2) Create a method which loads the data e.g.
private void LoadOrders()
{
// ... do stuff here
}
3) In the timer's Tick event handler make a call to the load method, in this case LoadOrders:
public void timer1_Tick(object sender, EventArgs e)
{
LoadOrders();
}
4) In the Form.Load event do timer1.Start();, and maybe also a initial call to the load method, to make a Form.Load event handler just double click the form:
private void Form1_Load(object sender, EventArgs e)
{
//LoadOrders(); //this is the initial load call.
//timer1.Start();
}
as a result you should have something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadOrders();
timer1.Start();
}
public void timer1_Tick(object sender, EventArgs e)
{
LoadOrders();
}
private void LoadOrders()
{
// ... do stuff here
}
}
UPDATE (sins the OP wants to load what is in the constructor):
If what is needed to be loaded, is in the Form1 constructor then just move everything from in there to a new method and make a call to that method in both the timer1_Tick handler and in the constructor itself, e.g.:
public Form1()
{
//InitializeComponent();
Load();
}
//should be kept as to start the timer.
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
public void timer1_Tick(object sender, EventArgs e)
{
Load();
}
private void Load()
{
//InitializeComponent(); //this shouldn't be called more than once as it can create duplicate objects, i.e. buttons, menu strips, etc.
// ... do other stuff here
}
Related
I made a simple GUI, in which I would like to add an option where I can switch pages, it is the same page when I open it, I would like a bar where I click and switch pages
namespace POS
{
public partial class Form1 : Form
{
private object gunaLabel_date;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Guna.UI.Lib.ScrollBar.PanelScrollHelper flowphelper ;
gunaLabel_date = DateTime.Now.ToLongDateString();
}
private void gunaVScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
}
private void userControl17_Load(object sender, EventArgs e)
{
}
}
}
I have three buttons in a window form to open 3 differnet forms, but these are opening same form which should be opened by button1 only.
public partial class Home : Form
{
public Home()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Task addnew = new Task();
addnew.Show();
}
private void button2_Click(object sender, EventArgs h)
{
Task History = new Task();
History.Show();
}
private void button3_Click(object sender, EventArgs v)
{
Task Evaluate = new Task();
Evaluate.Show();
}
}
if your form name are Task , History and Evaluate then Initialize your three from in three method
public partial class Home : Form
{
public Home()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Task addnew = new Task();
addnew.Show();
}
private void button2_Click(object sender, EventArgs h)
{
History history = new History();
history.Show();
}
private void button3_Click(object sender, EventArgs v)
{
Evaluate evaluate = new Evaluate();
evaluate.Show();
}
}
First, pardon my new-ness, I just started coding class recently. Now, upon startup, I want parts of my form (c#) to not be shown, however when I put
NameDisplay.Visible = false;
(NameDisplay being the label I wish to hide) into my Form1.cs it gives me the error of that it is a 'field' being used as a 'type'. How do I correct this, and apply to other object types (buttons, textboxes, etc?)
EDIT 1-
Code- as it stands
namespace ATM
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Label NameDisplay;
NameDisplay.Visible = false;
private void Form1_Load(object sender, EventArgs e)
{
}
private void StartButton_Click(object sender, EventArgs e)
{
}
private void NameDisplay_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Remove Label NameDisplay;, and place NameDisplay.Visible = false; into your FormLoad event.
The loading of a form is an event just like clicking a button, and will execute the code like so.
Also, when I hide labels, I use .Hide(), but I believe that only works on WinForms.
Hope this helps!
You need to drag and drop the Label on the form and object will be created and initialized automatically in InitializeComponent.
In the form constructor (after InitializeComponent function) or Form_Load event, you may set the visibility to false
For example:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
NameDisplay.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void StartButton_Click(object sender, EventArgs e)
{
}
private void NameDisplay_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
I would like to create custom Eventargs for a series of events. I am using a third party X/Y scope where I plot Strength vs frequency. This scope has the ability to place "Markers" on it which are just little triangles at various frequencies. These markers support events such as when the mouse enters the marker, a click is performed, and the mouse leaves the marker. So for two markers, here is the code:
private void createEvents()
{
this.scope2.MarkerGroups[0].Click += new EventHandler(Marker0_Click);
this.scope2.MarkerGroups[0].MouseEnter += new EventHandler(Marker0_Enter);
this.scope2.MarkerGroups[0].MouseLeave += new EventHandler(Marker0_Leave);
this.scope2.MarkerGroups[1].Click += new EventHandler(Marker1_Click);
this.scope2.MarkerGroups[1].MouseEnter += new EventHandler(Marker1_Enter);
this.scope2.MarkerGroups[1].MouseLeave += new EventHandler(Marker1_Leave);
}
// And now the event handlers
private void Marker0_Click(object sender, EventArgs e)
{
//do something;
}
private void Marker0_Enter(object sender, EventArgs e)
{
//do something
}
private void Marker0_Leave(object sender, EventArgs e)
{
// do something
}
private void Marker1_Click(object sender, EventArgs e)
{
//do something;
}
private void Marker1_Enter(object sender, EventArgs e)
{
//do something
}
private void Marker1_Leave(object sender, EventArgs e)
{
// do something
}
Now this is fine for two markers....but I need 80 of them. I could just write the whole thing out but there has to be a better way. So I started like this:
private void createMarkerEvents()
{
for (int i = 0; i < 80; i++)
{
this.scope2.MarkerGroups[i].Click += new EventHandler(Marker_Click);
this.scope2.MarkerGroups[i].MouseEnter += new EventHandler(Marker_Enter);
this.scope2.MarkerGroups[i].MouseLeave += new EventHandler(Marker_Leave);
}
}
private void Marker_Click(object sender, EventArgs e)
{
//do something;
}
private void Marker_Enter(object sender, EventArgs e)
{
//do something
}
private void Marker_Leave(object sender, EventArgs e)
{
// do something
}
So the question is how can I pass the actual marker number from the events to the event handlers?
There has got to be a way.
Thanks, Tom
If you want to identify marker group you may cast object sender to a MarkerGroup object
private void AnyMarker_Click(object sender, EventArgs e)
{
MarkerGroup group = (MarkerGroup)sender;
int indexOfMarkerGroup = this.scope2.MarkerGroups.IndexOf(group);
//do something;
}
OFF: You should define a custom EventArgs class:
public class MyEventArgs : EventArgs
{
public int MyCustomProperty {get;set;}
}
Then use it in your event:
public event EventHandler<MyEventArgs> ButtonPressed;
Fire event using custom args:
if(ButtonPressed != null)
{
ButtonPressed(this, new MyEventArgs { MyCustomProperty = 1 });
}
EDIT
Full example:
private void createMarkerEvents()
{
for (int i = 0; i < 80; i++)
{
this.scope2.MarkerGroups[i].Click += new EventHandler(Marker_Click);
this.scope2.MarkerGroups[i].MouseEnter += new EventHandler(Marker_Enter);
this.scope2.MarkerGroups[i].MouseLeave += new EventHandler(Marker_Leave);
}
}
private void Marker_Click(object sender, EventArgs e)
{
// When markergroup fires and event, it passes reference to itself as `sender` parameter
// so we can get access it
MarkerGroup mg = (MarkerGroup)sender; // this marker has fired a click event
// Now you know which marker has fired event
// if you want to determine it's index in MarkerGroups collection:
int index = this.scope2.MarkerGroup.IndexOf(mg);
// now you know MarkerGroup and it's index
}
private void Marker_Enter(object sender, EventArgs e)
{
//do something
}
private void Marker_Leave(object sender, EventArgs e)
{
// do something
}
Alrighty. Here is my problem. I have just about everything done. I just need to take input from the form, and then use it in an algorithm in the second form. I have everything else written up, I just need to know how to connect the 2 so I can write up the last of the code. I've done some research, but none of it seems to go with what I'm trying to do.
Here is the main form.
namespace Airplanes
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
}
private void Arrival_Click(object sender, EventArgs e)
{
ArrivalForm newForm;
newForm = new ArrivalForm();
newForm.ShowDialog();
}
private void Fuel_Click(object sender, EventArgs e)
{
Fuelform newForm2;
newForm2 = new Fuelform();
newForm2.ShowDialog();
}
private void Status_Click(object sender, EventArgs e)
{
}
private void Items_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void NameBox_TextChanged(object sender, EventArgs e)
{
}
private void FuelBox_TextChanged(object sender, EventArgs e)
{
}
private void GateBox_TextChanged(object sender, EventArgs e)
{
}
private void Singlebutton_CheckedChanged(object sender, EventArgs e)
{
}
private void PrivateButton_CheckedChanged(object sender, EventArgs e)
{
}
private void CommercialButton_CheckedChanged(object sender, EventArgs e)
{
}
}
}
And here is the form I'm trying to connect to the main form.
namespace Airplanes
{
public partial class Fuelform : Form
{
public Fuelform()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Fuelform_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Thanks in advance for any answers.
There are a couple of ways...the easiest would probably be to pass the data in through the constructor of your new form.
FuelForm newForm2 = new FuelForm(myData);
And then change the constructor for your FuelForm:
public FuelForm(int myData) // or whatever data type you need
{
// Deal with myData
}
In Source form
destinationForm df = new destinationForm ();
df .myValue= "My Value";
df .ShowDialog();
in Destination Form
private string destVariable;
public string myValue
{
get { return destVariable; }
set { destVariable= value; }
}
then you can use destVariable everywhere in destination form