Main window form showing up blank - c#

I've gone through a number of the other questions regarding this issue.
The form loads, but it's missing all its controls. It used to work. Clearly I screwed something up but I cannot figure out what I changed to break it.
I've checked and my UploaderUI.cs is spelled correctly. Its code is below:
namespace uploaderUI
{
static class UploaderUI
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new mainForm());
}
}
}
My InitializeComponent is working, and I am able to set a break point and step through all of it.
public mainForm()
{
InitializeComponent();
// Create an instance of a ListView column sorter and assign
// to the ListView control.
lvwColumnSorter = new ListViewColumnSorter();
this.listViewNotCurrent.ListViewItemSorter = lvwColumnSorter;
}
I'm at a loss, I don't know what else to look for. Not even sure what else to post here that can help.

I encountered the same problem. What I was doing was creating the form on a thread other than the "Main" form UI. This caused the "blank" form issue. Fix it by changing your code or ensuring the form creation happens on the UI thread:
mainForm.Invoke((MethodInvoker)delegate {
new Form();
});

Related

Update form from main method

So I have the template C# windows form, and made the debugInstructionsLabel label public so that I can edit it from outside the form, then added a few lines to main:
namespace test {
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 form = new Form1();
Application.Run(form);
form.debugInstructionsLabel.Text += "Aaaaaaa";
form.Refresh();
form.Update();
form.Invalidate();
Application.DoEvents();
}
}
}
However, this doesn't actually change anything in the form, how would I update the text?
EDIT: It seems like Application.Run doesn't return, should I create another thread or handle everything inside the form class?
You should put the line that changes the text of the label in the onLoad event of the form. Even on the constructor after initializecomponents is run would be fine. With that minor change it will work.
Hope this helps!

How to get hidden form from task bar to front in C# winform? From class

I am doing it from a class, because I've got code to make it so only one instance can work. I've seen lots of forums and just cannot figure it out. I've tried all sorts of methods like Application.OpenForms etc, nothing is working. The last form which was closed will be hidden in the taskbar. I just want to be able to bring that hidden form.
namespace EncryptionDecryption
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
bool instanceCountOne = false;
using (Mutex mtex = new Mutex(true, "MyRunningApp", out instanceCountOne))
{
if (instanceCountOne)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Login("", ""));
mtex.ReleaseMutex();
}
else
{
MessageBox.Show("An instance of this application is already running in the taskbar", "Note");
mtex.ReleaseMutex();
Environment.GetCommandLineArgs();
//Trying to pass the loggedinusername and role to new instance of the program so dont have to sign in again...
}
}
}
}
Instead of using the above I used this which plays with threads to make sure I only have one instance and it will open that one instance only with new parameters to! Which i really needed for my next step!
https://social.msdn.microsoft.com/Forums/vstudio/en-US/a5bcfc8a-bf69-4bbc-923d-f30f9ecf5f64/single-instance-application?forum=csharpgeneral

Sending variables between forms [duplicate]

This question already has answers here:
Communicate between two windows forms in C#
(12 answers)
Closed 7 years ago.
I am making a game, and I have 3 main forms that I am using, one is the start menu, one is the main game form, and the other is a create map form. My question is directed to how can I send variables between them. I know of the classic form setup, where you show one form from the other, and pass information like this...
Form1 form = new Form1();
form.Show();
form.varible = ...
But i don't like how the previous form is still showing, and form.ShowDialog isn't what I am going for either. I have a system where you cycle through forms, where it opens your new form and closes the old one, and it works great, except I don't know how to send information to the new form. I changed the Program.cs code to look like this
public static bool OpenForm1OnClose { get; set; }
public static bool OpenForm2OnClose { get; set; }
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
OpenForm1OnClose = true;
OpenForm2OnClose = false;
while (OpenForm1OnClose || OpenForm2OnClose)
{
if(OpenForm1OnClose)
Application.Run(new Form1());
if (OpenForm2OnClose)
Application.Run(new Form2());
}
}
This is just a testing program right now that switches between two forms - click one button and it goes to the other form and closes the one you were on. Here's what the button click code looks like
private void button1_Click(object sender, EventArgs e) // form1's button
{
Program.OpenForm2OnClose = true;
this.Close();
}
That is all the code right there, nothing more to it really. Any Idea's on how to pass variables would be awesome, or if its straight up impossible, advice on a good way to switch between forms would be helpful. Thanks for reading
If you're asking what I think, the answer is simple. You just need to have your constructor take the information as its args.
private var myInfo;
public Form1(var myInfo_)
{
InitializeComponent();
myInfo = myInfo_;
}
I hope this is in fact what you want.

Splash screen to show up in the middle of application

