How to use MessageBox in class library?
Here is my code
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 MessageBoxes
{
class ShowInfo
{
MessageBox.Show("test");
}
}
i can load MessageBox but can't have show property, MessageBox.Show("test"); <-- fail
You should NOT use a Windows forms MessageBox inside a class library. What if you use this library in an ASP.NET application. The MessageBox will be shown in Webserver. And your webserver will be waiting (hung) untill someone responds to that MessageBox in webserver.
An ideal design would be that you either return the message as string and deal with that string in caller specific way or throw an exception if thats what you want.
If you still want then here is your code corrected
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 MessageBoxes
{
class ShowInfo
{
public void ShowMessage(string msg)
{
MessageBox.Show(msg);
}
}
}
You have the call to messagebox outside any method.
This code cannot be compiled at all.
You should write
namespace MessageBoxes
{
class ShowInfo
{
public void ShowUserMessage(string messageText)
{
MessageBox.Show(messageText);
}
}
}
and then call it after instancing an object of type ShowInfo
ShowInfo info = new ShowInfo();
info.ShowUserMessage("This is a Test");
Additional Answer to this Question:
After the Class Library Project has been created.
Right Click your Project Add > New Item > Windows form
it's done by adding reference System.Windows.Forms.dll
Make sure you actually use the the class in the main form.
class ShowInfo
{
public static void show()
{
System.Windows.Forms.MessageBox.Show("test");
}
}
...
public Form1()
{
InitializeComponent();
ShowInfo.show();
}
Related
I am trying to access to a public static variable in a class but I don't know how to do it. I try different solutions like using and namespace but doesn't work.
In the form called "CategoriasCaracteristicas.cs" (blue), I am trying to access to empresaGlobal.cs (red) as you can see in the next image:
But I can't do it using namespace, or others. The code of the file "CategoriasCaracteristicas.cs" that is where I am trying to access the other class is the following:
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 System.Data.SqlClient;
using System.Configuration;
using Utils;
namespace imlnet
{
public partial class CategoriasCaracteristicas : Form
{
private string codEmp;
private string cadenaConexion;
public CategoriasCaracteristicas()
{
And this is the code of empresaGlobal.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ImeApps
{
public static class empresaGlobal
{
public static string empresaID = "3";
public static String EmpresaID
{
get { return empresaID; }
set { empresaID = value; }
}
}
}
Thanks in advance!
Put your empresaGlobal.cs file in a project and add that project as a reference to the GestorCategoriasCaracteristicasIngenieria project.
Make sure any classes, properties and methods you need to access are available using the public keyword.
I already follow the tutorial of adding sound in the c# but it cannot play the sound, but it is no error. How can I do?
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 System.Media;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SoundPlayer myPlayer = new SoundPlayer(WindowsFormsApplication2.Properties.Resources.sound1);
myPlayer.PlayLooping();
}
}
}
Make sure that you have the Form1_Load() assigned to the Form Load event. I've put in your code, and it works just fine.
To assign the event, just look at the following image..
This will ensure that the Form1_Load() is called when the form loads.
After having tested to see what would happen when you try to pass a non-.WAV file to the SoundPlayer, it WOULD produce an error:
Error 1 The best overloaded method match for 'System.Media.SoundPlayer.SoundPlayer(string)' has some invalid arguments.
This leads me to believe the code isn't being called.
Basically, I'm trying to move the contents of WriteLine to a Label box in a WinForm, as an intro to object oriented programming. I believe I have some syntax error, and I know the method I have the writeline in is void. So, any help with getting this to work is appreciated. This is just one of the attempts I've made.
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 ConsoleHelloWorld.Program;
namespace WindowsFormHelloWorld
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string words = new ConsoleHelloWorld.Program.Main(words);
label1.Text = words;
}
}
}
This is the code I'm referencing.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleHelloWorld
{
public static class Program
{
public static void Main(string[] args)
{
string words = "hello world";
Console.WriteLine(words);
Console.ReadLine();
}
}
}
Your words variable is a local one, i.e. its scope is the method where it is declared, that is Main. Outside that method you cannot reference it.
For the variable to be accessible, you need to make it a public field (or a property of the class). If you need to access it without having an instance of that class type, than this field should be declared static. Then your code would look like this:
public static class Program
{
public readonly static string Words = "hello world";
//...
}
Assuming the windows application project has a reference to the console application project in the windows application form you may write:
string words = ConsoleHelloWorld.Program.Words;
Also you don't need to launch the console application, besides you won't manage to do it like this (consider the answer by Cuong Le, if you meant to launch both Console and Windows applications).
Got it to work. Turns out I was rooting down incorrectly. (I had thought rooting worked a bit more like the android file structure.) Basically, I created a separate class for holding the Hello World variable, and just called that for whether I wanted to print the strings to a console or WinForm app.
The initial block of code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleHelloWorld
{
public static class Program
{
public static class Hello
{
public static string words = "Hello World";
}
public static void Main(string[] args)
{
string word = ConsoleHelloWorld.Program.Hello.words;
Console.WriteLine(word);
Console.ReadLine();
}
}
}
The WinForm calling the class:
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 ConsoleHelloWorld; //Program.Hello;
namespace WindowsFormHelloWorld
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string word = ConsoleHelloWorld.Program.Hello.words;
label1.Text = word;
}
}
}
In future projects I'll just keep the code being run by various environments in it's own project.
I added a CSharp Class to a form project. Now I want to access the form controls from the Class.
I am using this piece of code with the implicit var as follows:
var form = frmClasses.ActiveForm as frmClasses;
Using this code, I set the form Modifiers to public, place the code is a method/function and I am able to access the form controls in the class.
The problem is each time I need to call a form control I have to use the var and the entire line of code above. Is there a simpler way that is less redundant?
Example: Each method/function in a the class will need to call the form controls thus referencing the form controls using Var.
Take a look at the code below, you will noticed that I am using Var to call each form control.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace Classes
{
class ClsProperties
{
//VALIDATE TEXTBOX
public static void validateTextBox()
{
var form = frmClasses.ActiveForm as frmClasses;
if (form.txtMyTextBox.Text.Trim().Length == 0)
{
form.txtMyTextBox.BackColor = Color.Aquamarine;
}
}
//VALIDATE RADIO BUTTON
public static void validateRadio()
{
var form = frmClasses.ActiveForm as frmClasses;
if (form.radBtnColor.Checked == false)
{
form.lblShowError.ForeColor = Color.Aquamarine;
}
}
}
}
Form Code:
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 Classes
{
public partial class frmClasses : Form
{
public frmClasses()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
ClsProperties.validateRadio();
ClsProperties.validateTextBox();
}
}
}
I could put all the validation in one method but I am simply trying to show how I would call multiple methods using a class.
Is there a better way to access the form elements in a class?
Merci
I have had a fairly thorough look around, but couldn't find anything related, so I have decided to ask here :)
I have created a WinForm in Visual Studio, and in its code it has a fair few functions. At this point its all working nicely. I then go and put
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
in the constructor function and when I go and click the little cross in the corner, I have to click it twice for it to close! I would put the code in here, but its 240 lines, so its kinda huge.. If you need to see it though, I can put it up.
Thanks in advance!
Dronnoc
The ApplicationExit event is automaticly called on clicking the cross. So there is actual no need to call it. What do you want to do on closing?
If you want some action between clicking the cross and shutdown, you have to call the FormClosing() event.
I have solved it peoples!
I had a ListBox on the page, and a function running when the SelectedIndex changed. When i closed the form, it passed a SelectedIndex of -1, and then closed the second time. So, in order to fix it, I simply put some simple verification of the value on the ListBox function.
Example:
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();
listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
}
void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
becomes
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();
listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
}
void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listBox1.SelectedIndex == -1)
{
Application.Exit();
}
//Rest of the code goes here.
}
}
}