namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Authenticator at = new Authenticator();
at.validate();
}
}
public class Authenticator
{
private int num;
public bool validate()
{
if (textBox1.Text == num.ToString()) // problem #1
{
ListBox.Items.Add("Valid"); // problem #2
}
}
}
}
Hello everyone.
As you can see from the above code I am writing an application that requires a user defined class to be able to access the winforms I am working with like in the above simplified example. I'm very new to C# so please forgive my ignorance.
I need the authenticator class to be able to access the data in a textbox and then compare it and if both strings are equal then update the listbox. Is there a simple way to do this?
Probably best to keep the gui separate from the logic:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Authenticator at = new Authenticator();
if (at.validate(textBox1.Text)) {
ListBox.Items.Add("Valid");
}
}
}
public class Authenticator
{
private int num;
public bool validate(string s)
{
if (s == num.ToString())
{
return true;
}
return false;
}
}
How about something like this?
public bool validate(string text)
{
return (text == num.ToString());
}
And calling it in your code like this. You should try to make code as reusable as possible. Which means that references to specific instances of controls on a Form is not always the best design.
if (at.validate(textBox1.Text))
{
ListBox.Items.Add("Valid");
}
Sorry I can't comment on your post, I don't have enough reputation.
I've had similar trouble once before getting a form to access a string from another form.
In my case I just had to make the string static. Try doing the same with your textbox.
public static TextBox textBox1;
public Form1()
Related
i have a little problem on my form Login when i Login, i want send my username on the string username in my class functions.
And when my Main Form is loaded i want this class functions get the username from my form login with my username
i have try something like this:
My form login:
public Functions FUNCTIONS = new Functions(); //for my class Functions
FUNCTIONS.Username = "Username123";
My class Functions:
public class Functions
{
public string Username = ""; //empty
}
and my Main form after login
public Functions FUNCTIONS = new FUNCTIONS();
private void Main_Load(object sender, EventArgs e)
{
MessageBox.Show("Welcome "+ FUNCTIONS.Username " to my application.");
}
When my Main Form is loaded it's don't show the username string it's keep this empty, thanks for your time and your help for fix my problem.
You could try
public class Functions
{
public static string Username { get; set; }
}
Also this way you dont need to initialize Functions with new keyword, Functions.Username would be enough
It would work because static keyword ensures that there is one instance of this peroperty for yor application lifetime. Also you could consider using singleton pattern with dependency injection, read more there :
https://csharpindepth.com/articles/singleton
Don't create new 2nd time, new creates an another Functions instance. Pass to main Form the existing one instead and assign it to the field in main Form.
public partial class LoginForm : Form
{
private Functions functions = new Functions();
public LoginForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
functions.Username = "Username123";
new MainForm.Show(functions);
this.Close();
}
}
public partial class MainForm : Form
{
private Functions functions;
public MainForm()
{
InitializeComponent();
}
public MainForm(Functions f) : this()
{
functions = f;
}
private void MainForm_Load(object sender, EventArgs e)
{
MessageBox.Show(functions.Username);
}
}
There are only (3) things I need to know in here.
Firstly, there is only (1) button (close_button) in this form. Here's my frm_main.cs code:
public partial class frm_main : Form
{
public_class pc = new public_class();
public frm_main()
{
InitializeComponent();
this.Load += new System.EventHandler(frm_main_Load);
}
private void close_button_Click(object sender, EventArgs e)
{
this.Close();
}
private void frm_main_Load(object sender, EventArgs e)
{
pc.screen_adjust(close_button);
}
}
And here's my public_class.cs code:
public partial class public_class
{
public int cl_b;
public void screen_adjust(Button b)
{
cl_b = frm_main.ActiveForm.Width;
frm_main.ActiveForm.Width = Screen.PrimaryScreen.Bounds.Width;
frm_main.ActiveForm.Height = Screen.PrimaryScreen.Bounds.Height;
b.Left += frm_main.ActiveForm.Width - cl_b;
}
}
The aim of this borderless program is to auto-stretch the form to the whole screen. Now, what I'd like to learn is:
Did I do a correct "VB.net module" in C# properly?
How do I call the methods in public_class.cs without using the 'public_class pc = new public_class();' and 'pc.screen_adjust(close_button);'?
In the public_class.cs, for example, if I want to change the close_button's text, how should I do it? I can't do frm_main.close_button afterall...
Thanks!
I'm a newbie in c# and visual studio, but not programming in general.
I searched for answer to my question for 3 days and I found plenty of them, but for some weird reason (I'm sure I'm missing something very obvious) I cannot get it to work.
I think it's the most basic question newbies like me ask.
I have a form (Form3) with a text box and a button (I set it up is just for testing purposes).
I want to populate and read this text box from another class. I understand the most proper way to do this is to create a property in Form3.cs with GET and SET accessors. I did that but I cannot get it to work. I'm not getting any error messages, but I'm not able to set the value of the text box either. It just remains blank.
Here's my sample code:
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public string setCodes
{
get { return test1.Text; }
set { test1.Text = value; }
}
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{ }
private void button1_Click(object sender, EventArgs e)
{
a.b();
}
}
public class a
{
public static void b()
{
Form3 v = new Form3();
v.setCodes = "abc123";
}
}
}
Can someone lend me a hand solving this?
The problem is you are setting the value to a new instance of the form. Try something like this:
public partial class Form3 : Form {
public string setCodes
{
get { return test1.Text; }
set { test1.Text = value; }
}
private A a;
public Form3()
{
InitializeComponent();
a = new A(this);
}
private void button1_Click(object sender, EventArgs e)
{
a.b();
}
private void Form3_Load(object sender, EventArgs e)
{
}
}
public class A
{
private Form3 v;
public a(Form3 v)
{
this.v = v;
}
public void b()
{
v.setCodes = "abc123";
}
}
You're creating a brand new Form3() instance.
This does not affect the existing form.
You need to pass the form as a parameter to the method.
Try this:
public partial class Form3 : Form
{
/* Code from question unchanged until `button1_Click` */
private void button1_Click(object sender, EventArgs e)
{
a.b(this);
}
}
public class a
{
public static void b(Form3 form3)
{
form3.setCodes = "abc123";
}
}
This passes the current instance of the form to the other class so that it can update the setCodes property. Previously you were creating a new form instance rather than updating the current form.
Sending form instance to other other class
Form1 objForm1=new Form1();
obj.Validate(objForm1);
Easy way to access controls in another class by modifying Controls Private to Public in the Form(Designer.cs)
Currently have a mainForm.cs which calls a class panel.cs
panel.cs holds multiple pictureboxes with events such as _click, _mousedown, _mouseup
I wish to call a function in mainForm.cs from panel.cs
do i need to use a callback / delegate. Is there another way to access this function
I have tried
Main main = new Main();
main.functioninMain does not work
any help or direction would be appreciated.
for example
panel.cs
private void pb_button1_Click(object sender, EventArgs e)
{
this.BeginInvoke(new Action(main.functioninMain));
}
You should not instantiate another object of MainForm, which creates a duplicate and all the objects withing the new MainForm will not have the values of your actual MainForm.
Approach 1
You can try creating a static instance of you MainForm like below
public partial class MainForm : Form
{
public static MainForm Instance = null;
public MainForm()
{
InitializeComponent();
Instance = this;
}
public SomeMethod()
{
}
}
Now if you have your panel class, then you can easily access public methods and variables of MainClass
class Panel : Form
{
public Panel()
{
MainForm.Instance.SomeMethod();
}
}
Edit: Pass Handle as parameter to the form (From Ben Voigt's suggestion)
Approach 2:
As Ben suggested, it is also important to have a safer code, so much cleaner approach will be passing handle of the control as parameter and then to access them.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TestClass test = new TestClass();
test.ModifyText(textBox1);
}
}
public class TestClass
{
public void ModifyText(TextBox textBox)
{
textBox.Text = "New text";
}
}
If we can use delegate & events something like this
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Panel formPanel = new Panel();
formPanel.OnPanelClick += new Panel.OnPanelButtonClick(formPanel_OnPanelClick);
formPanel.Show();
}
void formPanel_OnPanelClick(string a)
{
MessageBox.Show(a);
}
}
public partial class Panel : Form
{
public delegate void OnPanelButtonClick(string a);
public event OnPanelButtonClick OnPanelClick = null;
public Panel()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (OnPanelClick != null)
{
OnPanelClick("from Panel.cs");
}
}
}
You should avoid circular dependencies at all costs (class A depends on class B, and class B depends on Class A).
There are so many ways to do this, but perhaps one of the easier ways is to use an interface. mainForm.cs implements it like this:
public class mainForm : IMainForm {
// This method is defined in the interface, so mainForm
// must implement it.
public string GetStringFromMainForm() {
return "Hello from MainForm";
}
public void CreatePanel() {
// pass in a reference to myself so panel knows how to
// talk to me.
var panel = new Panel(this);
}
}
And IMainForm (the interface) could look like this:
public interface IMainForm {
string GetStringFromMainForm();
}
Then your panel class can talk to the Main form without having to reference mainForm.cs explicitly:
public class Panel {
// use a private variable to keep track of main form
private IMainForm _mainForm;
// Constructor: pass in a class that implements IMainForm. It isn't
// a type of MainForm, so there's no dependency on the concrete class.
public Panel(IMainForm mainForm) {
_mainForm = mainForm;
}
public void TalkToMainForm() {
var resultFromMainForm = _mainForm.GetStringFromMainForm();
Console.WriteLine(resultFromMainForm);
}
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to Pass String to another forms from one form in c#
C# Query : When I login to my software I select the desired username using combobox. After I login, I want to see the username text in the main window on the toolstrip.
I tried get and set method but something is going wrong. Can you please help me out? Thanks in advance.
Form 1:
public partial class login : Form
{
public login()
{
InitializeComponent();
}
public string username
{
get{
return a.ToString();}
}
public string a;
private void button1_Click(object sender, EventArgs e)
{
a = comboBox1.Text;
Form main1 = new main();
main1.Show();
// rest is the code for login.
}
}
Form 2:
public partial class main : Form
{
public main()
{
InitializeComponent();
}
public string username
{
set { toolStripLabel1.Text = value; }
}
private void main_Load(object sender, EventArgs e)
{
Form home = new home();
home.MdiParent = this;
home.WindowState = FormWindowState.Maximized;
home.Show();
}
}
Here Home is form3 a childform which opens in Main form. at load event of Main form itself.
Somebody just answered this question for you a few minutes ago!? And as far as I could tell, it was a perfectly appropriate answer, so why don't you just implement that?
https://stackoverflow.com/a/9872043/1017882
Replace the main_Load code with this:
private void main_Load(object sender, EventArgs e)
{
login home = new login();
home.MdiParent = this;
home.WindowState = FormWindowState.Maximized;
home.ShowDialog(); // waits for the home form to be closed
this.username = home.username;
}
Im not sure where you call your login class.
But you could pass an instance of your main form and set the value from there.
public login(main mainInstance)
{
//... login etc.
mainInstance.UserName = comboBox1.Text;
}
or have the login method return a user object or string.
I can't post this as a comment, otherwise it would be unreadable, but unless I am mistaken:
Form home = new home()
Would not even compile considering the only two classes are main and login
While you could correct the code and declare the form the correct way
login home = new login();
It seems easier just to use a static variable in a case like this.
I believe best way is to use events:
Form1:
public partial class login : Form
{
public delegate void sendStringDelegate(string s);
public event sendStringDelegate sendString;
public login()
{
InitializeComponent();
}
public string username
{
get{
return a.ToString();}
}
public string a;
private void button1_Click(object sender, EventArgs e)
{
a = comboBox1.Text;
sendString(a);
// rest is the code for login.
}
}
Form2:
public partial class main : Form
{
login log = new login();
public main()
{
InitializeComponent();
log.sendString += new login.sendStringDelegate(setString);
}
public void setString(string s)
{
whatever.text = s;
}
public string username
{
set { toolStripLabel1.Text = value; }
}
private void main_Load(object sender, EventArgs e)
{
Form home = new home();
home.MdiParent = this;
home.WindowState = FormWindowState.Maximized;
home.Show();
}
}
code not tested so there might be some errors
Also you could use static event:
change
public event sendStringDelegate sendString;
to
public static event sendStringDelegate sendString;
remove login log = new login();
change
log.sendString += new login.sendStringDelegate(setString);
to
login.sendString += new login.sendStringDelegate(setString);