Visual Studio think that winform is a variable - c#

So i've just started learning C#, and I came across a error saying that Form5 "is a variable but is used like a type". I have shown the page of code that has the error, but if you want to look at the bigger picture, here's my github repo ( I am a totally noob at gihub, so if anything is mispelled or totally wrong, I'm Sorry! : https://github.com/ValorZard/Chocobomb
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace animal_years
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
/*
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
*/
Form Form1 = new Form1();
Form1.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Form Form3 = new Form3();
Form3.Show();
}
private void button3_Click(object sender, EventArgs e)
{
Form Form2 = new Form2();
Form2.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Form Form4 = new Form4();
Form4.Show();
}
private void button5_Click(object sender, EventArgs e)
{
Form Form5 = new Form5();
Form5.Show();
}
}
}

Form5 "is a variable but is used like a type"
That is precisely correct. And clear. You're using the type name Form5 (which is a terrible, undescriptive name by the way) as a variable name too. A simple alternative is to write new Form5().Show(); -- why do you need a local variable in the first place?

Your class name in form5.cs file is outputDescriptionLabel. So you have to call that class name here. Use below code:
private void button5_Click(object sender, EventArgs e)
{
outputDescriptionLabel f = new outputDescriptionLabel();
f.Show();
}

Related

DataGridView showing table from MS Access Database. How do I program save button in bindingNavigator1 to update database?

I've been googling this all day. I have a form that uses the database to autofill some of the fields, but the data in the database needs to be able to be updated periodically. The easiest way to do this for us is to allow user to update fields in datagridview. I just cannot find how to save changes to database. Thank you for in advance for any tips, I'm new to this so it's been alot of googling and this is the first issue I haven't been able to find any clear answers to.
```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calculator
{
public partial class Database : Form
{
public Database()
{
InitializeComponent();
}
private void Database_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the
'tilesDataSet.Tiles_and_Quantities' table. You can move, or remove it, as needed.
this.tiles_and_QuantitiesTableAdapter.Fill(this.tilesDataSet.Tiles_and_Quantities);
}
private void runtimeCalculatorToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
var form1 = new Form1();
form1.Show();
}
private void standardCalculatorToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
var form3 = new Form3();
form3.Show();
}
private void savedResultsToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
var form2 = new Form2();
form2.Show();
}
private void tiles_and_QuantitiesBindingNavigatorSaveItem_Click(object sender,
EventArgs e)
{
}
private void tiles_and_QuantitiesDataGridView_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{
}
}
}
```
I know there are many examples to this, but mostly using SQL, and I'm using an Access database. There are still examples showing this but I am pretty green at this and am struggling to implement them. Currently, I'm trying:
```
private void saveToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.Validate();
this.tiles_and_QuantitiesBindingSource.EndEdit();
this.tiles_and_QuantitiesTableAdapter.Update(this.tilesDataSet.Tiles_and_Quantities);
MessageBox.Show("Update successful");
}
catch (System.Exception ex)
{
MessageBox.Show("Update failed");
```
and am getting an error CS1061 in .Update(). Again, any help would be greatly appreciated.

Connecting Two Forms

When I try to connect two forms, I successfully connect them, but the problem is that when I open Form2 from Form1 then Form2 opens but Form2 does not show its buttons, text etc.
I used these instructions.
I am using Visual Studio 2012. I believe Visual Studio is doing this mistakenly and I might have to install new visual studio.
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Login_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}
// Create Form2.
public class Form2 : Form
{
public Form2()
{
Text = "Form2";
}
}
}
}
Form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Login_Form
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}
Now remove following code snippet from Form1 class
// Create Form2.
public class Form2 : Form
{
public Form2()
{
Text = "Form2";
}
}

C# Windows Forms - Textbox in Form2 into List item Form1 [duplicate]

This question already has answers here:
Send values from one form to another form
(20 answers)
Closed 7 years ago.
I am having a problem adding text from my Form 2, to the List item in Form 1. Could anyone help please, Sorry if it's a bit jumbled, I'm new with coding :) What I am trying to achieve is a to-do list application, in the 2nd form, text should be entered in a text box - when the user clicks enter, this text should be entered into the list item.
Another question is, in this case would I be better using a list item or a list box?
Here is the code:
FORM 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ToDoList
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void button2_Click(object sender, EventArgs e, string tbtext)
{
//Form1 form1 = new Form1();
//form1.listBox1.Items.Add(tbtext);
}
public void button1_Click(object sender, EventArgs e)
{
this.Close();
}
public void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
ListViewItem item = new ListViewItem(textBox1.Text);
Form1 f1 = new Form1();
f1.listView1.Items.Add(item);
}
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
}
}
}
FORM 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ToDoList
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void filesToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
public void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void button2_Click(object sender, EventArgs e)
{
}
public void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void F1Button1_Click(object sender, EventArgs e)
{
}
private void tB1_TextChanged(object sender, EventArgs e)
{
}
public void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
{
}
}
}
You create different object Form1, and the object is destroyd after you quit button2_Click. Is it local variable.
You need to create the datasource for your TODO list and propaget it to both forms, for example by method Set DataSource. Then you have one object accesible by
both forms.

button push increment number to a text box c# winform

This is for a project I'm doing for class, I'm trying to create a win form that will have 2 buttons one that will increment in the text box when the button is pushed and one that will decrement when a different button is pushed. I'm having trouble finding the right line that will do what I want. Is there someone that could help me out?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Project10TC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Teancum Project 10");
}
private void button1_Click(object sender, EventArgs e)
{
int i = 1;
textBox1.Text = Convert.ToString(i++);
}
private void button2_Click(object sender, EventArgs e)
{
int i = 1;
textBox1.Text = Convert.ToString(i--);
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Since its a class project, I can only give you a hint.
You need to define the variable i outside of your button click events. Use the same variable in both events.
Also look at the difference between i++ and ++i
Declare the i variable as a field. Besides I would use ++i instead of i++. Otherwise you have different values in the textbox and in the variable. Also, no need to use Convert.ToString().
public partial class Form1 : Form
{
int i;
public Form1()
{
InitializeComponent();
i = 0;
}
//...
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = (++i).ToString();
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = (--i).ToString;
}
}

C# TaskBar Item disappears when I hover over it

Has anyone got any idea why when I run the program, and go to click the taskbar item to open the small text entry area the icon disappears as soon as I get to it!!!
Thanks very much
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace systemTray
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
this.Visible = false;
}
private void Form1_Resize(object sender, System.EventArgs e)
{
if (FormWindowState.Minimized == WindowState)
{
Hide();
}
}
private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
{
var screen = Screen.PrimaryScreen;
this.Left = screen.WorkingArea.Right - this.Width;
this.Top = screen.WorkingArea.Bottom - this.Height;
Application.Run();
}
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void quitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
}
}
edit: I am not sure if this helps but to make the application not open the form I changed the main method from
Application.run(new form1())
to
new form1()
Application.Run is use to run your windows form application,
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
when you delete the line Application.Run(new Form1()); then your application just started and call Main() and after that it closed because it has finished it's work.
the question is why you delete Application.Run(new Form1());??

Categories