I want create a new component which shows a splash screen when i call the .show() method. The component must be like a Windows Form with an image and a duration in msec passed like parameters. What type of project should I choose in Visual Studio for do that? If I choose a ClassLibrary, it create a dll Class but if I choose a new ControlLibrary it create a new control, but I can't use a Windows Form.
protected int nSec;
public SplashScreen(string img, int nSec)
{
// duration
this.nSec = nSec;
// background splash screen
this.BackgroundImage = Image.FromFile("img.jpg");
InitializeComponent();
}
private void SplashScreen_Load(object sender, EventArgs e)
{
timer1.Interval = nSec * 1000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.Close()
}
I want reuse this "component" in other future work without create a new one every time.
Avoid assuming there's magic behind these project templates, you can easily configure the project yourself. Using the Class Library project template is fine, just right-click the project after you created it, pick Add New Item and select "Windows Form". Other than adding the form and opening it in the designer, that also added two items to your project's References node: System.Drawing and System.Windows.Forms
Which you automatically get when you pick the "Windows Forms Control Library" project template. Which also automatically added a UserControl. Which you don't need, just right-click the UserControl1.cs item in the project and pick Delete. Add New Item to select "Windows Form", just as above. Two ways to get the same result.
Sounds like they want you to make a class library and have it create the form for you.
//Whatever other usings you want
using System.Windows.Forms; //Include the win forms namespace so you create the form
namespace ClassLibrary1
{
public static class Class1
{
public static Form CreateNewForm()
{
var form1 = new Form();
form1.Width = 200;
form1.Height = 200;
form1.Visible = true;
form1.Activate(); //Unsure if you need to call Activate...
//You're going to want to modify all the values you want the splash screen to have here
return form1;
}
}
}
So in another project, say a console app, I can just reference the class library I just made, call the CreateForm function and it's gonna make a form at runtime pop up with a width and height of 200.
using ClassLibrary1; //You'll need to reference this
//Standard console app template
static void Main(string[] args)
{
var x = Class1.CreateNewForm(); //Bam form pops up, now just make it a splash screen.
Console.ReadLine();
}
Hope that's what you were looking for
Related
I am totally blind, I use NVDA screen reader to operate my system, and I am trying to create a Windows desktop application. Visual Studio isn't screen-reader friendly, at least not the forms designer part. VS Code is pretty much accessible, although powershell's protools' designer isn't.
Whenever I see a question about writing GUI in WPF with pure code I find a lot of "why in the name of God would you want to do that? Use VS!" answers. Well, here is the reason, VS form designer is out of reach to blind programmers, so the least practical approach will have to do.
So, can it be done? Would you suggest any resources or approaches to this?
I'm going to try and build up from the other example to get you compile-able code. To get WPF working, you'll need a project that builds with WPF support.
The simplest way is to create a .csproj ourself and use the previously posted code-only solution.
So, start with a minimal App.csproj file as so:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>
Then the app & window code in App.cs as before. I've added a couple controls to it, as you'll need a container anyway (I used StackPanel) if you want to start adding more stuff later.
using System;
using System.Windows;
using System.Windows.Controls;
public class App : Application {
protected override void OnStartup(StartupEventArgs e) {
base.OnStartup(e);
new MainWindow().Show();
}
[STAThread]
public static void Main() => new App().Run();
}
public class MainWindow : Window {
protected override void OnInitialized(EventArgs e) {
base.OnInitialized(e);
var panel = new StackPanel();
this.Content = panel;
var label = new Label { Content = "Click me:" };
panel.Children.Add(label);
var closeButton = new Button { Content = "Close", Height = 100 };
closeButton.Click += (sender, args) => this.Close();
panel.Children.Add(closeButton);
(this.Width, this.Height) = (200, 200);
}
}
You then build the whole project by using dotnet build. And you can run it with dotnet run or invoking the exe directly.
Alternatively, if you'd like to explore a WPF project with separate .xaml and .xaml.cs code, you can create your project using dotnet new wpf -o MyProjectName. This will produce a project, a xaml-based App, and a xaml-based form.
Yes. You can do WPF only with code.
Here is a simple example from http://www.java2s.com/Tutorial/CSharp/0470__Windows-Presentation-Foundation/CodeOnlyWPFApplicationSample.htm
using System;
using System.Windows;
using System.Windows.Controls;
public class App : Application {
protected override void OnStartup(StartupEventArgs e) {
base.OnStartup(e);
MainWindow window = new MainWindow();
window.Show();
}
[STAThread]
public static void Main()
{
App app = new App();
app.Run();
}
}
public class MainWindow : Window {
protected override void OnInitialized(EventArgs e) {
base.OnInitialized(e);
Button closeButton = new Button();
closeButton.Content = "Close";
this.Content = closeButton;
closeButton.Click += delegate {
this.Close();
};
}
}
Also, it might be easier to use WinForms since it uses C# as code-behind and less XML.
In my application I have two winforms. The first acts as my control panel and the second I use to take screen shots. However, when I go from Winform 2 back to Winform 1 I have a new winform created and a brand new tray icon. This is on top of the initial ones I create when the program first starts.
When I go from winform 1 to winform 2 I do the following:
this.Hide();
Form2 form2 = new Form2();
form2.InstanceRef = this;
form2.Show();
Then when I want to go back from Winform 2 to Winform 1 I do the following:
this.Close();
Winform1 form;
form = new Winform1 (capturedImageObj);
form.Show();
I know straight off the bat the issue falls on the fact I'm creating a new Winform1, but I need to do that so I can pass my capturedImageObj back into Winform 1.
I've tried calling this.close() and this.dispose() in the my first section of code but that only closes the program down. Is there a way I can dispose of Winform 1 but still use Winform 2 and pass the object I need to back into a new copy of Winform 1?
Here is the constructor for my Winform 1:
public ControlPanel(Image imjObj)
{
InitializeComponent();
_screenCap = new ScreenCapture();
_screenCap.OnUpdateStatus += _screen_CapOnUpdateStatus;
capturedImage = imjObj;
imagePreview.Image = capturedImage;
}
Change Winform1 to use properties like this:
public Image CapturedImage {
get { return imagePreview.Image; }
set { imagePreview.Image = value; }
}
Then change your constructor like this:
public ControlPanel(Image imjObj)
{
InitializeComponent();
_screenCap = new ScreenCapture();
_screenCap.OnUpdateStatus += _screen_CapOnUpdateStatus;
CapturedImage = imjObj;
}
And, finally, change your Winform2 to do this:
((Winform1)this.InstanceRef).CapturedImage = capturedImageObj;
this.InstanceRef.Show();
this.Close();
Based on the comment, it sounds like your InstanceRef property is of type Form. Change it to be of type Winform1. Or, I changed the code above to do some casting for you.
In VB.NET there is an option to add a Splash Screen when you click Add New Window but when I do that with C#, I can't find any thing.
so
How I can add a Splash Screen in C#?
(I'm on my Mac now so I may be a bit rusty...)
You need to open up your project preferences.
Project -> Preferences
In the first tab, select there is a dropdown menu called "Startup Object". The default is Form1.csYou should be able to change that to a splash screen window.
In Visual Studio 2010, this is really easy. Simply add an image to your Solution, then right-click on the image, and set it's Build Action to "SplashScreen".
No coding required!
Add a reference to Microsoft.VisualBasic. Then write the following code in Program.cs
static class Program
{
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new MyApp().Run(args);
}
public class MyApp : WindowsFormsApplicationBase
{
protected override void OnCreateSplashScreen()
{
this.SplashScreen = new MySplashScreen();
}
protected override void OnCreateMainForm()
{
// Do stuff that requires time
System.Threading.Thread.Sleep(5000);
// Create the main form and the splash screen
// will automatically close at the end of the method
this.MainForm = new MyMainForm();
}
}
}
Basically, I have a settings window, and when you click "OK", it's suppose to apply settings to the main form (eg, set font of a control, etc), and then close.
frmmain frm = new frmmain();
frm.OLVAltBackColor = Color.Aquamarine ;
I tried that, but it only applies the settings to that instance, and you can see it if you do frm.Show();
I'm trying to make it so the already opened form has it's control's properties changed.
What you are trying to do is not working because you are creating a NEW instance of your main form and updating that rather than the first instance. It is possible to update the main form by keeping a reference to it in your settings form... but...
...it sounds like you are approaching this from the wrong direction.
Don't make the settings form dependent on the main form. Instead create the settings form from the main dialog.
class SettingsForm : Form
{
// You need to ensure that this color is updated before the form exits
// either make it return the value straight from a control or set it
// as the control is updated
public Color OLVAltBackColor
{
get;
private set;
}
}
In your main form
(I'm assuming some kind of button or menu click)
private void ShowSettingsClicked(object sender, EventArgs args)
{
using (SettingsForm settings = new SettingsForm())
{
// Using 'this' in the ShowDialog parents the settings dialog to the main form
if (settings.ShowDialog(this) == DialogResult.OK)
{
// update settings in the main form
this.OLVAltBackColor = settings.OLVAltBackColor;
}
}
}
Apply the property change to the form that already exists and is already shown instead of creating a new form and changing that one.
In this code you're creating a new instance of the frmmain. Any changes you make to that new object will happen in the new object, not the one you actually want to change.:
frmmain frm = new frmmain(); //Creating a new object isn't the way.
frm.OLVAltBackColor = Color.Aquamarine ;
What you're looking for is a way to call on the already existant frmmain class and change the property of that.
Edit, for example:
using System;
class Statmethod
{
//A class method declared
static void show()
{
int x = 100;
int y = 200;
Console.WriteLine(x);
Console.WriteLine(y);
}
public static void Main()
{
// Class method called without creating an object of the class
Statmethod.show();
}
}
I'm new to C# and trying to write a simple GUI Unit Test with NUnit and NUnitForms. I want to test if a click on a button opens a new Form.
My application has two forms, the main form (Form1) with a button that opens a new form (Password):
private void button2_Click(object sender, EventArgs e)
{
Password pw = new Password();
pw.Show();
}
The source for my Test looks like this:
using NUnit.Framework;
using NUnit.Extensions.Forms;
namespace Foobar
{
[TestFixture]
public class Form1Test : NUnitFormTest
{
[Test]
public void TestNewForm()
{
Form1 f1 = new Form1();
f1.Show();
ExpectModal("Enter Password", "CloseDialog");
// Check if Button has the right Text
ButtonTester bt = new ButtonTester("button2", f1);
Assert.AreEqual("Load Game", bt.Text);
bt.Click();
}
public void CloseDialog()
{
ButtonTester btnClose = new ButtonTester("button2");
btnClose.Click();
}
}
}
The NUnit output is:
NUnit.Extensions.Forms.FormsTestAssertionException : expected 1 invocations of modal, but was invoked 0 times (Form Caption = Enter Password)
The Button Text check is successfull. The problem is the ExpectModal method. I've also tried it with the Form's name but without success.
Does anyone know what might be wrong?
I figured it out myself. There are two ways Windows Forms can be displayed:
Modal: "A modal form or dialog box must be closed or hidden before you can continue working with the rest of the application"
Modeless: "... let you shift the focus between the form and another form without having to close the initial form. The user can continue to work elsewhere in any application while the form is displayed."
Modeless Windows are opened with the Show() method and Modal ones with ShowDialog(). NUnitForms can only track Modal Dialogs (that's also why the method is named 'ExpectModal').
I changed every "Show()" to "ShowDialog()" in my source code and NUnitForms worked fine.
I would only change your source code to use ShowDialog() if you want your forms to actually be opened as Modal dialogs.
You are correct in that NUnitForms supports "Expect Modal", but does not have an "Expect Non-Modal". You can implement this yourself with relative ease using the FormTester class. FormTester is an extension class available from the latest repository for NUnitForms. You pass in the string of the .Name property of the form you are checking to see if it is shown.
public static bool IsFormVisible(string formName)
{
var tester = new FormTester(formName);
var form = (Form) tester.TheObject;
return form.Visible;
}