C#, menustrip specific menu open when user have logged in - c#

in my menuStrip i have multiple forms,
So in 1 of them i want to be access only when use is pass from a login form i have create..
Lets say menustrip, menu is on form1, and the login form is Form2,
So how i am gonna comebine this to so menustrip form open when the user first pass the form2 login..
i am searching all day over the net i found only by passing a variable and then if its true the form1 is gonna pop-out but it didnt work well!

Somewhere you will need to have a static Boolean property telling you if the user is logged in.
public static class Config {
public static bool LoggedIn { get; private set; }
public static Login ()
{
var frm = new frmLogin();
if (frm.ShowDialog() == DialogResult.OK) {
LoggedIn = frm.LoggedIn;
}
}
}
In frmLogin have again a public, but this time non-static property LoggedIn, which you set true if username and password are OK.
Then, in the menustrip form enable or disable the menu items accordingly in the form Activated event handler:
private void Form1_Activated(object sender, EventArgs e)
{
myToolStripMenuItem.Enabled = Config.LoggedIn;
}

Well thinking I got it right "You want to have input in form 1 from form2or something like modify something in form1 from form2"
It may not be the best but works
Well the simplest solution that has helped me till now is that you
Make menuStrip modifier from properties to public
create an instance on Form1 in Form2
Modify menuStrip from Form2
for example we can have
Form2.cs
public partial class ViewCars : Form
{
Form1 mainForm;
public ViewCars (Form1 MainForm)
{
this.mainForm = MainForm;
}
}
private void LoginButton_Click(object sender, EventArgs e)
{
mainForm.menuStrip.Visible = false;
}
Now to when you make the instance of Form2 you need to have give parameter this
i.e
ViewCars view = new ViewCars(this);
view.Show();
This is not Limited to controls you can do anything with it
Good Luck

Related

TextBox B in Form2 to display text from TextBox A in Form 1 using C# and WinForms

After entering in the appropriate text and pressing enter, I want txtbA text in Form1 to display in txtbB in Form 2.
I already have the key events code written, but I can't seem to figure out the rest.
Visual basic seems to be more straightforward with this, and I am new to C#.
This is using WinForms.
I am really only familiar with visual basic's way of handling this:
txtbA.text = My.Forms.Form2.txbB.text
Thank you for any help you can give!
You can use a static variable
In Form1
public static string txtbAtext
{
get { return txtbAtext ; }
set {txtbAtext = value; }
}
OnTextChanged Event
txtAtext = txtA.Text;
In Form2
Form1 f1 = new Form1();
txtbB.Text = Form1.txtAText;
The simpliest and not very elegant way is to do ist like this:
Make a property in the second form, that can hold a Textbox and set the Text property in the TextChange event of the property in Form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public TextBox TextBoxWithSameText { get; set; }
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (TextBoxWithSameText != null)
TextBoxWithSameText.Text = textBox2.Text;
}
}
All you have to do now is to set the property to the Form1.textbox1 when the second form (Form2) is created:
Form2 form = new Form2();
form.TextBoxWithSameText = textbox1;
In order to exchange strings between two Forms classes, you can use a third class. Once added to your project, you need to instantiate this new Class1 in your Form1 and Form2:
Class1 class = new Class1;
Make sure Class1 has a constructor. Now, you can create properties in your Class1, with get and set properties:
public string TextA
{
get
{
return textA;
}
set
{
textA = value;
}
}
You can then call these properties from any other class that has instantiated your Class1, like so:
class.TextA = txtbA.Text;
In order to show the text in the other textbox, you need an event to trigger changing the txtbB.Text value. You could use txtbA.ValueChanged event, or a button with a Click event. You should figure out what event is most appropriate for your project.
I hope this helped! Good luck.
Based on your comment:
Right, Form1 will open an instance of Form2 upon pressing the Enter
key. Thanks!
You can setup Form2 to receive the initial value for the TextBox via its Constructor:
public partial class Form2 : Form
{
public Form2(string InitialValue)
{
InitializeComponent();
this.txtbB.Text = InitialValue;
}
}
Then in Form1 you'd do something like:
Form2 f2 = new Form2(this.txtbA.Text);
f2.Show();

Calling a method from another Form in C#

