Add button from code - c#

I'm trying to add a button to the form using code and iv'e looked it up on the internet but nothing works.
public void addSnake()
{
Button btn = new Button();
btn.Location = new Point(360, 390);
btn.Size = new Size(10, 10);
btn.Text = "";
btn.Name = num + "";
btn.Tag = this.oneD;
btn.IsAccessible = false;
Controls.Add(btn);
}
public Point getPoint()
{
Button btn = (Button)Controls.Find(num + "");
return this.pos; //temporary
}
it says "The name 'Controls' does not exist in the current context". (for both functions)
Note: the functions addSnake and getPoint are inside a Class I made
Full code here: deleted

Your class is not inheriting from System.Windows.Forms.Form. So there is no such property called Controls.
What you can do is pass a reference of the form to the constructor of SnakeB:
public class SnakeB
{
private System.Windows.Forms.Form parentForm;
public SnakeB(System.Windows.Forms.Form parent)
{
parentForm = parent;
}
}
and use it in your methods like this:
public Point getPoint()
{
Button b = parentForm.Controls.Find(num + "") as Button;
return b.Location;
}
public void addSnake(bool isFirst)
{
Button b = new Button();
// ...
parentForm.Controls.Add(b);
}
Usage:
public Form1()
{
InitializeComponent();
SnakeB snake = new SnakeB(this);
}

