Windows Forms variables generated automatically - c#

I'm following the example from "Programming C# 4.0, 6th edition" for working with Windows Forms, but I get to a point where I can't understand what actually happens. I have 3 files
One with Main() :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace ToDoList
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
one to work with the fields of the form:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class ToDoEntry
{
public string Title { get; set; }
public string Description { get; set; }
public DateTime DueDate { get; set; }
}
And the form itself :
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 ToDoList
{
public partial class Form1 : Form
{
private BindingList<ToDoEntry> entries = new BindingList<ToDoEntry>();
public Form1()
{
InitializeComponent();
entriesSource.DataSource = entries;
CreateNewItem();
}
private void bindingSource1_CurrentChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void CreateNewItem()
{
ToDoEntry newEntry = (ToDoEntry)entriesSource.AddNew();
newEntry.Title = "New entry";
newEntry.DueDate = DateTime.Now;
entriesSource.ResetCurrentItem();
}
private void entriesSource_ListChanged(object sender, ListChangedEventArgs e)
{
switch (e.ListChangedType)
{
case ListChangedType.ItemAdded:
MakeListViewItemForNewEntry(e.NewIndex);
break;
case ListChangedType.ItemDeleted:
RemoveListViewItem(e.NewIndex);
break;
case ListChangedType.ItemChanged:
UpdateListViewItem(e.NewIndex);
break;
}
}
private void MakeListViewItemForNewEntry(int newItemIndex)
{
ListViewItem item = new ListViewItem();
item.SubItems.Add("");
entriesListView.Items.Insert(newItemIndex, item);
}
private void UpdateListViewItem(int itemIndex)
{
ListViewItem item = entriesListView.Items[itemIndex];
ToDoEntry entry = entries[itemIndex];
item.SubItems[0].Text = entry.Title;
item.SubItems[1].Text = entry.DueDate.ToShortDateString();
}
private void RemoveListViewItem(int deletedItemIndex)
{
entriesListView.Items.RemoveAt(deletedItemIndex);
}
}
}
My problem is with saying that The name entriesListView does not exist in the current context. Which is true, but for example entriesSource.DataSource = entries;
I don't have entriesSource either but for some reason I can use it. Now I'm not sure if the missing class(at least I think it should be a class) is something that VS2010 (Yes I'm using Visual Studio 2010) should generate but then - why it hasn't.
Is something that I should write manually but then, there's nothing about this in the example and I too have no idea how to define entriesListView.

These variables are being created by Visual Studio in a designer file. This is done through the use of the partial keyword, which is a feature in C# that allows you to split a class definition into two or more files.
The designer file will be called Form1.designer.cs in this instance. You can see which controls have been created by opening this file, or, if using Visual Studio, by opening the form in the Visual Studio Designer. To open the designer file in Visual Studio, expand the Form1.cs entry in the Solution Explorer TreeView, and double-click the designer file.
In your code above, the problem appears to be that the entriesListView variable does not exist. It is possible that it was created at one point, and then deleted (this can even happen inadvertently due to Visual Studio bugs). Adding a new ListView to your form and setting the Name property to entriesListView should correct the problem.

Related

C# startup form unable to be shown

This is my first assignment using C# and I've learned briefly about programming with Python in my pre-u. The C# form that I would like to show the user upon logging in can't be shown and instead, the program displays the Built errors msg, where I click on 'Yes' to continue running the last successful build. However, it will show one of the previous forms (which I have deleted from the program) instead - making it hard for me to test run and check for any other errors in my program.
The form I would like to show is TechnicianHome.
Error
There were built errors. Would you like to continue and run the last successful build?
Any help is much appreciated.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ASSIGNMENT
{
public partial class TechnicianHome : Form
{
public static string name;
public TechnicianHome()
{
InitializeComponent();
}
public TechnicianHome (string n)
{
InitializeComponent();
name = n;
}
private void TechnicianHome_Load(object sender, EventArgs e)
{
lblWelcome.Text = "Welcome" + name;
}
private void button1_Click(object sender, EventArgs e)
{
ServiceRequests vr = new ServiceRequests();
this.Hide();
vr.ShowDialog();
}
private void button1_Click_1(object sender, EventArgs e)
{
UpdateProfile up = new UpdateProfile();
this.Hide();
up.ShowDialog();
}
}
}

C# -Last edited Textbox not being saved to database

I have a C# program that is used for data entry and retrieval from a SQL Database. Generally it works very well however there is a saving error I am trying to get to the bottom of. It happens under this specific circumstance.
I update data in a bound textbox.
without leaving the textbox I then press the save button.
When I do this the data in the the bound textbox is not saved to the SQL Database.
This is the program
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;
using System.Data.Entity;
namespace Labassist2017
{
public partial class Form1 : Form
{
//Declare entity objects
//____________________________________________________________________________________________
public Lab_Assistant_backendEntities Database_Backend;
public DbSet Sample_Register;
public DbSet Product;
//____________________________________________________________________________________________
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Load_Data();
}
public void Load_Data()
{
Database_Backend = new Lab_Assistant_backendEntities();
Sample_Register = Database_Backend.Sample_Register;
Sample_Register.Load();
sample_RegisterBindingSource.DataSource = Sample_Register.Local;
Product = Database_Backend.Products;
Product.Load();
productBindingSource.DataSource = Product.Local;
}
private void Link_To_Form()
{
}
private void sample_RegisterBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
Save();
}
public void Save()
{
Database_Backend.SaveChanges();
}
}
}
the data is saved under the following circumstance.
I update data in a bound textbox.
I leave the textbox and then press the save button.
Clarification: when I say leave I mean that I press tab and exit the textbox so that it no longer has focus.
Why is this happening?
What I needed to do was call .EndEdit() in my save method.

