Display a picturebox from a form in usecontrol - c#

I need to display the picturebox that is formed in the main form in the usercontrol, but also save subscriptions to this picturebox.
In this class, I fill in the fields and then use foreach to pass the picturebox to the usercontrol
public class UserCtr
{
public event Action<List<Users>> ClickPicture;
public event Action<List<Users>, Group> DoubleClickPicture;
public Group group { get; set; }
public PictureBox picture { get; set; }
public List<Users> users { get; set; }
public UserCtr(Group group, PictureBox picture, List<Users> users)
{
this.group = group;
this.picture = picture;
this.users = users;
this.picture.Click += PictureClick;
this.picture.MouseDoubleClick += PictureDoubleClick;
}
private void PictureClick(object s, EventArgs e)
{
ClickPicture(users);
}
private void PictureDoubleClick(object s, EventArgs e)
{
DoubleClickPicture(users, group);
}
}
foreach loop
foreach (var item in groups.users)
{
item.picture.Image = yellow;
panel.Controls.Add(new UserControl1(item.picture)
{
Text = item.user.Name,
//pb = item.picture
});
}
My usercontrol
public partial class UserControl1 : UserControl
{
public UserControl1(PictureBox pic)
{
InitializeComponent();
Load += (s, e) => pictureBox1 = pic;
Invalidate();
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
label1.Text = Text;
}
}
}
The label is filled in, and the picturebox is empty

Related

List is not set value to the control from presenter

I'm trying to make a list of numericUpDown.value and, when I press the button, it sets a random value from the presenter; but, when I press the button nothing happen, I see the value change but I think it just replace the numeric control with a value instead of set value.
The view :
public partial class Form1 : Form, IForm1
{
public List<decimal> _valueList { get; set; }
public Form1()
{
InitializeComponent();
this._valueList = new List<decimal> { numericUpDown1.Value, numericUpDown2.Value, numericUpDown3.Value, numericUpDown4.Value, numericUpDown5.Value, numericUpDown6.Value };
}
public List<decimal> ValueList
{
get => _valueList; set => _valueList = value;
}
public event EventHandler ButtonClick;
private void button1_Click(object sender, EventArgs e)
{
ButtonClick?.Invoke(sender, e);
}
}
Interface:
public interface IForm1
{
List<decimal> ValueList { get; set; }
event EventHandler ButtonClick;
}
Presenter:
public class PresenterForm1
{
private IForm1 _form1;
public PresenterForm1(IForm1 form1)
{
_form1 = form1;
form1.ButtonClick += ButtonClick;
}
private void ButtonClick(object sender, EventArgs e)
{
var random = new Random();
for(int i = 0; i < 6; i++)
{
_form1.ValueList[i] = random.Next(0, 10);
}
}
If I change the type to List<NumericUpDown> it works, but I think this is the wrong way:
foreach (var item in _form1.ValueList)
{
item.Value = random.Next(0, 99);
}

How can I access label.Text from my User Control

I want to access labelNummer.Text from my User Control in my form.
Form
if (idPartijen.Contains(labelNummer.Text))
{
con.SqlQuery("SELECT * FROM `kandidaat` where `partijnummer` =#nummer ");
con.Cmd.Parameters.Add("#nummer", MySql.Data.MySqlClient.MySqlDbType.VarString).Value = labelNummer.Text;
User Control
public string KandidaatNummer
{
set
{
Nummer = value;
labelNummer.Text = Nummer;
}
}
Your UserControl
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
}
private void MyUserControl_Load(object sender, EventArgs e)
{
}
public string KandidaatNummer
{
get
{
return labelNummer.Text;
}
set
{
labelNummer.Text = value;
}
}
}
Let's say that your user control has a name called: MyUserControl1
if (idPartijen.Contains(myUserControl1.KandidaatNummer)) { ... }

How to bind BindingList<T>.Count property to specific label text?

I have some label that should display actual amount of items that contain BindingList that bound to the DataGridView.
I tried to bind in this way:
CountOfLoadedItemsLabel.DataBindings.Add("Text", _items.Count, String.Empty);
But when BindingList updates, the label that bound to its Count property not changes.
Never used BindingList<T> but this worked for me:
public partial class Form1 : Form
{
private BindingList<Test> list;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.list = new BindingList<Test>
{
new Test(1,"Entry"),
new Test(2,"Another Entry")
};
dataGridView1.DataSource = new BindingSource(list,null);
list.ListChanged += list_ListChanged;
list.Add(new Test(3, "After Binding"));
}
void list_ListChanged(object sender, ListChangedEventArgs e)
{
CountOfLoadedItemsLabel.Text = string.Format("Items: {0}", list.Count);
}
}
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
public Test(int id, string name)
{
this.Id = id;
this.Name = name;
}
}

