i created user control which looks like this:
and code behind looks like:
using System;
using System.Windows.Forms;
namespace Controlls
{
public partial class Toolbar : UserControl
{
public Toolbar()
{
InitializeComponent();
}
private void pbNaprej_Click(object sender, EventArgs e)
{
}
private void pbNazaj_Click(object sender, EventArgs e)
{
}
private void pbNovo_Click(object sender, EventArgs e)
{
}
private void bpbBrisi_Click(object sender, EventArgs e)
{
}
private void pbPotrdi_Click(object sender, EventArgs e)
{
}
private void txtOd_TextChanged(object sender, EventArgs e)
{
}
}
}
now i move this UC to form but there is a problem. How to get this events from UC. I need to implement click events for buttons in form where i use this user control. What i can see is that i can use it like one component and i can not get separate buttons from it.
Add 2 event handlers for yor control, and subscribe to the toobox event at the place you use it:
using System;
using System.Windows.Forms;
namespace Controlls
{
public enum ToolboxButtonType { Potrdi, Brisi, Novo, Nazaj, Naprej }
public partial class Toolbar : UserControl
{
public Toolbar()
{
InitializeComponent();
}
private void pbNaprej_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Naprej);
}
private void pbNazaj_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Nazaj);
}
private void pbNovo_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Novo);
}
private void bpbBrisi_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Brisi);
}
private void pbPotrdi_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Potrdi);
}
private void txtOd_TextChanged(object sender, EventArgs e)
{
OnTextChanged(txtOd.Text);
}
}
private OnToolboxClick(ToolboxButtonType button)
{
if (ToolboxClick != null)
{
ToolboxClick(this, new ToolboxClickEventArgs(button));
}
}
private OnTextChanged(string text)
{
if (ToolboxTextChanged!= null)
{
ToolboxTextChanged(this, new ToolboxTextChangedEventArgs(text));
}
}
public class ToolboxTextChangedEventArgs: EventArgs
{
public string Text { get; private set; }
public ToolboxClickEventArgs(string text) { Text = text; }
}
public class ToolboxClickEventArgs : EventArgs
{
public ToolboxButtonType Button { get; private set; }
public ToolboxClickEventArgs(ToolboxButtonType button) { Button = button; }
}
public event EventHandler<ToolboxClickEventArgs> ToolboxClick;
public event EventHandler<ToolboxTextChangedEventArgs> ToolboxTextChanged;
}
For example:
//Toolbar toolbar = new Toolbar();
toolbar.ToolboxTextChanged += (s, e) => { btn1.Text = e.Text; };
toolbar.ToolboxClick += (s, e) =>
{
swith(e.Button)
{
case ToolboxButtonType.Brisi: //
break;
default:
break;
}
};
You need to create event in MainForm class and generarate it in hand mode.
If you also draw your icons on canvas or smth, you need to handle onClick your MainForm, than calculate mouse click position, and generate event that you were create.
If it amount of buttons or smth that has their own onClick events, you can hang on this buttons event the action that will generate your even in main form.
Answer what approach you use, and I past some code.
ADDED:
public partial class Toolbar : UserControl
{
public Toolbar()
{
InitializeComponent();
}
public enum ToolBarCommands
{
Naprej, Nazaj, Novo
}
public Action<object, ToolBarCommands> MenuItemClick;
private void pbNaprej_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Naprej);
}
private void pbNazaj_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Nazaj);
}
private void pbNovo_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Novo);
}
}
How to use it? easy:
// Define section
var toolbar = new Toolbar();
toolbar.MenuItemClick += MenuItemClickHandler;
...
MenuItemClickHandler(object sender, Toolbar.ToolBarCommands item)
{
switch(item)
{
case Toolbar.ToolBarCommands.Naprej:
// Naperej handler
break;
case Toolbar.ToolBarCommands.Nazaj:
// Nazaj handler
break;
case Toolbar.ToolBarCommands.Novo:
// Novo handler
break;
}
}
ADDED:
If names of your buttons same as enum members you can cut this part of code:
private void pbNaprej_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Naprej);
}
private void pbNazaj_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Nazaj);
}
private void pbNovo_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Novo);
}
It will look's like one method (hang this method to all your menu element events)
private void pbItem_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
{
ToolBarCommands command;
var res = ToolBarCommands.TryParse(((Button)sender).Name, command)
if(res == true)
MenuItemClick(this, command);
}
}
Related
Can I get some help? I wanna make an app in Visual Studio and I've created a password Reset Form in which I want to change the password. However, how can I replace the password string variable from the Form1 with the NewPassword string from Form2?
Form1:
namespace Aplicatie
{
public partial class Aplicatie : Form
{
public string Password = "123";
public Aplicatie()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
}
public void textBox2_TextChanged(object sender, EventArgs e)
{
}
public void textUsername_TextChanged(object sender, EventArgs e)
{
}
public void ButtonLogin_Click(object sender, EventArgs e)
{
if (textUsername.Text == "augnova001")
{
if (textPassword.Text == Password)
{
MessageBox.Show("Autentification Succesfull");
new Form2().Show();
this.Hide();
}
else
{
MessageBox.Show("ACCESS DENIED!");
MessageBox.Show("Have a great day!");
this.Close();
}
}
else
{
MessageBox.Show("ACCESS DENIED!");
MessageBox.Show("Have a great day!");
this.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
new PasswordReset().Show();
this.Hide();
}
public void textPassword_TextChanged(object sender, EventArgs e)
{
}
private void unhideButton_Click(object sender, EventArgs e)
{
if (textPassword.PasswordChar == '*')
{
hideButton.BringToFront();
textPassword.PasswordChar = '\0';
}
}
private void hideButton_Click(object sender, EventArgs e)
{
if (textPassword.PasswordChar == '\0')
{
unhideButton.BringToFront();
textPassword.PasswordChar = '*';
}
}
}
}
PasswordReset Form:
namespace Aplicatie
{
public partial class PasswordReset : Form
{
public PasswordReset()
{
InitializeComponent();
}
public void buttonReset_Click(object sender, EventArgs e)
{
Aplicatie app = new Aplicatie();
if (textOldPassword.Text == app.Password)
{
if (textNewPassword.Text == textRetypeNewPassword.Text)
{
Pass = textNewPassword.Text;
app.Password = Pass;
MessageBox.Show("The Password has been changed succesfully!");
}
else
{
MessageBox.Show("The Passwords do NOT correspond! Please Try Again... ");
}
}
else
{
MessageBox.Show("The old password is not correct! Please Try Again... ");
}
}
public void textOldPassword_TextChanged(object sender, EventArgs e)
{
}
public void textNewPassword_TextChanged(object sender, EventArgs e)
{
}
public void textRetypeNewPassword_TextChanged(object sender, EventArgs e)
{
}
}
}
Create an event in the password form then when a button is clicked in the password form send it to the calling form.
Child form
using System;
using System.Windows.Forms;
namespace WinApp1
{
public partial class PasswordResetForm : Form
{
public delegate void PasswordChanged(string sender);
public event PasswordChanged OnPasswordChanged;
public PasswordResetForm()
{
InitializeComponent();
}
private void SubmitButton_Click(object sender, EventArgs e)
{
OnPasswordChanged?.Invoke(PasswordTextBox.Text);
}
}
}
Calling form
using System;
using System.Windows.Forms;
namespace WinApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ShowResetFormButton_Click(object sender, EventArgs e)
{
PasswordResetForm f = new PasswordResetForm();
f.OnPasswordChanged += OnPasswordChanged;
if (f.ShowDialog() == DialogResult.OK)
{
// TODO
}
f.OnPasswordChanged -= OnPasswordChanged;
}
private void OnPasswordChanged(string sender)
{
// sender contains password from PasswordResetForm
}
}
}
make Password variable static
like this:
public static string Password = "123";
my problem is that i can't transfer values in the list between two classes in WFA
public partial class Example : Form
{
public List<string> myList = new List<string>();
private void Btn1_Click(object sender, EventArgs e)
{
new Example2().Show();
}
private void Example_Activated(object sender, EventArgs e)
{
if (myList.Count == 0)
{
//...
}
else
{
//...
}
}
public partial class Example2 : Form
{
static Example ex = new Example();
private void Btn2_Click(object sender, EventArgs e)
{
ex.myList.Add("something");
Close();
}
Form "Example" is shown first. Then i click "Btn1" on the screen, and form "Example2" appears. When i click "Btn2" on "Example2" form, myList should get new value of "something" and "Example2" closes. But this part of script
private void Example_Activated(object sender, EventArgs e)
{
if (myList.Count == 0)
{
//...
}
else
{
//...
}
}
shows that myList has no values (myList.Count equals 0).
What can i do?
you need to reference existing Example form with data from Example2 form. static Example ex is another (static) instance, which doesn't have data.
public partial class Example : Form
{
private List<string> myList = new List<string>();
private void Btn1_Click(object sender, EventArgs e)
{
var e2 = new Example2();
e2.SetMainForm(this);
e2.Show();
}
public void AddItem(string item)
{
myList.Add(item);
}
private void Example_Activated(object sender, EventArgs e)
{
if (myList.Count == 0)
{
//...
}
else
{
//...
}
}
}
public partial class Example2 : Form
{
private Example ex;
private void Btn2_Click(object sender, EventArgs e)
{
ex.AddItem("something");
Close();
}
public void SetMainForm(Example e1)
{
ex = e1;
}
}
I am trying to make a morse code for a college project, what I'm trying to do is use a 2 dimensional array to save the morse code people input to a text file and then be able to load it from the text file, my logic was to was that within the array was this array[morse name][morse input]. what I need to figure out first is how to send data from methods / buttons OBtn_Clicked , LBtn_Clicked, SBtn_Clicked and EndBtn_Clicked to NewMorseBtn_Clicked to add into the array which will then write it out to a text file I've created.
namespace FlashLightApp2018
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MorsePage : ContentPage
{
//bool exitLoop = false;
public MorsePage()
{
InitializeComponent();
}
private async void NewMorseBtn_Clicked(object sender, EventArgs e)
{
bool isTextEmpty = String.IsNullOrEmpty(MorseName.Text);
if (isTextEmpty)
{
}
else
{
OBtn.IsEnabled = true;
LBtn.IsEnabled = true;
SBtn.IsEnabled = true;
EndBtn.IsEnabled = true;
// String morseName = MorseName.Text;
//String[,] morseSave = new String[100,100];
}
//File.WriteAllText(morseName, text);
//while (exitLoop != true)
//{
//}
}
private void LoadMorseBtn_Clicked(object sender, EventArgs e)
{
}
private void PlayMorseBtn_Clicked(object sender, EventArgs e)
{
}
private void OBtn_Clicked(object sender, EventArgs e)
{
}
private void LBtn_Clicked(object sender, EventArgs e)
{
}
private void SBtn_Clicked(object sender, EventArgs e)
{
}
private void EndBtn_Clicked(object sender, EventArgs e)
{
}
}
}
first, declare you data at the class level (outside of a single method) so that it is accessible from throughout your class
string morseData = string.Empty;
then have your different button methods update the data
private void OBtn_Clicked(object sender, EventArgs e)
{
morseData += ".";
}
private void LBtn_Clicked(object sender, EventArgs e)
{
moreseData += "-";
}
I have two user controls usercontrol_1 and usercontrol_2, using a click event i am bring in usercontrol_1 to a panel called panel_screen on my main form.
private void btn_Click(object sender, EventArgs e)
{
if (!panel_screen.Controls.Contains(usercontrol_1.Instance))
{
panel_screen.Controls.Add(usercontrol_1.Instance);
usercontrol_1.Instance.Dock = DockStyle.Fill;
usercontrol_1.Instance.BringToFront();
}
else
usercontrol_1.Instance.BringToFront();
}
Similarly i wanna bring usercontrol_2 to the same panel on the main form using a button (click event) on usercontrol_1.
How do i do this? any help would be appreciated.
I'm not sure where you are having problem, I think the way you are using usercontrol_1.Instance might cause the issue.
I have working example here, you can try it.
private void btn_Click(object sender, EventArgs e)
{
bool userControlIsAlreadyInPanel = false;
//assuming you are cheking if usercontrol_1 is already there on panel
// you don't want to create new usercontrol, but just bring existing control to the front
foreach(UserControl control in panel_screen.Controls)
{
if (control.GetType() == typeof(usercontrol_1))
{
userControlIsAlreadyInPanel = true;
control.BringToFront();
}
}
if(!userControlIsAlreadyInPanel)
{
usercontrol_1 instane = new usercontrol_1();
panel_screen.Controls.Add(instane);
instane.Dock = DockStyle.Fill;
instane.BringToFront();
}
}
output
namespace WindowsFormsApp1
{
public delegate void MyEventDelegate(object sender, string name);
public partial class Form1 : Form
{
usercontrol_1 _ctrl1 = null;
usercontrol_2 _ctrl2 = null;
public Form1()
{
InitializeComponent();
_ctrl1 = new usercontrol_1();
_ctrl1.Dock = DockStyle.Fill;
_ctrl1.userControlButtonClicked += userControlButtonClicked;
_ctrl2 = new usercontrol_2();
_ctrl2.Dock = DockStyle.Fill;
_ctrl2.userControlButtonClicked += userControlButtonClicked;
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
userControlButtonClicked(_ctrl1, "1");
}
private void userControlButtonClicked(object sender, string name)
{
panel1.Controls.Clear();
if (sender.Equals(_ctrl1))
{
panel1.Controls.Add(_ctrl2);
}
else if (sender.Equals(_ctrl2))
{
panel1.Controls.Add(_ctrl1);
}
}
}
}
namespace WindowsFormsApp1
{
public partial class usercontrol_1 : UserControl
{
public event MyEventDelegate userControlButtonClicked;
public usercontrol_1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyEventDelegate med = userControlButtonClicked;
if (med != null)
{
med(this, "1");
}
}
}
}
namespace WindowsFormsApp1
{
public partial class usercontrol_2 : UserControl
{
public event MyEventDelegate userControlButtonClicked;
public usercontrol_2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyEventDelegate med = userControlButtonClicked;
if (med != null)
{
med(this, "2");
}
}
}
}
Here is my code.
private void PlaceOrder_Click(object sender, EventArgs e)
{
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
I want to add what is in the menu box to a another list box on another form.
Update
(added by jp2code)
Form1 (Main):
namespace WindowsFormsApplication1 {
public partial class RESTAURANT : Form
{
double soup = 2.49;
double ordertotal;
public RESTAURANT()
{
InitializeComponent();
}
private void RESTAURANT_Load(object sender, EventArgs e)
{
}
private void Add_Click(object sender, EventArgs e)
{
MenuBox.Items.Add("Soup");
TotalBox.Items.Add(String.Format("{0:C}", soup));
ordertotal += soup;
total.Text = Convert.ToString(String.Format("{0:C}", ordertotal));
}
private void TotalBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void PlaceOrder_Click(object sender, EventArgs e)
{
new AreYouSure().Show();
this.Show();
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
}
}
Form2 (Confirmation)
namespace WindowsFormsApplication1 {
public partial class Confirmation : Form
{
public Confirmation()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Confirmation_Load(object sender, EventArgs e)
{
}
private void MenuBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
When clicking the 'Send Order' button the items from 'MenuBox' in form 1 need to be sent to the 'MenuBox' in form 2
OtherForm.OtherListbox.Items.Clear();
foreach(var itm in MenuBox.Items)
OtherForm.OtherListbox.Items.Add(itm);
Form1 (Main):
namespace WindowsFormsApplication1 {
public partial class RESTAURANT : Form
{
double soup = 2.49;
double ordertotal;
public RESTAURANT()
{
InitializeComponent();
}
private void RESTAURANT_Load(object sender, EventArgs e)
{
}
private void Add_Click(object sender, EventArgs e)
{
MenuBox.Items.Add("Soup");
TotalBox.Items.Add(String.Format("{0:C}", soup));
ordertotal += soup;
total.Text = Convert.ToString(String.Format("{0:C}", ordertotal));
}
private void TotalBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void PlaceOrder_Click(object sender, EventArgs e)
{
new AreYouSure().Show();
this.Show();
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
}
}
Form2 (Confirmation)
namespace WindowsFormsApplication1 {
public partial class Confirmation : Form
{
public Confirmation()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Confirmation_Load(object sender, EventArgs e)
{
}
private void MenuBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
When clicking the 'Send Order' button the items from 'MenuBox' in form 1 need to be sent to the 'MenuBox' in form 2
It is better that the controls on your other form (ListBox, in this case) are set to Private by default.
In that case, you would either need to set the control's visibility to Public (bad form, in my opinion) or create a method in your other form to accept the parameters from your form.
Consider something like this:
public void ListBoxData(object[] array)
{
listBox1.Clear();
listBox1.AddRange(array);
}
To get the data or selected item information back to your main form, you would likewise create another public object that you could check, like the property below:
public object SelectedItem { get { return listBox1.SelectedItem; } }
I hope that is what you were looking for.
UPDATE:
Using the code you supplied in the post below, I can see you do not have anything in your Confirmation form to send data to, much less a way to pass that information.
If you had a ComboBox, you could do something like follows:
public partial class Confirmation : Form
{
private ComboBox comboBox1;
public void AddRange(object[] array)
{
comboBox1.Items.AddRange(array);
}
}
That does not place the ComboBox anywhere on your form. You would need to work that out.
With that done, I'm guessing you need to edit your PlaceOrder_Click routine:
private void PlaceOrder_Click(object sender, EventArgs e)
{
//new AreYouSure().Show();
//this.Show();
using (var obj = new Confirmation())
{
var list = new List<object>(MenuBox.Items.Count);
foreach (var o in MenuBox.Items)
{
list.Add(o);
}
obj.AddRange(list.ToArray());
if (obj.ShowDialog(this) == DialogResult.OK)
{
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Items.Clear();
ordertotal = 0;
}
}
}
If you are struggling with this, you might need to look into some C# Windows "multiple forms" tutorials.
Here is a YouTube (that I did not sit all the way through): https://www.youtube.com/watch?v=qVVtCPDu9ZU