Where would the responsibility to handle this event be placed? - c#

I have a NavigationBar.cs user control.
I also have NavigationItem.cs user control.
Here's the code for both:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Uboldi.CustomUI
{
public partial class NavigationBar : UserControl
{
public NavigationBar()
{
InitializeComponent();
}
public List<NavigationItem> NavigationItems { private get; set; }
public NavigationItem SelectedItem { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Uboldi.CustomUI
{
public partial class NavigationItem : UserControl
{
public NavigationItem()
{
InitializeComponent();
}
private Image _picture = null;
public Image Picture
{
get
{
return _picture;
}
set
{
_picture = value;
ptbIcon.Image = _picture;
}
}
private string _content = null;
public string Content
{
get
{
return _content;
}
set
{
_content = value;
lblDisplayText.Text = _content;
}
}
}
}
I only want a single NavigationItem in the navigationbar to be 'selected' at any given time.
When an item is selected a different color will be given to it.
My question is, where should I program this code? In the bar, or is it something a button should do and have the bar just invoke that SetYourSelfAsSelected() method?
Thanks.

I would implement this functionality in the NavigationBar for the following reason: Some other developer could use your NavigationItems in a different context where no active items are marked or marked any different. Or one wants to overload the NavigationBar to implement another behaviour for active items.

Related

An unhandled exception of type 'System.StackOverflowException' occurred in WindowsFormsApplication3.exe [duplicate]

This question already has answers here:
Why does Property Set throw StackOverflow exception?
(3 answers)
Closed 2 years ago.
I'm using C# to develop a windows forms application and I required to store certain values (Ex: UserID and Role), in order to use them again in various forms throughout the application.
The User ID and the Role will be changed with each login.
So tried using static classes.
To test it out first, I did the following.
Created "Form1" with a textbox and a button.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
common.text = textBox1.Text;
Form2 obj = new Form2();
obj.Show();
}
}
}
Then created "Form 2" with only a label.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = common.text;
}
}
}
And to interconnect these two forms, created the following class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication3
{
public static class common
{
public static string text
{
get
{
return text;
}
set
{
text = value;
}
}
}
}
The purpose was to see if the label text on form2 would change when clicked on the button after entering text into the textbox in form1.
When running the code, the following error was thrown. Displays that this is thrown from the "set" method of the class.
An unhandled exception of type 'System.StackOverflowException' occurred in WindowsFormsApplication3.exe
If anyone could provide any clarity on this, it would be highly appreciated.
Thanks in advance.
Your set method is calling itself.
You need to add private string and change it and then return the changes via get.
Try this:
private static string _text;
public static string text
{
get
{
return _text;
}
set
{
_text = value;
}
}

How to send a list from a form to another one

I have two forms in a Windows Forms project: Form1 and aCurso.
I'm trying to send a List with objects of a class called curso (I mean: List<curso>) from Form1 to aCurso.
But Visual Studio show this:
Accessibility inconsistency: The type of parameter List<curso> is less accessible than the method aCurso.aCurso(List<curso>).
So, here is the code from Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _18_05_18
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<curso> cursos = new List<curso>();
private void btnAC_Click(object sender, EventArgs e)
{
Form f = new aCurso(cursos);
f.Show();
}
}
}
Here's code from aCurso:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _18_05_18
{
public partial class aCurso : Form
{
List<curso> cursos = new List<curso>();
public aCurso(List<curso> cursos)
{
InitializeComponent();
this.cursos = cursos;
}
}
}
Here's code from class curso:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _18_05_18
{
class curso
{
private string nombre;
public curso(string nombre)
{
this.nombre = nombre;
}
}
}
You cannot expose a public method signature where some of the parameter types of the signature are not public. It wouldn't be possible to call the method from outside since the caller couldn't construct the parameters required.
All you have to do is make curso class public
public class curso
{
private string nombre;
public curso(string nombre)
{
this.nombre = nombre;
}
}

C# - Tier Separation - How to use these delegates?

