Unable to SET value from another class - c#

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;
}

Related

how to pass a form as a parameter?

First I know there are already answer for this question but most solution seems complicated for nothing.
Situation :
I have a form called frm1. I want to pass it as parameter
myfunc(ref frm1)
I would then do
private void myfunc(ref Form frm1)
It says : frm1 is a type but is used as a variable.
My reason for doing this is because depending on choice I pass my form to one of either two functions which fills it differently.
Problem :
However I cannot pass as argument my form. However I can pass other controls like button in the same way. How can I do this simply with the form, without interface etc...
There is something wrong with the way you are passing the parameter in. Are you definitely passing in the instance and not the type?
Here's a working example.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Name = "form";
Form f = this;
doSomethingWithForm(f);
}
private void doSomethingWithForm(Form f)
{
Console.WriteLine(f.Name);
}
}
I have created one function. I think it will help you. I am using this in my practice.
-->function below:
public void showForm(Form _form, Form _main) {
if (_main != null)
{
if (_main.ActiveMdiChild != null)
{
_main.ActiveMdiChild.Close();
}
_form.MdiParent = _main;
_form.Activate();
_form.Show();
}
else
{
_form.Activate();
_form.ShowDialog();
}
-->how to use it:
objLib.showForm(new frmMain(), null);
OR
objLib.showForm(new frmNewspaper(), this);
Thank You
I will add to kenjara's answer.
// For example: change color of the form - from some other method
private void Form_Load(object sender, EventArgs e)
{
ChS = new ChangeSomething();
ChS.ChangeBackColor(this);
}
public class ChangeSomething
{
public void ChangeBackColor(Form form)
{
form.BackColor = System.Drawing.Color.Black;
return;
}
}
Tested VS2022 / .NET4.8 / Windows Forms

Custom MessageBox For Errors and Information

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.

Can I only call static functions from a form to another?

I have 2 forms connected to a database, LoadDocument form and a Fom1 that is the primary form. In LoadDocument I get document names out of my database, and when I close LoadDocument I send the document id to Form1 so I can retrieve its content there.
The only problem is that if i make a function in Form1, called public void showContent() my LoadDocument can't call it because it's not static, and if I make it static, I get problems creating radioButtons.
public partial class Form1 : Form
{
public void showTasks()
{
radioButtons = new RadioButton[numberOfTasks];
for (int i = 0; i < numberOfTasks; ++i)
{
radioButtons[i] = new RadioButton();
radioButtons[i].Text = "Task " + (i+1);
radioButtons[i].Location = new System.Drawing.Point(
10, 10 + i * 20);
groupBox1.Controls.Add(radioButtons[i]);
radioButtons[i].Click += new EventHandler(this.radioButtons_Click);
}
}
}
Is there any way I can call this function from LoadDocument without making it static? Do I have to make LoadDocument dynamic, and in that case how?
EDIT: I guess this piece of code would be quite relevant:
private LoadDocument m_form1;
private bool m_underConstruction = false;
private void ShowLoadDocument()
{
if (m_underConstruction)
{
// We're about to show it anyway
return;
}
m_underConstruction = true;
try
{
if (m_form1 == null)
{
m_form1 = new LoadDocument();
// m_form1.FormClosed += new FormClosedEventHandler(m_form1_FormClosed);
m_form1.Show();
}
}
finally
{
m_underConstruction = false;
}
m_form1.BringToFront();
m_form1.Activate();
}
I'm not sure about the control flow and the co-existence of the two forms, but you could pass the instance of Form1 to LoadDocument and call the method directly on that object. Like:
public class LoadDocument : Form {
private Form1 form1;
public LoadDocument(Form1 form1) {
this.form1 = form1;
}
// later
public void Method() {
form1.showTasks();
}
}
public class Form1 : Form {
public void SomeMethod() {
LoadDocument doc = new LoadDocument(this);
doc.Show();
}
}
You don't need to make it static, but you need to have a reference to Form1 to call it. You can pass this reference to the constructor of LoadDocument when you create it:
public class Form1 : Form
{
...
LoadDocument loadDocument = new LoadDocument(this);
loadDocument.ShowDialog();
...
}
public class LoadDocument : Form
{
private readonly Form1 _form1;
public LoadDocument(Form1 form1)
{
_form1 = form1;
InitializeComponent();
}
...
_form1.showTasks();
...
}
You can call the showContent method on an instance of Form1.
I understand that the LoadDocument form is created from Form1. Pass the instance of Form1 to the LoadDocument constructor. That way, you'll be able to do form1WhoCreatedMe.ShowContent() somewhere in LoadDocument.
The dynamic keyword won't help you there. dynamic is not the contrary of static.

C# Set Form Parent after calling method from another class

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 );
}

How do I modify a form's text box from a separate class?

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");
}

Categories

Resources