Cannot change startup form in c# windows form application project - c#

In a windows application project (C#, Visual Studio 2010) I simply want to change to a new startup form (leaving the older one that has already been tested as it is). In program.cs, I comment out the "old" form and and introduce my new "clone":
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1()); // this working form uses ListBox
Application.Run(new GridForm()); // want this new form which uses DataGridView
}
Both Form1 and GridForm are in the same project and both have the same using directives. (snippet follows):
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.XPath;
using System.IO;
using System.Reflection;
using CBMI.Common;
using log4net;
using log4net.Config;
namespace CBMI.WinFormsUI
{
public partial class GridForm : Form
{
private string defaultRootFolder;
public static readonly ILog log = LogManager.GetLogger(typeof(GridForm));
public GridForm()
{
InitializeComponent();
}
Seems like a ridiculously simple question/issue but I am stumped.
EDIT-UPDATE:
Somehow in the cloning/"refactoring" process, I messed up the namespace on the newer form by introducing a case change. This was discovered by trying your suggestion as follows:
private void Form1_Load(object sender, EventArgs e)
{
CBMI.WinFormsUI.GridForm improvedForm = new CBMI.WinFormsUI.GridForm();
improvedForm.Show();
return;
Form1.cs has namespace coded as follows:
namespace CBMI.WinformsUI
GridForm.cs has namespace coded as follows (note the CamelCase):
namespace CBMI.WinFormsUI
EDIT-UPDATE: What is best practice for cloning/refactoring new form?
I forget how I hacked out the new form. I think I started by "add new item" to the project, gave it new name and saved. Then I manually copied over some code from the original and introduced new datagridview, etc. and removed the prior user interface controls from the new form. I've always found this a bit awkward trying to leave "version 1" working and introduce a new extended thing built up from the "old". How do the "pros" do this sort of thing?

That is the proper way to run the form you want. So there has to be an issue with how Main() relates to your new form. Like it being in the wrong namespace, or something to that effect.
Check it by trying to run an instance of you form from the existing startup form using:
Formname frm1 = new Formname();
frm1.show();

I assume you copied all files for Form1 (there may be up to three of them: Form1.cs Form1.Designer.cs and Form1.resx). Then did you change the class name in new *.Designer.cs? To GridForm ?
Other than this issue, there should be no problem with your code in Main. And you can test it just by adding a brand new form to the project and referencing it as you did.

Related

Open existing form with button

I have a login form, from which i want to open a "Home form", which i obviously made myself, when login is success. You can look at the part where it should open a form below. It should make a reference to the existing form right? I tried everything but i cant figure it out.
But it opens a new form instead of the form i called "HomeFRM".
How can i open that form?
this.Hide();
Form _HomeFRM = new Form();
_HomeFRM.Show();
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 DELETE
{
public partial class HomeFRM : Form
{
public HomeFRM()
{
InitializeComponent();
}
}
}
I suppose that your _HomeFrm form is defined in a file with this content (or like this)
namespace MyForms
{
public class HomeFrm : Form
{
public HomeFrm()
{
InitializeComponent();
.....
}
.... other methods and event handlers for the HomeFrm class
}
}
Now, if you want to create an instance of this class and show it your code should create the correct class, not the base class
// This is required to get access to all classes included
// in the MyForms namespace unless the following code is
// itself inside the same namespace....
using MyForms;
.....
this.Hide();
HomeFrm myHomeFrm = new HomeFrm();
myHomeFrm.Show();
As a side note, I suggest to not start your application with the Login form and the keep it hidden for the lifetime of your application. Insted start with the HomeFrm and inside the constructor start the Login form, save the result of the login in a global variable and in the Form_Load event handler decide if you want to continue or stop the application
Change to:
this.Hide();
_HomeFRM newForm = new _HomeFRM();
newForm.Show();
Your custom form is a new Class: "_HomeFRM"
You are creating a new instance of the Form Class (wich is the default empty one) called like your custom one.
Instead, you need to use your form as a Class.

C# Opening form directly from code and getting input

C# newbie here, I was wondering if there was a way to open a form in runtime, get the inputs from it, then close it. I am trying to create it as an object in runtime but can not access it.
var myForm1 = new myForm();
myForm1.Show();
However, though I created the form as another .cs file, I get a syntax error saying it can not be found. What am I missing? Or should I use something other than form?
EDIT:
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 ert
{
public partial class ConnectionForm: Form
{
public BaglantiBilgileri()
{
InitializeComponent();
}
}
}
This is my Form's code, I have created it as a separate form.cs file
var myForm = new ConnectionForm();
This is inside my main executed code block, it is a separate .cs file, and it says type or namespace could not be found, am I doing it the wrong way?
Please provide the error Message you get. Anyways opening a form at runtime and closing it when you have retrieved some data could be done like this:
using(Form myForm1 = new myForm())
{
//Initialize the components of your form
DialogResult result = myForm1 .ShowDialog();
if(result == DialogResult.OK)
{
//return whatever it is you want to return
//for example via a getter inside of myForm1
}
}
The DialogeResult of the form is set automatically when you close the form but you can also set it programatically from inside your form:
this.DialogResult = DialogResult.OK
after a specific event occured that makes you know when to retrieve the data.
You need to add the bellow line to the main form:
using ert;

I cant get SevenZipLib library to work (p/invoke)

Very basic knowledge on c# and is the first i use anything p/invoke related.
Help me pls, i have made this code but it doesnt seem to work.
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.Runtime.InteropServices;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
[DllImport("C:\\Users\\lchris\\Desktop\\SevenZipLib_9.13.2\\SevenZipLib\\SevenZipLib\\7z86.dll")]
public static extern void SevenZipArchive(string c);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (SevenZipArchive archive = new SevenZipArchive("file.rar"))
{
foreach (ArchiveEntry entry in archive)
{
Console.WriteLine(entry.FileName);
}
}
}
}
}
It tells me that SevenZipArchive is a 'Method' and is being used like a 'type'.
I have include the library to my project already, i just dont know how to use it.
Here is the library:
https://sevenziplib.codeplex.com/
You first need to remove this code:
[DllImport("...")]
public static extern void SevenZipArchive(string c);
You do not need to provide any p/invoke declarations. The library wraps that up for you.
This is a .net assembly. You use it just as you use any other. Take the following steps:
Download the project. You already did that I think.
Build the solution. Make sure that
In your project, add a reference to the assembly that you built in the previous stage. It's SevenZipLib\bin\Debug\SevenZipLib.dll or SevenZipLib\bin\Release\SevenZipLib.dll depending on the target you selected.
Add using SevenZipLib; to your project code to to gain access to the namespace.
Once you've done that your code will work. You can use the tests project that is supplied as part of the download as a rich source of example code.