how apply item click function and display data in next page in windows Phone

public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
myButton.Click += new RoutedEventHandler(myButton_Click);
}
void myButton_Click(object sender, RoutedEventArgs e)
{
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri("http://www.taxmann.com/TaxmannWhatsnewService/mobileservice.aspx?service=topstories"));
}
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
var rootObject = JsonConvert.DeserializeObject<List<NewsItem>>(e.Result);
lstEmployee.ItemsSource = rootObject;
}
public class NewsItem
{
public string news_id { get; set; }
public string news_title { get; set; }
public string website_link { get; set; }
public string imagepath { get; set; }
public string news_date { get; set; }
public string news_detail_description { get; set; }
}
}
This is My code and I am able to Print data in Listview news_title and news_data.
Now I want select item of particular news item and display its news_description in another page.
Please help me how I'll Implement.
I think you are using ListBox .So try like this
<ListBox x:Name="lstEmployee" SelectionChanged="lstEmployee_SelectionChanged_1" /> <br/><br/>
private void lstEmployee_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
NewsItem selectedItem = (sender as ListBox).SelectedItem;
if (selectedItem != null)
{
// pass news detail as parameter and take it from you nerw page
NavigationService.Navigate(new Uri(string.Format( "/Path/YourNewPage.xaml?desc=selectedItem.news_detail_description ", UriKind.Relative));
}
}
this link will help you to understand how parameters can be passed between pages.
Additional: by nkchandra
To pass entire NewsItem to another page, one best approach is to use Application.Current
First create an instance of NewsItem in App.xaml.cs page
public NewsItem selectedNewsItem;
then in your SelectionChanged event handler of ListBox,
NewsItem selectedItem = (sender as ListBox).SelectedItem;
if (selectedItem != null)
{
(Application.Current as App).selectedNewsItem = selectedItem;
// Navigate to your new page
NavigationService.Navigate(new Uri("/YourNewPage.xaml", UriKind.Relative));
}
Finally, in your new page you can access the selectedNewsItem in the same way as above.
NewsItem selectedNewsItem = (Application.Current as App).selectedNewsItem;

How to draw the .NET controls after setting data in properties of specific controls using overrideables?

I need to draw controls using the .NET "show" method of Control.
Suppose I have to draw TreeViewControl on a WinForm by providing test data on some click event, but it's not drawing over the parent WinForm.
Here's the code:
public class TestTreeView: TreeView
{
public TestTreeView()
{
}
public override Color BackColor
{
set;
get;
}
public override Image BackgroundImage { set; get; }
public override ImageLayout BackgroundImageLayout { set; get; }
public BorderStyle BorderStyle { get { return base.BorderStyle; } }
public bool CheckBoxes { get { return base.CheckBoxes; } }
protected override CreateParams CreateParams
{ get { return base.CreateParams; } }
protected Size DefaultSize { get { return base.DefaultSize; } }
protected override bool DoubleBuffered { set; get; }
public TreeViewDrawMode DrawMod { get { return base.DrawMode; } }
public override Color ForeColor { set; get; }
.....
.....
.....
..all other overrideables
#endregion
public void Sort()
{
}
public override string ToString()
{
return null;
}
protected override void WndProc(ref Message m)
{
}
public void setPropeties()
{
this.BackColor = Color.Black;
this.BackgroundImage = null;
this.ForeColor = Color.Blue;
this.Text = "Test Control";
}
public void Show()
{
// I used following line of statement
base.show();
// then for the testing sake, also construct test control object and
// set some data to if, it also didn't work as well.
Control oControl = new Control();
oControl.Text = "Test Control";
oControl.BackColor = Color.Black;
oControl.Height = 125;
oControl.Show();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_Click(object sender, EventArgs e)
{
TestTreeView oTreeView = new TestTreeView();
oTreeView.setPropeties();
oTreeView.Show();
}
}
What should I do?
These lines do no really thing:
Control oControl = new Control();
oControl.Text = "Test Control";
oControl.BackColor = Color.Black;
oControl.Height = 125;
oControl.Show();
The oControl has no Parent (exactly, Container), so where will it show? You need to add it to a container (maybe the control you are creating, using this)

Categories

Resources