I want that when I insert or update records in another form (Form2), the DataGridView on Form1 should automatically refresh (call btnRefresh) after each operation or preferably wait until all change operations have finished, and update the DataGridView form Form2's closing event with all changes.
I believe in VB.NET this is achieved with Form1.DataGridView.Refresh, but I am not sure in C#. I was told that I pass the reference of the DataGridView on Form1 to Form2 using properties but since I'm new to C#, I didn't know how to. How can I resolve this issue?
My refresh button code:
private void btnRefresh_Click(object sender, EventArgs e)
{
GVThesis.DataSource = thesisRepository.GetThesis();
GVThesis.Refresh();
}
Firs, wrap your refresh code into a method of its own, and call that from your click event handler method, like so:
private void btnRefresh_Click(object sender, EventArgs e)
{
this.RefreshData();
}
public void RefreshData()
{
GVThesis.DataSource = thesisRepository.GetThesis();
GVThesis.Refresh();
}
Then, asuming you are instantiating and launching the new form (Form2) from your Form1, simply go into the code of Form2 and create yourself a new constructor overload which will accept a reference to Form1, and store it in a private variable, like so:
public partial class Form2 : Form
{
private Form1 frm1;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 otherForm)
{
InitializeComponent();
this.frm1 = otherForm;
}
}
Then you can call the "refresh" from anywhere you like in Form2 like so:
this.frm1.RefreshData();
EDIT:I created a small sample, I cannot upload it here...but here is a screenshot of both the program itself in VS, as well as a screenshot of the result of running it and performing the function...hopefully that will help.
The program (zoom your view if it appears too small)
The Result:

Event handling (Detect when hiding form)

Well normally I am quite good at figuring and researching problems without guidance however I have come across a snag. I am trying to create an "Event" with C# (which I have not done before) everything I have looked up has nothing to do with what I need.
I am trying to call a class on my main form when form2 is hidden. I found some code which was supposed to check to see if form2 closed - Either I didn't integrate it into my code properly or closing is different to hiding.
So just to clarify I want to run through the program like this:
Form1 runs
Click setting button on Form1 which opens Form2
Form2 opens where settings can be changed
Click the "Ok" button on Form2 (here is where I want Form1 to realise Form2 has been hidden
Hide form one and run a class called Refresh which refreshes button names and URLs
Also,
you can use VisibleChanged event in Form2
private Form2_VisibleChanged(object sender, EventArgs e)
{
if (!this.Visible) { Refresh(); }
}
This might be more elegant...
Open the second form as modal
Form2 form2 = new Form2();
DialogResult result = form2.ShowDialog();
check the result and refresh:
if (result == DialogResult.OK)
Refresh();
What you also need to do in this case is when closing the form set DialogResult of the form, for example if you have an OK button, in the button handler set:
this.DialogResult = DialogResult.OK;
This will automatically close the form as well as I remember correctly.
What you can also do is set DialogResult.Cancel on cancel button if you need that.
If I understand correctly, you want to have a class that stores settings information that both Form1 and Form2 can access. Let's call that class Form1Settings, and implement as:
public static class Form1Settings
{
public static string ButtonText;
public static string Uri;
}
For simplicity, I made this class and its properties static, so both Form1 and Form2 have direct access to it, removing the need for a refresh method.
Form1 would call Form2 in a blocking way, and only update its display if the OK button was clicked.
public partial class Form1 : Form
{
private Form2 form2 = new Form2();
public Form1()
{
InitializeComponent();
}
private void buttonSettings_Click(object sender, EventArgs e)
{
if (form2.ShowDialog() == DialogResult.OK)
{
this.button1.Text = Form1Settings.ButtonText;
this.textBoxUrl.Text = Form1Settings.Uri;
this.Update();
}
}
}
And finally, Form2 will update the settings values with input from the user:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void buttonOK_Click(object sender, EventArgs e)
{
Form1Settings.ButtonText = this.textBoxButton.Text;
Form1Settings.Uri = this.textBoxUri.Text;
this.DialogResult = DialogResult.OK;
this.Hide();
}
}
Why not open Form2 as a modal dialog using ShowDialog()? That way you may return a value if Form2 was closed by OK or by Cancel and act accordingly in Form1 after the call returned.

Firing a method in form 1 on click of a button on form2

I am quite new to windows forms. I would like to know if it is possible to fire a method in form 1 on click of a button in form 2?
My form 1 has a combobox. My form 2 has a Save button. What I would like to achieve is:
When the user clicks on Save in form 2, I need to check if form 1 is open. If it is open, I want to get the instance and call the method that would repopulate the combo on form 1.
I would really appreciate if I get some pointers on how I can do work this out. If there any other better way than this, please do let me know.
Thanks :)
Added:
Both form 1 and form 2 are independent of each other, and can be opened by the user in any order.
You can get a list of all currently open forms in your application through the Application.OpenForms property. You can loop over that list to find Form1. Note though that (in theory) there can be more than one instance of Form1 (if your application can and has created more than one instance).
Sample:
foreach (Form form in Application.OpenForms)
{
if (form.GetType() == typeof(Form1))
{
((Form1)form).Close();
}
}
This code will call YourMethod on all open instances of Form1.
(edited the code sample to be 2.0-compatible)
Of course this is possible, all you need is a reference to Form1 and a public method on that class.
My suggestion is to pass the Form1 reference in the constructor of Form2.
What you can do is create static event in another class and then get Form 1 to subscribe to the event. Then when button clicked in Form 2 then raise the event for Form 1 to pick up on.
You can have declare the event in Form 1 if you like.
public class Form1 : Form
{
public static event EventHandler MyEvent;
public Form1()
{
Form1.MyEvent += new EventHandler(MyEventMethod);
}
private void MyEventMethod(object sender, EventArgs e)
{
//do something here
}
public static void OnMyEvent(Form frm)
{
if (MyEvent != null)
MyEvent(frm, new EventArgs());
}
}
public class Form2 : Form
{
private void SaveButton(object sender, EventArgs e)
{
Form1.OnMyEvent(this);
}
}