How would i properly get 1 void from another form, to be used in a different form

Once again i need your help.. Anywho.. I've managed to get this code below to "work". the part there doesn't work is "this". I've no idea of what to use in static and how it works. I'm rather new to this aswell, so i might need some explenation for dummies.
Anyway. This code below is suppossed to be my "mainform", where everything is loaded like icons, size, settings, menu and whatever not.
Currently i'm trying to add a global "settings". which can be loaded from all the forms. So each invidual forms would be this.ClientSize = new System.Drawing.Size(1440, 900); and anything else i may add would have same impact on the form. like icons, opacity, whatever not.
Overally it's just a place to store settings there can be accessed from any other forms.
What i've written below here, is what i've managed so far. The SettingsOnProgramStart is recognised in my Settings form, but it does not change the Clientsize or the icon. It's pobably because of the "this" as it shows red lines.
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 lala.events
{
public partial class TrayMenu : Form
{
public TrayMenu()
{
InitializeComponent();
SettingsOnProgramStart();
}
}
public static void SettingsOnProgramStart()
{
//
// Load global settings.
//
this.Icon = new Icon("images/skin/global/icon.ico");
this.ClientSize = new System.Drawing.Size(1440, 900);
}
}
}
Settings file :
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 lala.events
{
public partial class Settings : Form
{
public Settings()
{
InitializeComponent();
TrayMenu.SettingsOnProgramStart();
}
}
}
Thank you for your time and sorry for any problems this may have caused :/
I'd love if anyone could give me a useful link to a "configuration for dummies", where i'd learn about using cfg, ini for my project. so i can get the invidual settings loaded from a file.
You need to pass the form whose settings need to be set in:
public static void SettingsOnProgramStart(Form formToSet)
{
//
// Load global settings.
//
formToSet.Icon = new Icon("images/skin/global/icon.ico");
formToSet.ClientSize = new System.Drawing.Size(1440, 900);
}
Used as:
//When called from another form
TrayForm.SettingsOnProgramStart(this);
As to settings, see MSDN application settings: http://msdn.microsoft.com/en-us/library/k4s6c3a0(v=vs.110).aspx
As an aside; this code doesn't really make sense as part of the TrayForm class, as its not related to that object at all. It should likely be a member of a GlobalSettingsManager class, or something similar.

The type or namespace name 'form2' could not be found (are you missing a using directive or an assembly reference?)

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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 myform = new Form2();
myform.ShowDialog();
}
}
}
The following code should "launch" form2. They are in the same project, and have the same namespace. I don't understand. I have tried changing the .NET framework from version 4 client to version 4.
Any other ideas?
right click on Form2, chose "resolve" and pick one of the resolutions, alternatively move it to the WindowsFormsApplication3 namespace.
In my case I created form2, but renamed it later.
I found that although the name of the form changed, the name of the class remained as I originally created it.
For example:
1. Project->Add Windows Form (Setup.cs)
2. File->Save Setup.cs as... (frmSetup.cs)
3. Add code: frmSetup SetupForm = new frmSetup();
However, the actual name of the Setup class did not change with the file name, so changing my code to:
Setup SetupForm = new Setup();
So I was referencing the class name and not the file name solved the problem for me.
You can solve this issue in two wais:
You remove the namespace outside your form's class code or
Add a "using projectName" at the top of the code in your form.
Both forms should have the same namespace

Categories