New coder, trying to make Label invisible on program start - c#

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)
{
}
}

Related

add pagebar in my form for to change page

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)
{
}
}
}

How to persist data on win form when changing forms

I am working on a multi form project in C# and I am having issues with the forms data persisting when changing between tabs. Each tab will open the respective form as shown below.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
loadForm(new FormHome());
}
#region FORM CALLER
public void loadForm(object Form)
{
if (this.gunaPanelHome.Controls.Count > 0)
this.gunaPanelHome.Controls.RemoveAt(0);
Form fetched = Form as Form;
fetched.TopLevel = false;
fetched.BringToFront();
fetched.Dock = DockStyle.Fill;
this.gunaPanelHome.Controls.Add(fetched);
this.gunaPanelHome.Tag = fetched;
fetched.Show();
}
private void gunaHomeButton_Click(object sender, EventArgs e) => loadForm(new FormHome());
private void gunaDelayButton_Click(object sender, EventArgs e) => loadForm(new FormDelay());
private void gunaAssemButton_Click(object sender, EventArgs e) => loadForm(new FormAssembly());
private void gunaBuildButton_Click(object sender, EventArgs e) => loadForm(new FormBuilder());
private void gunaSettingsButton_Click(object sender, EventArgs e) => loadForm(new FormSettings());
#endregion
}
I have data in other forms that I want to keep throughout the tab changes, and was wondering if there was an easier way besides building a constructor that set data through each action change. Overall, I would like to just .hide() the current form in the window.

I want to load my main code every 5 seconds?

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
}

Retrieve data from one form and use it in another form

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

Save user settings in c#

i try to save user settings in c#.
I can read the value and put it in a textbox but i can not change it.
This is my WindowsForm with an textbox and a save button:
namespace tool
{
public partial class Form4 : Form
{
string source = Properties.Settings.Default.Source;
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
textBox1.Text = source;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void save_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Source = source;
Properties.Settings.Default.Save();
Application.Exit();
}
}
}
And here is a picture with my settings:
I hope someone as an idea :-)
Thanks for help
Try this:
private void save_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Source = textBox1.Text;
Properties.Settings.Default.Save();
Application.Exit();
}
You need to use what is currently in the text box, not your member variable.
Or you can change this event to be as follows (and then you don't have to modify the above):
private void textBox1_TextChanged(object sender, EventArgs e)
{
source = textBox1.Text;
}
could you possibly have a read lock being applied to the key you're looking at while testing this? Maybe the code's not the problem?

Categories