I have used Google to find a solution on this without any luck, hoping you guys/girls can help me out.
Code :
private void funButtonToolStripMenuItem_Click(object sender, EventArgs e)
{
var FF = new FunForm();
FF.ShowDialog();
}
Very simple, opens up a second Form.
However, when this form launches it disables access to my main form.
Am I able to prevent this from happening?
Use FF.Show() rather than FF.ShowDialog()
Is FF.Show() what you want? It displays the second form non-modal. See MSDN for details.
Related
I've run into some problems when opening a new form. The first time the new form is opened the code runs as expected, but if I close that form and open a new one right after, the code has a problem executing correctly.
Observe that this is not the main form, this is a separate form that is opened from the main form.
I suspect the problem is lying in the Application.OpenForms that I'm running from a separate class. But I really can't figure out why it wouldn't run the second time.
Here is an example (this code runs from a separate class.):
In the textbox (txtCFProjectNr) I wrote "0000-0000", it returns 0000-0000 the first time the form is opened, the second time it returns "2".
static frmCFNewProject newProjFrm = Application.OpenForms.OfType<frmCFNewProject>().FirstOrDefault();
public static void projectNrChanged()
{
bool format = Regex.IsMatch(newProjFrm.txtCFProjectNr.Text, #"[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]");
}
Here is how I open the new form
public void btnCFCreateNewStructure_Click(object sender, EventArgs e)
{
frmCFNewProject frm = new frmCFNewProject();
frm.ShowDialog();
}
When I restart the application, it works like a charm, until the form is closed and opened again.
I hope this explains enough.
I apologize for my bad English, this is not my native language. Thanks for your understanding.
I know I'm a bit underqualified to be on here, but seeing as my teacher is no good, was wondering if you could help with this extremely obscure problem that i have.
In this task, we have been asked to create a quiz through windows form application on c#. To be honest, I actually have no idea what I'm doing and am just trying to follow a booklet my teacher has given me. However, it appears this booklet isn't working, and the problem to obscure to be answered with a youtube video.
The problem I have is that I am running the form, and recieving problems when I click a button to access a different form within the program, and instead of just opening the linked windows form, it appears both forms remain open, regardless of the this.Close(); that I have coded. When both forms are open, I am unable to access the sought after form, as when i attempt to click on it it cuts back to the form where I initially clicked the link label. I realise this is very badly written, and I have no idea what I'm doing compared to you guys, but any input whatsoever would be greatly appreciated.
I have linked screenshots below. Ideally I'd like for the Program to run frmSplashScreen, the timer running out, then frmPlayerLogin, where I would click the link label to access frmPlayerSelectionNew.
Screenshot link: http://imgur.com/a/K6RqE
Edit
I've been asked for the key code to be written here
The code on the frmPlayerLogin Screen relating to the link label is this.
private void lblClickToRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Form myNextScreen = new frmPlayerSelectionNew();
myNextScreen.Show();
this.Close();
}`
The code on Program.cs is as follows
`static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmSplashScreen());
Form myNextScreen = new frmSplashScreen();
myNextScreen.Show();
Application.Run(new frmPlayerLogin());`
You have two problems.
ShowDialog() is modal. That means when you use it, instead of Show(), you are halting further execution until you enter a response and return a DialogResult
myNextScreen.Show();
We can't see the rest of your code from that image, and there's a way to post code on here. If it's more than what I just pointed out, we'll need that to help.
I have a software in C# that has a main form (meaning the form that opens when you start a software like almost every software has). However, the main form opens a form called ProjectFiles . I would like to know if it is possible to add files to the listView in ProjectFiles from the main form. I have tried searching the internet for how to do this but everyone has been asking how to do it a different way. I have also tried doing this without help so here is my code but the problem is when I use this code, the software will stop responding.
main form:
public string[] lines { get; set; }
void projectFilesToolStripMenuItem1_Click(object sender, EventArgs e)
{
if (this.parentForm == null)
{
return;
}
if(opfd.ShowDialog()==DialogResult.OK)
{
string[]lines = File.ReadAllLines(opfd.FileName);
}
}
ProjectFiles:
void label1_Click(object sender, EventArgs e)
{
MainForm mf = new MainForm();
{
string[] lines = File.ReadAllLines(mf.lines.ToString());
foreach (string line in lines)
{
listView1.Items.Add(line);
}
}
}
Any help is greatly appreciated. Thanks!
Edit:
I was asked to post exactly what the error is in more detail. Like I said above, the software becomes greyed out, and a message box shows up saying it has stopped responding. If this is not clear enough, please let me know.
Edit: I have tried the below asnwers but the software still continues to do the same. It seems like too much for the software to handle.
First of all, when you call ShowDialog() to display a form, any code after this statement will not execute until that form is closed. See the Remarks section of this
Therefore, it is not possible to add files to the list view in the ProjectFiles from the MainForm in your case (ShowDialog)
But if you need to do that;
Change the access modifier of the listView1 in ProjectFiles to public
Keep the pointer to the ProjectFiles form as a member variable in MainForm
ProjectFiles theProjectFiles = new ProjectFiles();
Display the ProjectFiles form with .Show() instead of .ShowDialog()
theProjectFiles.Show();
Just use the listView1 in ProjectFiles form as usual:
theProjectFiles.listView1.Add(...);
I'm fairly new to Visual C# and I'm writing an GUI app with multiple forms. One form is main window, and the rest are some kind of option windows. When showing an option window, I need to load some data to it (for example a string to window's editbox), then edit it and return back to main window when closing option window. Is there any simple way I can achieve it?
I've found some solutions like, or c# event handling between two forms, but I can't really conform it to my needs. I was thinking about passing data in constructor, but how to get it back? I've found something about ShowDialog, but as I said I'm new to C# (started yesterday ^^) and don't know if I can use it.
Any ideas, please?
I found the following previous answer which outlines sending specific properties from the one form to another:
Send values from one form to another form
The using keyword will also ensure that the form is cleaned-up properly, here's a link to it's usage (pardon the pun...) : http://msdn.microsoft.com/en-us/library/vstudio/yh598w02.aspx
I've run into the same issue to be honest, and I have to say that prior to this discussion I would just pass the parent form itself to the child and alter it in that way. Such as:
ChildForm child = new ChildForm(this); //from the parent
and
public ChildForm(ParentForm parent)
{
this.parent = parent;
}
Probably not the best convention though, as you probably don't need to access that much from the parent as the child.
Thanks guys, I think I finally get it. Idle_Mind, your idea was the easiest in my point of view, so I decided to use it. If someone else has a problem like this, here's what I've coded:
In main window form: when button is clicked, a new form appears; after closing it, label1 shows the text typed in that form
private void Button1_Click(object sender, EventArgs e)
{
LoadDataForm loaddata = new LoadDataForm("initial value");
if (loaddata.ShowDialog() == DialogResult.OK)
{
label1.Text = loaddata.textBox1.Text;
}
}
In load data form: argument passed in form's constructor appears in textBox1; textBox1's Modifiers property has to be modified to "public"
public LoadDataForm(string initvalue)
{
InitializeComponent();
textBox1.Text = initvalue;
}
private void ApplyButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}
Regards,
mopsiok
I have a project that I'm doing with
Microsoft VSTO (office 2013 excel)
I have certain things that make calls that take maybe 10 seconds to come back.
Ideally I would like to display an progress bar or some status... After a lot of searching I found an article that is titled:
How do I create a splash screen window for the VSTO applications?
http://www.datazx.cn/Fv7p5a/xw/oa2v/2q7xs6/mcccjfti-988m-f8r8-8d44-bstb4rfsi4xm23rsdfd.html
So I started creating this code in a form, but then I realize that I need to call it up within my methods and really attach events etc...
The article says to
"display a modal form on a background thread" What is the best way to do this?
I find it easier to use modal less form on main thread and so far haven't seen any problem with modal less approach. Something like code below
var splashWindow = new SplashWindow();
splashWindow.Show();
splashWindow.SetMessage("Starting please wait...");
DoSomeWork(splashWindow);
splashWindow.Close();
Following you will see a way I programmed a Splash Screen for Excel-VSTO in C#. My Excel file is enabled for macros (.xlsm). These are the steps:
Create your splash screen. Let's assume the name of the form is SplashScreen.
Go to the code of the object ThisWorkbook.cs
Check the code looks like:
public partial class ThisWorkbook
{
SplashScreen SC = new SplashScreen();
private async void ThisWorkbook_Startup(object sender, System.EventArgs e)
{
SC.Show();
await Task.Delay(3500);
SC.Close();
more code...
}
}
It is important that you notice that I added the word async to the subroutine.
private void ThisWorkbook_Startup(object sender, System.EventArgs e)
I hope this is very useful.