Does not contain a static 'Main' method suitable for an entry point in Windows Forms

I am trying to make some library software (For an assignment), but keep getting this error, Despite it working 20min before.
I did originally make this from a Windows Form Application, but do not know what went wrong :(
I have a feeling that this is a "simple" error to fix. But its answer still alludes me.
Program k:\LibrarySoftware\Library_Software\Library_Software\obj\Debug\Library_Software.exe' does not contain a static 'Main' method suitable for an entry point
Library.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Library_Software
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void lblYear_Click(object sender, EventArgs e)
{
}
private void btnAddBook_Click(object sender, EventArgs e)
{
if (Application.OpenForms["Add_Book"] != null)
{
//you can use closing or hiding method
Application.OpenForms["Add_Book"].Close();
//Application.OPenForms["Add_Book"].Hide();
}
Add_Book B = new Add_Book();
B.Show();
}
private void lblNarrator_Click(object sender, EventArgs e)
{
}
private void cbxBookType_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void btnLoadBooks_Click(object sender, EventArgs e)
{
}
private void btnDeleteBook_Click(object sender, EventArgs e)
{
if (Application.OpenForms["Form2"] != null)
{
//you can use closing or hiding method
Application.OpenForms["Form2"].Close();
//Application.OPenForms["Add_Book"].Hide();
}
Form2 D = new Form2();
D.Show();
}
}
}
Also if there are any other errors please point them out
You are missing a main method in your project. The form does not initialize itself, it must be initialized in a method which acts as an entry point for your application.
Creating a new winforms project in VS2012 yields this class:
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
If you do not have a class file "Program.cs" in your project create one and add the code I just provided. The application should then run as expected.
My guess is you deleted the class by accident or something like that.
You need to put your form file into WindowsFormsApplication project.
In your visual studio:
- Go to File -> New Project
- Search for Windows Forms Application
- Fill the textboxes and click Finish
Now you can hit F5 to start the application and eventually add your library form to solution

How to change user control label.text from a form