You need to have instance of the form in your class and add the control to that instance like
public class SnakeB
{
int num;
int oneD;
Point pos = new Point();
Form1 frm1 = new Form1();
frm1.Controls.Add(btn);
How will you have the instance of the form? Like below
public Form1()
{
InitializeComponent();
SnakeB snake = new SnakeB(this);
}
In your class have a similar consructor
public class SnakeB
{
int num;
int oneD;
Point pos = new Point();
Form1 frm1;
public SnakeB()
{
this.num = 0;
this.oneD = 2;
//here construct the maain head button and 2 reguler ones
addSnake(true);
addSnake(false);
addSnake(false);
}
public SnakeB(Form1 frm) : this()
{
this.frm1 = frm;
}

Related

C# dynamically add button event handler with arguments

I've seen many examples of how to add a click event to a dynamically created button, but none of the examples show how to pass arguments with the event.
I am new to C# and just looking for a simple solution. See the code below:
public partial class Main : Form {
public Main() {
InitializeComponent();
// Add a button dynamically
Button Test = new Button();
this.Controls.Add(Test);
Test.Left = 0;
Test.Top = 0;
Test.Width = 100;
Test.Height = 20;
Test.Text = "Hello";
int param1 = 1;
string param2 = "Test";
// Add click event handler with parameters ????
// I know this syntax is wrong, but how do I get param1 & 2
// into the Test_Click ????
Test.Click += Test_Click(param1,param2);
}
private void Test_Click(int param1, string param2) {
MessageBox.Show(param1.ToString() + ": " + param2);
}
You do not provide arguments when adding the event but you provide it in the event it self, in this case the click event arguments are:
private void Test_Click(object sender, EventArgs e)
{
}
the first argument is usually object sender while the second changes depending on the event type, in case of click event it's "EventArgs e"
and for the adding event :
Test.Click += Test_Click;
Hope i helped you.
Ok. I know this is a little ugly, but this is what I came up with from the comments above. If there is a better way to do this, please let me know:
public partial class Main : Form {
public Dictionary<object, Tuple<int, string>> Params = new Dictionary<object, Tuple<int,string>>();
public Main() {
InitializeComponent();
// Add a button dynamically
Button Test = new Button();
this.Controls.Add(Test);
Test.Left = 0;
Test.Top = 0;
Test.Width = 100;
Test.Height = 20;
Test.Text = "Hello";
Test.Name = "Test";
// Add click event handler with parameters
Params.Add(Test, new Tuple<int, string>(1, "Test"));
Test.Click += Test_Click;
}
private void Test_Click(object sender, EventArgs e) {
Tuple<int,string> value;
if (Params.TryGetValue(sender, out value)) {
MessageBox.Show(value.Item1.ToString() + ": " + value.Item2);
}
else {
MessageBox.Show(sender.ToString() + " not found.");
}
}
Here is another solution I came up with without using Dictionary or Tuple by adding the button and the parameters together into a class structure. I like this one better:
class MyButton {
private Button oButton;
private int iParam1;
private string sParam2;
public MyButton(Form Me, int Left, int Top, int Width, int Height, string Text, int Param1, string Param2) {
oButton = new Button();
Me.Controls.Add(oButton);
oButton.Left = Left;
oButton.Top = Top;
oButton.Width = Width;
oButton.Height = Height;
oButton.Text = Text;
iParam1 = Param1;
sParam2 = Param2;
oButton.Click += Click;
}
private void Click(object sender, EventArgs e) {
MessageBox.Show(iParam1.ToString() + ": " + sParam2);
}
}
public partial class Main : Form {
public Main() {
InitializeComponent();
MyButton Test = new MyButton(this, 0, 0, 100, 20, "Hello", 1, "Test");
}
}
Here's a bit cleaner version of what I was talking about (sorry, I wasn't near a computer earlier). The initial implementation of Tuple was truly clunky; it's since become part of the language.
I started with the auto-generated:
public partial class MyForm : Form
{
public MyForm()
{
InitializeComponent();
}
}
And then added this to the form class:
private Dictionary<string, (int param1, string param2)> _parametersMap = new Dictionary<string, (int param1, string param2)>();
If you are using the latest compiler, you can simplify this to:
private Dictionary<string, (int param1, string param2)> _parametersMap = new();
Then I added a method to the form class that the click handler will call:
public void ShowSomething (string buttonName, int i, string s)
{
MessageBox.Show(this, $"Button: {buttonName} cliked: i={i}, s = {s}");
}
All button click handlers have the same method signature. It's determined by the code that raises the click event. So...
public void OnDynamicButtonClick (object sender, EventArgs e)
{
if (sender is Button button)
{
var (param1, param2) = _parametersMap[button.Name];
ShowSomething(button.Name, param1, param2);
}
}
Notice that the if statement uses a pattern matching if mechanism. The code within the if sees button as the sender cast to a Button.
Then, to match your initial code, I put this in the form constructor (after InitializeComponent. It really doesn't belong there. It should be in some event handler, at the very least the Form Load handler, more likely wherever it is that you want to create the buttons (creating them in the constructor kind of defeats the idea of dynamically constructing them):
var firstButton = new Button
{
Name = "FirstButton",
Text = "First",
Location = new Point(100, 100),
Size = new Size(200, 50),
Parent = this,
};
firstButton.Click += OnDynamicButtonClick;
_parametersMap.Add(firstButton.Name, (42, "Test1"));
Controls.Add(firstButton);
var secondButton = new Button
{
Name = "SecondButton",
Text = "Second",
Location = new Point(100, 300),
Size = new Size(200, 50),
Parent = this,
};
secondButton.Click += OnDynamicButtonClick;
_parametersMap.Add(secondButton.Name, (111, "Test2"));
Controls.Add(firstButton);
Note that I created two buttons, both pointing to the same button click handler (that's the point of the sender argument.
If you are going to be doing this a lot, an alternative approach would be to sub-class the Button class, adding your Param1 and Param2 as properties:
public class MyButtonSubClass : Button
{
public int Param1 { get; set; }
public string Param2 { get; set; }
}
(hopefully giving them more meaningful names)
Instances of your subclass are Buttons, just with two extra properties, so you can do this:
var firstButton = new MyButtonSubclass
{
Name = "FirstButton",
Text = "First",
Location = new Point(100, 100),
Size = new Size(200, 50),
Parent = this,
Param1 = 42,
Param2 = "Some Test",
};
firstButton.Click += OnDynamicButtonClick;
Controls.Add(firstButton);
//same for the second button
And then change the click event handler to:
public void OnDynamicButtonClick (object sender, EventArgs e)
{
if (sender is MyButtonSubclass button)
{
ShowSomething(button.Name, button.Param1, button.Param2);
}
}
And the program will appear to work in the same way.
Or, at this point, you could change the event handler to look like:
public void OnDynamicButtonClick(object sender, EventArgs e)
{
if (sender is MyButtonSubclass button)
{
MessageBox.Show(this, $"Button: {button.Name} cliked: i={button.Param1}, s = {button.Param1}");
}
}

How to add variable to list from different class

I am creating app with data base inside in windows forms and i don't know how to pass information through one class to another like here:
public partial class Form1 : Form
{
public Form1()
{
private void button1_Click(object sender, EventArgs e)
{
BazaDanychTabela BazaDanychTabelaVariable = new BazaDanychTabela();
string rejestracja = Rejestracja.ToString();
var dataoddania = dateTimePicker1.Value;
var kosztczescizamiennych = numericUpDown2.Value;
var pracamechanikawgodzinach = numericUpDown1.Value;
var index = MechanicyBox.SelectedIndex;
if (index < 0)
{
MessageBox.Show("Prosze Wybrac Mechanika");
}
else
{
var imie = ListC[index].Name;
var nazwisko = ListC[index].Nazwisko;
// this one I want to add
BazaDanych NowyLog = new BazaDanych(imie, nazwisko, rejestracja, dataoddania, kosztczescizamiennych, pracamechanikawgodzinach);
MessageBox.Show("Nowy log zostal utworzony!");
}
}
}
}
And here is the other class:
public partial class BazaDanychTabela : Form
{
public List<BazaDanych> Logi = new List<BazaDanych>();
}
And I want to add "NowyLog" to "Logi" how?

Mouse events for Picture box that is Created in a C# class

I am Creating a Chess.I added a Picture box on design form as Chessboard..then for each piece (for example horse or elephant and...) Added a Class.cs and created picture box for each piece in those classes.like this:
public class Mohre
{
public Mohre()
{
}
public void draw(Form form,PictureBox pic )
{
pic.Size = new System.Drawing.Size(50, 50);
pic.Image = Chess1.Properties.Resources.sarbaz;
pic.SizeMode = PictureBoxSizeMode.StretchImage;
pic.Visible = true;
form.Controls.Add(pic);
pic.BringToFront();
}
}
&
public class Soldier:Mohre
{
public PictureBox picsoldier = new PictureBox();
public Soldier()
{
picsoldier.Left = 436;
picsoldier.Top = 670;
}
public void movement()
{
picsoldier.Top -= 67;
}
}
(not add picture box on design form Directly.) now I want to when user click on the pieces that created in classesÙˆ movement function will be called.
what should I Do?
You mean this ?
lblname.Click += delegate { nameOfUrFunction (lblname); };
private void nameOfUrFunction (Label lbl)
{
}

Modal dialog like system MessageBox in Windows Phone

I would like to make my own class for setting some values in dialog box in the way that MessageBox.Show() does.
My code is:
MainPage.xaml.cs
using System;
using System.Windows;
using Microsoft.Phone.Controls;
namespace ModalWindow
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
string result = MyModalBox.GiveMeValue();
MessageBox.Show(result);
}
}
}
MyModalBox.cs
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
namespace ModalWindow
{
public class MyModalBox
{
private static Popup modalBox;
public static string GiveMeValue()
{
TextBox textBox = new TextBox();
textBox.Width = 300;
textBox.Height = 100;
Button okButton = new Button();
okButton.Click += okButton_Click;
okButton.Content = "ok";
okButton.Width = 300;
okButton.Height = 100;
StackPanel stack = new StackPanel();
stack.Background = new SolidColorBrush(Colors.Black);
stack.Width = Application.Current.Host.Content.ActualWidth;
stack.Height = Application.Current.Host.Content.ActualHeight;
stack.HorizontalAlignment = HorizontalAlignment.Center;
stack.VerticalAlignment = VerticalAlignment.Center;
stack.Children.Add(textBox);
stack.Children.Add(okButton);
modalBox = new Popup();
modalBox.Child = stack;
modalBox.IsOpen = true;
// how to change my code to return value only after okButton is cklicked?
return textBox.Text;
}
static void okButton_Click(object sender, RoutedEventArgs e)
{
modalBox.IsOpen = false;
}
}
}
Of course it shows no result befor popup appears. How can I change my code to return value onli after clicking button? Is it possible?
Thanks in advance!
You can use TaskComplectionSource for that.
Add this:
public class DialogResult<T>
{
private readonly T _result;
private readonly bool _canceled;
public DialogResult(bool isCanceled)
: this(string.Empty, isCanceled)
{
}
public DialogResult(string result, bool canceled = false)
{
_result = result;
_canceled = canceled;
}
public T GetResults()
{
if (HasDialogBeenCanceled())
throw new InvalidOperationException("Dialog has been canceled - no results");
return _result;
}
public bool HasDialogBeenCanceled()
{
return _canceled;
}
}
// inside your dialog control
private TaskCompletionSource<DialogResult<string>> dialogResultAwaiter;
private Button button;
private TextBlock textBox;
private Popup popup;
public async Task<DialogResult<string>> ShowPopup()
{
dialogResultAwaiter = new TaskCompletionSource<DialogResult<string>>();
button.Tapped += (sender, args) => dialogResultAwaiter.SetResult(new DialogResult<string>(textBox.Text, false));
var popup = new Popup();
popup.Closed += PopupOnClosed;
// popup code
popup.IsOpen = true;
return await dialogResultAwaiter.Task;
}
private void PopupOnClosed(object sender, object o)
{
if (dialogResultAwaiter.Task.IsCompleted)
return;
dialogResultAwaiter.SetResult(new DialogResult<string>(true));
}
In this way you can create your "own await" - which will "end" (and return results) when TaskComplectionSource.SetResult is called.

Event Handlers & delegates (Simple Question)

I have a simple application. Here's how it works. I have a class (MyForm) that inherits from Windows.Forms. It has a button, a label and a textbox. It looks like a chat window.
There's another class (Cliente) that takes an array of strings and it returns a List with a MyForm instance for each element in the array.
I have a third class (Prueba) that makes use of the previous two classes to test them. This class creates four instances of MyForm, and displays them. (I will omit some code and functionality because I know it works correctly.)
I need to be able to type something in one window and when click on the button, it should broadcast this message and display it in all the other windows.
I know I have to use event handlers and delegates, but after hours of looking at tutorials everywhere I can't figure out what to put where.
Would you please help me? If you can point me to a good tutorial or example it'd be enough, but if you can be more specific on my code, it'd be great.
(I can't figure out how to make one instance of MyForm be aware of the other instances, who should be the listener here? I was thinking that Client, but I can't see how to do it.)
Any help will be appreciated!
//MyForm
namespace Dia26 {
//public delegate void ChangedEventHandler(object sender, EventArgs e);
public class MyForm : System.Windows.Forms.Form {
public Button btn = new Button();
public TextBox textbox = new TextBox();
public Label label = new Label();
public Button btnEnviar = new Button();
public delegate void OwnerChangedEventHandler(string newOwner); //~
public event OwnerChangedEventHandler OwnerChanged;
protected void btn_Click(object sender, System.EventArgs e) {
this.Close();
}
protected void btnEnviar_Click(object sender, System.EventArgs e) {
label.Text += textbox.Text + "\n";
textbox.Text = "";
if (this.OwnerChanged != null) {
this.OwnerChanged("something?");
}
}
public MyForm() {
btn.Text = "cerrar";
btn.Left = 400;
btn.Top = 280;
btn.Click += new EventHandler(this.btn_Click);
btnEnviar.Click += new EventHandler(this.btnEnviar_Click);
textbox.Left = 15;
textbox.Top = 20;
textbox.Width = 330;
label.Left = 15;
label.Top = 50;
label.AutoSize = false;
label.Height = 210;
label.Width = 450;
label.BackColor = Color.White;
btnEnviar.Left = 350;
btnEnviar.Top = 17;
btnEnviar.Text = "Enviar";
this.Controls.Add(textbox);
this.Controls.Add(label);
this.Controls.Add(btn);
this.Controls.Add(btnEnviar);
this.SuspendLayout();
this.Name = "MyForm";
this.ResumeLayout(false);
return;
}
}
}
//Cliente.cs
namespace Dia26Prueba {
public class Cliente {
public int creadas;
public int nocreadas;
public List<MyForm> MostrarVentanas(out bool error, ref int creadas, params string[] nombres) {
List<MyForm> list = new List<MyForm>();
int bienCreadas = 0;
foreach (string str in nombres) {
if (str.Length >= 1) {
MyForm mf = new MyForm();
mf.Text = str;
//mf.OwnerChanged += new OwnerChangedEventHandler(mf_OwnerChanged);
list.Add(mf);
mf.Show();
bienCreadas++;
}
}
error = (bienCreadas == creadas);
nocreadas = bienCreadas - creadas;
creadas = bienCreadas;
return list;
}
public void ModificarPosicionYMedidas(MyForm mf, int x = 262, int y = 209, int width = 500, int height = 350) {
mf.Left = x;
mf.Top = y;
mf.Width = width;
mf.Height = height;
}
}
}
// Prueba
namespace Dia29 {
class Prueba {
static void Main(string[] args) {
Cliente cliente = new Cliente();
int n = 4;
Console.WriteLine(cliente.Autor);
if (args.Length != n) {
return;
}
int InstanciasCreadas = n;
bool HayErrores;
List<Dia26.MyForm> list;
list = cliente.MostrarVentanas(
creadas: ref InstanciasCreadas,
error: out HayErrores,
nombres: new string[] { "FirstWindow", "2nd", "3rd", "4th" });
cliente.ModificarPosicionYMedidas(list.ElementAt<MyForm>(0), 0, 0, 512, 384);
cliente.ModificarPosicionYMedidas(list.ElementAt<MyForm>(1), 512, 0, 512, 384);
cliente.ModificarPosicionYMedidas(list.ElementAt<MyForm>(2), 0, 384, 512, 384);
cliente.ModificarPosicionYMedidas(list.ElementAt<MyForm>(3), 512, 384, 512, 384);
for (int i = 0; i < n; i++) {
// .....
Application.Run(list.ElementAt<MyForm>(i));
}
Console.ReadLine();
}
}
}
Here is a small sample. I'm using a interface to remove the coupling between the MainWindow and the ChatWindows.
public class ChatEventArgs : EventArgs
{
public string ChatEventArgs(string message)
{
Message = message;
}
public string Message { get; private set; }
}
public interface IChatMessageProvider
{
event EventHandler<ChatEventArgs> MessageArrived;
void TriggerEvent(object source, ChatEventArgs args);
}
public class MainWindow : IChatMessageProvider
{
public event EventHandler<ChatEventArgs> MessageArrived = delegate{};
public void AddChatWindow()
{
ChatWindow window = new ChatWindow(this);
window.Show();
}
public void TriggerEvent(object source, ChatEventArgs args)
{
MessageArrived(source, args);
}
}
public class ChatWindow :
{
IChatMessageProvider _provider;
public ChatWindow(IChatMessageProvider provider)
{
_provider = provider;
provider.MessageArrived += OnMessage;
}
public void OnMesage(object source, ChatEventArgs args)
{
// since we could have sent the message
if (source == this)
return;
myListBox.Items.Add(args.Message);
}
public void SendButton_Click(object source, EventArgs e)
{
_provider.TriggerEvent(this, new ChatEventArgs(Textbox1.Text));
}
}
There are actualy multiple ways to do it.
Simply make method on Cliente and call it from Prueba. This is simplest and most intuitive solutoin.
Add event to Prueba, pass instance of Prueba to Cliente and let Cliente register to this event.
Use some kind of global static messenger class. Either using events, or simple message passing.

Categories

Resources