Based on my research, the only way to get a MessageBox Form to Center on the parent form is to write a custom MessageBox class. I have successfully implemented the CustomMessageBox Form and am able to center my errors and informational messages on the parent form. However, I cannot figure out how to make the CustomMessageBox form static so that I do not have to instantiate my new CustomMessageBox Form. I want to be able to just call a static method like below:
CustomMessageBox.Show(type, message, etc...)
The basic version of my MessageBox class is below. Ideally, I would like to have the functionality to display this form without having to instantiate my CustomMessageForm. Is this possible?
namespace MarineService
{
public partial class CustomMessageForm : DevExpress.XtraEditors.XtraForm
{
private static CustomMessageForm form = new CustomMessageForm();
public CustomMessageForm()
{
InitializeComponent();
}
public void ShowDialog(string type, string message)
{
this.Text = type + "Information";
this.groupMessage.Text = type + "Information";
this.memoEditMessage.Lines[0] = message;
}
}
}
Something like this:
public partial class CustomMessageForm : DevExpress.XtraEditors.XtraForm
{
private static CustomMessageForm form = new CustomMessageForm();
public CustomMessageForm()
{
InitializeComponent();
}
private void ShowDialog(string type, string message)
{
form .Text = type + "Information";
form .groupMessage.Text = type + "Information";
form .memoEditMessage.Lines[0] = message;
form.ShowDialog();
}
public static Show(string type, string message)
{
if(form.Visible)
form.Close();
ShowDialog(type, message);
}
}
And use this like:
CustomMessageForm.Show("customtype", "warning!");
Something like that, just an idea.
If this is not what you're asking for, please clarify.
Related
I know this has been asked thousands of time but still after a lot of research I can't find a solution and I am really sorry about this post.
I want to access my Label from a class in another namespace.
This is a sample of code to understand better what I am trying to do:
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
}
//class in another namespace
class Servers
{
public void _SetlabelText()
{
Main.label1.Text = "New Text";
}
}
How am I supposed to do it the proper way?
One option is to store a reference to the form in the constructor like this:
public class Servers
{
private Form _frmMain;
public Servers(Form frmMain)
{
_frmMain = frmMain;
}
public void SetlabelText()
{
_frmMain.label1.Text = "New Text";
}
}
And use it like this:
public partial class Main : Form
{
public Main()
{
InitializeComponent();
var servers = new Servers(this);
servers.SetlabelText();
}
}
However, it's typically advised to return back to the Form class and set it there, like this:
public partial class Main : Form
{
public Main()
{
InitializeComponent();
label1.Text = Servers.GetTextForLabel();
}
}
public class Servers
{
public static string GetTextForLabel()
{
return "New Text"; //(I assume this will be much more complex)
}
}
Im trying to set LabelStatus's text to a message in the class but it doesn't work.
Here's my code:
Class:
public bool openConnection()
{
SetStatus("Connecting to " + Server);
//Mysql code
}
private void SetStatus(string msg)
{
Form1 form = new Form1();
form.SetStatus(msg);
}
Form1:
public void SetStatus(string status)
{
labelStatus.Text = _status;
}
I'm fairly new to C# (php guy) and for the life of me can't figure out what I am doing wrong
Try calling the ShowDialog or Show Method on your form
private void SetStatus(string msg)
{
Form1 form = new Form1();
form.SetStatus(msg);
form.ShowDialog(this);
}
looks like you are setting the member variable and not the function's parameter.
//try something like this
this._status = status;
this.labelStatus.Text = this._status;
When setting labelStatus.Text, you're not setting it with the parameter you passed to SetStatus(string). It seems like you accidentally used a data member instead.
Look at the names : try to make them same, see
labelStatus.Text = **status**;
From your code, I think your class is change the status label of form label. To change form label text you need object of already opened form. define variable for form in your class.
public class ConnectionCheck
{
private Form myForm;
public void ConnectionCheck(Form form)
{
myForm = form;
}
public bool openConnection()
{
SetStatus("Connecting to " + Server);
//Mysql code
}
private void SetStatus(string msg)
{
//Call method to change label text
myForm .SetStatus(msg);
}
}
Pass form1 object at the time of ConnectionCheck object creation from from1 codebehind(form1.cs).
ConnectionCheck connection = new ConnectionCheck(this);
And Also, change _status to parameter variable.
public void SetStatus(string status)
{
labelStatus.Text = status;
}
I want to create a custom message box for a program so I added a windows form item. I would like it to behave like MessageBox in that it is static and I just call MessageBox.Show(a, b, c, ...). In the forms designer, however, I don't see how I can make it static. Can I just add static to the code? Is there a property setting I'm missing in the designer mode?
Thanks!
MessageBox is not a static class, the Show method however is. Make Show static, in code. E.g.
public class MyMessageBox : Form
{
public static int MyShow()
{
// create instance of your custom message box form
// show it
// return result
}
}
It is a regular class with one method as static which instantiate new instance and act.
public class MyMessageBox
{
public static MyResult Show(params)
{
var myMessageBox = new MyMessageBox();
myMessageBox.Message = params ...
return myMessageBox.ShowDialog();
}
}
Add a static method to your form that displays itself and returns a DialogResult:
public partial class MyMessageBoxForm : Form {
public static DialogResult Show(string message) {
using (MyMessageBoxForm form = new MyMessageBoxForm(message)) {
return form.ShowDialog();
}
private MyMessageBoxForm(string message) {
// do something with message
}
}
If you want create static Form1 for access to it without object reference, you can change Program.cs:
public class Program
{
public static Form1 YourForm;
[STAThread]
static void Main(string[] args)
{
using (Form1 mainForm = new Form1())
{
YourForm = mainForm;
Application.Run(mainForm);
}
YourForm = null;
}
}
and call Form1 class methods from any place of your program:
Program.YouForm.DoAnything();
Do not forget to call Invoke for access from other threads.
I've searched Google all day and can't find the correct answer to my issue, hoping someone here can help me.
So, in the "Main" form I have the method to show a form that needs to be centered directly above the parent form (frmMain). Normally I would call ShowDialog(this) to see the parent, but for some reason I have to set the loadNewsFeedItem to static in order to see the method from the flpNewsFeedHeader : Label derrived class (below). The OnClick event triggers the method loadNewsFeedItem().
When I call this to set the parent, I'm getting the message "Keyword 'this' is not valid in a static property, static method, or static field initializer"
namespace NewsFeeds
{
public partial class FrmMain : Form
{
public static void loadNewsFeedItem()
{
frmNewsFeedView frmFeedView = new frmNewsFeedView(FrmFuncs.selFeedID);
frmFeedView.ShowDialog(this); // Error occurs on this line, when calling this via a static method
}
}
}
public class flpNewsFeedHeader : Label
{
private int FeedID = 0;
public int theFeedID
{
get { return FeedID; }
set { FeedID = value; }
}
protected override void OnClick(EventArgs e)
{
FrmFuncs.selFeedID = FeedID;
Thread thrShowFeed = new Thread(new ThreadStart(FrmMain.loadNewsFeedItem));
thrShowFeed.Start();
}
}
Can someone please give me a corrected code example or a hint as to how to get the loadNewsFeedItem() to be visible without setting the accessor to static, or how to work around this in a static accessor?
Thanks in advance!
Chris
Edit: used ActiveForm for owner.
public partial class FrmMain : Form
{
public static void loadNewsFeedItem(Form owner)
{
frmNewsFeedView frmFeedView = new frmNewsFeedView(FrmFuncs.selFeedID);
frmFeedView.ShowDialog(owner);
}
}
}
public class flpNewsFeedHeader : Label
{
private int FeedID = 0;
public int theFeedID
{
get { return FeedID; }
set { FeedID = value; }
}
protected override void OnClick(EventArgs e)
{
FrmFuncs.selFeedID = FeedID;
// Shouldn't need a new thread. Already on the GUI thread.
FrmMain.loadNewsFeedItem (System.Windows.Forms.Form.ActiveForm);
}
}
may be you mean this:
frmFeedView.Owner = System.Windows.Forms.Form.ActiveForm;
frmFeedView.ShowDialog();
In a static method, this is meaningless. One option is to skip the parameter
frmFeedView.ShowDialog();
The other option is to setup a static variable as shown below (but beware, it can have side effects if you try to open multiple instances of FrmMain)
public partial class FrmMain : Form
{
private static FrmMain staticInstance;
public FrmMain()
{
staticInstance = this;
InitializeComponent();
...
}
public static void loadNewsFeedItem()
{
frmNewsFeedView frmFeedView = new frmNewsFeedView(FrmFuncs.selFeedID);
frmFeedView.ShowDialog(staticInstance );
}
I am trying to create a form where I can update text in a text box without requiring interaction from the user. Right now I am trying to create a method in my form's class that updates the TextBox.Text field. Outside of the class I am not able to access the function.
Right now I am trying
static void Main()
{
Form status = new Window();
Application.Run(status);
status.UpdateTextBox("NewText");
}
My form class looks like (from the designer)
public partial class Window : Form
{
public Window()
{
InitializeComponent();
}
public void UpdateTextBox(string text)
{
textBox1.Text = text;
}
}
I have also tried making the Textbox a public property like this.
public string DisplayedText
{
get
{
return textbox1.Text;
}
set
{
textbox1.Text = value;
}
}
I am trying to edit the field during runtime. Is this possible?
You can access the function, but if you look at the code, there is a problem:
static void Main()
{
Form status = new Window();
Application.Run(status); // Blocks here!
status.UpdateTextBox("NewText");
}
When you call Application.Run(), it will not return control to your program until the status form has closed. At that point, setting the status is too late...
You can set it before you run, but after you've constructed:
static void Main()
{
Form status = new Window();
status.UpdateTextBox("NewText");
Application.Run(status);
}
It's this code...the Application.Run will block, status.UpdateTextBox isn't going to execute until you close the form.
static void Main()
{
Form status = new Window();
Application.Run(status);
status.UpdateTextBox("NewText");
}