Right so I have a user control called "ModbusMaster" and a form with literally a single button on it..
When I click the button I want to change the text of a label on my control..
However nothing happens..
Here is the main form
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 ModbusMaster_2._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ModbusMaster mb = new ModbusMaster();
public void button1_Click(object sender, EventArgs e)
{
mb.openPort("wooooo");
}
}
}
I am calling the method openPort and passing the string "wooo" to it..
here is my control
The text does not get updated :(:(:(
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace ModbusMaster_2._0
{
public partial class ModbusMaster : UserControl
{
string portName = "COM1"; //default portname
int timeOut = 300; //default timeout for response
SerialPort sp = new SerialPort();
public ModbusMaster()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
portLabel.Text = portName;
}
public void openPort(string port)
{
statusLabel.Text = port;
}
/*
* Properties
*/
public string SerialPort //Set portname
{
get { return portName; }
set { portName = value;}
}
public int TimeOut //Set response timeout
{
get { return timeOut; }
set { timeOut = value; }
}
}
}
I think you must have two instances of ModbusMaster.
One of them is the one you can see on the display, and is NOT being updated.
The other one is the one you create in class Form1 with the line of code:
ModbusMaster mb = new ModbusMaster();
That is the one you are modifying, but it isn't the displayed one (I cannot see anywhere that you can be displaying that).
What you need to do is use the reference to the actual displayed one instead when you call mb.openPort("wooooo");
[EDIT]
Thinking about it - it's possible that you haven't instantiated another user control at all.
Did you use Visual Studio's Form Designer to add the user control to your main form? I had assumed that you did, but now I realise that might not be the case.
If not, you should do that, give it the name mb and remove the line that says ModbusMaster mb = new ModbusMaster(); and it might work without you having to make more extensive changes.
You are creating your UserControl but not assigning it to your Form's Control Collection. Try something like this in your Constructor.
namespace ModbusMaster_2._0
{
public partial class Form1 : Form
{
ModbusMaster mb = new ModbusMaster();
public Form1()
{
InitializeComponent();
this.Controls.Add(mb); //Add your usercontrol to your forms control collection
}
public void button1_Click(object sender, EventArgs e)
{
mb.openPort("wooooo");
}
}
}

Dll does not seem to be read, why?

I am creating a application form to view/change a tag from a software called InTouch.
I added the dll as a reference and I would like to use the Read(string tagName) fct in the IOM.InTouchDataAccess. VS does not see the fct Read when I write InTouchWrapper TagType = new read(). It only sees InTouchWrapper as I wrote in the code which gives me the error IOM.InTouchDataAccess.InTouchWrapper' does not contain a constructor that takes 0 arguments
I don't understand why is this happening. I am running the InTouch software while coding, maybe there is an access conflict with the software.
MyCode
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;
using IOM.InTouchDataAccess;
namespace TagBrowser
{
public partial class TagBrowser : Form
{
public TagBrowser()
{
InitializeComponent();
}
private void TagBrowser_Load(object sender, EventArgs e)
{
}
private void TagBox_TextChanged(object sender, EventArgs e)
{
}
private void TypeBox_SelectedIndexChanged(object sender, EventArgs e)
{
InTouchWrapper TagType = new InTouchWrapper();
}
The dll
using System;
using System.Collections.Generic;
using System.Text;
using NDde.Client;
namespace IOM.InTouchDataAccess
{
public class InTouchDdeWrapper : IDisposable
{
private int DDE_TIMEOUT = 60000;
private DdeClient _ddeClient;
public InTouchDdeWrapper()
{
_ddeClient = new DdeClient("View", "Tagname");
}
~InTouchDdeWrapper()
{
Dispose();
}
public void Initialize()
{
_ddeClient.Connect();
}
public string Read(string tagName)
{
return _ddeClient.Request(tagName, DDE_TIMEOUT).Replace("\0", "");
}
I'm putting this here in case somebody else would get the same problem:
Are you sure it's the correct dll you referenced? Try to open the
exact referenced dll in a decompiler (JustDecompile free,
Reflector or dotPeek free) and see if it's the code you
expect.

Categories

Resources