How to change text in a textbox on another form in Visual C#?

In Visual C# when I click a button, I want to load another form. But before that form loads, I want to fill the textboxes with some text. I tried to put some commands to do this before showing the form, but I get an error saying the textbox is inaccessible due to its protection level.
How can I set the textbox in a form before I show it?
private void button2_Click(object sender, EventArgs e)
{
fixgame changeCards = new fixgame();
changeCards.p1c1val.text = "3";
changeCards.Show();
}
When you create the new form in the button click event handler, you instantiate a new form object and then call its show method.
Once you have the form object you can also call any other methods or properties that are present on that class, including a property that sets the value of the textbox.
So, the code below adds a property to the Form2 class that sets your textbox (where textbox1 is the name of your textbox). I prefer this method method over making the TextBox itself visible by modifying its access modifier because it gives you better encapsulation, ensuring you have control over how the textbox is used.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string TextBoxValue
{
get { return textBox1.Text;}
set { textBox1.Text = value;}
}
}
And in the button click event of the first form you can just have code like:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.TextBoxValue = "SomeValue";
form2.Show();
}
You can set "Modifiers" property of TextBox control to "Public"
Try this.. :)
Form1 f1 = (Form1)Application.OpenForms["Form1"];
TextBox tb = (TextBox)f1.Controls["TextBox1"];
tb.Text = "Value";
I know this was long time ago, but seems to be a pretty popular subject with many duplicate questions. Now I had a similar situation where I had a class that was called from other classes with many separate threads and I had to update one specific form from all these other threads. So creating a delegate event handler was the answer.
The solution that worked for me:
I created an event in the class I wanted to do the update on another form. (First of course I instantiated the form (called SubAsstToolTipWindow) in the class.)
Then I used this event (ToolTipShow) to create an event handler on the form I wanted to update the label on. Worked like a charm.
I used this description to devise my own code below in the class that does the update:
public static class SubAsstToolTip
{
private static SubAsstToolTipWindow ttip = new SubAsstToolTipWindow();
public delegate void ToolTipShowEventHandler();
public static event ToolTipShowEventHandler ToolTipShow;
public static void Show()
{
// This is a static boolean that I set here but is accessible from the form.
Vars.MyToolTipIsOn = true;
if (ToolTipShow != null)
{
ToolTipShow();
}
}
public static void Hide()
{
// This is a static boolean that I set here but is accessible from the form.
Vars.MyToolTipIsOn = false;
if (ToolTipShow != null)
{
ToolTipShow();
}
}
}
Then the code in my form that was updated:
public partial class SubAsstToolTipWindow : Form
{
public SubAsstToolTipWindow()
{
InitializeComponent();
// Right after initializing create the event handler that
// traps the event in the class
SubAsstToolTip.ToolTipShow += SubAsstToolTip_ToolTipShow;
}
private void SubAsstToolTip_ToolTipShow()
{
if (Vars.MyToolTipIsOn) // This boolean is a static one that I set in the other class.
{
// Call other private method on the form or do whatever
ShowToolTip(Vars.MyToolTipText, Vars.MyToolTipX, Vars.MyToolTipY);
}
else
{
HideToolTip();
}
}
I hope this helps many of you still running into the same situation.
In the designer code-behind file simply change the declaration of the text box from the default:
private System.Windows.Forms.TextBox textBox1;
to:
protected System.Windows.Forms.TextBox textBox1;
The protected keyword is a member access modifier. A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member (for more info, see this link).
I also had the same doubt, So I searched on internet and found a good way to pass variable values in between forms in C#, It is simple that I expected. It is nothing, but to assign a variable in the first Form and you can access that variable from any form. I have created a video tutorial on 'How to pass values to a form'
Go to the below link to see the Video Tutorial.
Passing Textbox Text to another form in Visual C#
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
TextBox txt = (TextBox)frm.Controls.Find("p1c1val", true)[0];
txt.Text = "foo";
}

Categories

Resources