I have a list of report names displayed as Tree hierarchy in ReportViewer control. When user clicks on a report name, an input form loads, user enters some values and presses OK. At this point, Splash screen should load while the backend process is happening (connecting to DB, retrieving values etc). Once the report is loaded in Reportviewer editor, the splashscreen should close.
So far, I am able to display the splash screen however it gets stuck at that point, the actual report does not load and the splash screen stays on forever.
Is it possible to use splashscreen in the middle of application, not at app launch? If so, how do I continue with loading report?
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new SnapPlusReports());
//new SplashScreenApp().Run(args);
}
}
public class SplashScreenApp : WindowsFormsApplicationBase
{
private static SplashScreenApp _application;
public static void Run(Form form)
{
_application = new SplashScreenApp { MainForm = form };
_application.Run(Environment.GetCommandLineArgs());
}
protected override void OnCreateSplashScreen()
{
this.SplashScreen = new ShowProgress();
base.OnCreateSplashScreen();
}
}
I have done this before by making a new form at run time dynamically with code. Make sure you set all the options up, especially FormBorderStyle to none, or something like that so the user can't close it. Then simply manipulate labels that appear on that form, and eventually close it once your process is complete.
This way you don't have to worry about threading and a nice side effect is that the initial form won't be clickable.
For example I have an about form that pops up during run time (granted I don't change anything on it but the idea is there:
AboutForm aboutForm = new AboutForm();
aboutForm.StartPosition = FormStartPosition.CenterParent;
Label lblAbout = new Label();
Version applicationVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
lblAbout.Text = applicationVersion.ToString();
lblAbout.Location = new Point(145,104);
aboutForm.Controls.Add(lblAbout);
aboutForm.ShowDialog();
This shows the current programs version number, etc. There are other labels that already exist on the form (I created it visually first and then called an instance of it).
Hope this helps!
...Catch other instances and gracefully exit if you need only one copy of your app in memory at a given time
static void Main()
{
Application.EnableVisualStyles();
bool exclusive;
System.Threading.Mutex appMutex = new System.Threading.Mutex(true, "MY_APP", out exclusive);
if (!exclusive)
{
MessageBox.Show("Another instance of xxxx xxxBuilder is already running.","MY_APP",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation );
return;
}
Application.SetCompatibleTextRenderingDefault(false);
xxxWindowsApplication.InitializeApplication();
Application.Run(new frmMenuBuilderMain());
GC.KeepAlive(appMutex);
}
In the main form load you could do something like:
private void frmMenuBuilderMain_Load(object sender, EventArgs e)
{
//Show Splash with timeout Here--
if(!SystemLogin.PerformLogin())
{
this.Close();
return;
}
tmrLoad.Enabled = true;
}

Modifying a form object created by application.run(form1)

I'm new to C# and trying to figure out how to edit a form from another class. The form is created by the default VS approach, as so:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
I've created a method in the Form1.cs file, just as a test for how to update a label1 field that is in Form1. Here is the method:
public void UpdateLabel(string state = "Changed Text")
{
label1.Text = state;
}
The problem I'm having is that the Application.Run command doesn't provide for a named object of type Form1. So when I want to trigger the UpdateLabel method from Program.cs, like so:
XXX.UpdateLabel();
I don't have an object to target to access the form. If I were creating the form manually, then I believe this would work fine:
Form1 myForm = new Form1();
myForm.UpdateLabel();
With the Application.Run(Form1) that the Windows Form Application provides, how do I access the form object that is being created? Also, is this the approach I should be taking for this type of problem, or is there a better method?
Well, you could integrate your last example in this way....
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 myForm = new Form1();
myForm.UpdateLabel();
Application.Run(myForm);
}
but let me ask you: Why your logic dictates to do this outside the Form1 constructor?
public Form1()
{
InitializeComponents();
label1.Text = "Changed Text";
}
EDIT: Following the comments below I think you should work on something like this:
MyApplicationCode appCode;
public Form1()
{
InitializeComponents();
appCode = new MyApplicationCode();
this.Text = appCode.GetFormText();
label1.Text = appCode.GetLabelText();
cmdSave.Enabled = appCode.UserHasSavePermission();
...... // and so on for other decisions on
}
You could fix the immediate issue with:
Form1 form = new Form1());
form.UpdateLabel();
Application.Run(form);
As for the second question, it really depends on what you are trying to ultimately achieve.
So you have some fairly complicated logic that is used to determine the initial value of some controls in your form. Due to the complexity of that logic, you would prefer to extract that code from the definition of Form1 and move it to another class. That is all good so far.
You can create some other class, have Form1 use that class, and have it provide a value to Form1. Rather than having some other class that has-a Form1 (which would be the effect of putting your code in Main, Form1 should have that other class.
Implementing this is fairly simple. You create another class, you give it an instance or static method that returns a string. Form1 either calls the static method, or creates an instance of the class and calls the instance method. It then sets a label based on the results of that method.
Application.Run is typically used to launch a WinForms application until such a point that that application closes. What function of your Program.cs class is supposed to call UpdateLabel? Is your application launched externally with label value parameters?

Categories

Resources