Here's the relevant code:
ClickMeGame.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary
{
public class ClickMeGame
{
public OnClickMe onClickMeCallback;
public int score;
public ClickMeGame()
{
score = 0;
}
private void IncrementScore()
{
score++;
}
}
}
ClickMeCallBackDefinitions.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary
{
public delegate void OnClickMe();
}
MainWindow.cs (Windows Form)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ClassLibrary;
namespace ClickMe
{
public partial class mainWindow : Form
{
private ClickMeGame game;
public mainWindow()
{
InitializeComponent();
game = new ClickMeGame();
game.onClickMeCallback = clickMeButton_Click();
}
private void clickMeButton_Click(object sender, EventArgs e)
{
UpdateUI();
}
private void UpdateUI()
{
scoreLabel.Text = string.Format("The score is: {0}", game.score);
}
}
}
So what I'm trying to do is, when the user clicks a button present on the form, I want a label on the form to update with the game score which increments with every click.
I'm learning about/want to be able to do this with delegates in that I want to separate the project into 2 tiers; Presenation and Logic. I know it's unnecessary to do so, but I'd like to make it such that when you click the button, the Windows Form receives information about the game score via delegates/callback methods. I'm unsure how to do this, but I tried making the callback definition and referencing it, but I'm lost from there.
Assuming that the UI button uses the click event clickMeButton_Click then here you go.
public partial class mainWindow : Form
{
private ClickMeGame game;
public mainWindow()
{
InitializeComponent();
game = new ClickMeGame();
game.onClickMeCallback = param => UpdateUI();
}
private void clickMeButton_Click(object sender, EventArgs e)
{
game.onClickMeCallback.Invoke();
}
private void UpdateUI()
{
scoreLabel.Text = string.Format("The score is: {0}", game.score);
}
}

Create custom User Control Items

I want to make my own DropDown (Combobox) with visual Items, which contain a Picture, a Name and a Comment. The problem is if I add an Item in the properties everything is okay, visual studio adds the code in the form designer, but the item isn't displayed.
The Item Collection Class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
namespace List_Item_Test
{
public partial class My_Item : CollectionBase
{
public Items this[int Index]
{
get
{
return (Items)List[Index];
}
}
public bool Contains(Items itemType)
{
return List.Contains(itemType);
}
public int Add(Items itemType)
{
//i think hier something is missing???
return List.Add(itemType);
}
public void Remove(Items itemType)
{
List.Remove(itemType);
}
public void Insert(int index, Items itemType)
{
List.Insert(index, itemType);
}
public int IndexOf(Items itemType)
{
return List.IndexOf(itemType);
}
}
}
The Item Container Class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace List_Item_Test
{
public partial class Item_Container : UserControl
{
public Item_Container()
{
InitializeComponent();
}
My_Item hallo = new My_Item();
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public My_Item MyItemTypes
{
get { return hallo; }
set
{
Items hallo1 = new Items();
hallo1.SetBounds(0, 10 + /*hallo.Count - 1 **/ 50, Width, 50);
this.Controls.Add(hallo1);
}
}
}
}

How can I include a class to aspx.cs file?

I can't use a simple class in aspx.cs file. I know i should include some code to declare this class before creating an instance like
User user = new User();
, but I don't know the syntax. I'm new to C# and ASP.net, as you see, and can have other errors in logic so any help is appreciated
This is how my website structure looks like:
This is the code of User.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class User
{
public int Id
{
get { return id; }
set { id = value; }
}
public String Username
{
get { return username; }
set { username = value; }
}
public String Password
{
get { return password; }
set { password = value; }
}
}
This is the code of Default.aspx.cs:
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
doSomething();
}
}
private void doSomething() {
User user = new User(); // error is shown
}
}
use ctr+. placing cursor in the user and you will get help on that.
Edit 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace DBConnection.model.business
{
public class User
{
...
Then using like below in your default.aspx
using DBConnection.model.business;
You can get the reference of your class.
Add using DBConnection.model.business;
or
Point user object and click mouse right click
Select resolve

Categories