How to: Get information from dynamic data - c#

This might sound strange at first but its rather simple. I have a wcf webservice (restful) in this service I have set some generic "user" fields and one of the fields which I have set is the userID to increment every time a user is added. All pretty simple stuff.
I then have a consuming wpf application. For each of the users mentioned above in my webservice I dynamically set out content to hold each of those users in my wpf application which is done on button click, this then loads the content area with groupboxs and textblocks neatly arranged (again pretty simple). The containers I hold them in are GroupBox.Header which holds the userID and Textblock which equals the students first name and last name.
Like so:
private void button1_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
PanelMainContent.Children.Clear();
MainArea1.Children.Clear();
string uriGroups = "http://localhost:8000/Service/Student";
XDocument xDoc = XDocument.Load(uriGroups);
var sortedXdoc = xDoc.Descendants("Student")
.OrderByDescending(x => Convert.ToDateTime(x.Element("TimeAdded").Value));
foreach (var node in xDoc.Descendants("Student").OrderByDescending(x => Convert.ToDateTime(x.Element("TimeAdded").Value)))
{
GroupBox groupbox = new GroupBox();
groupbox.Header = String.Format(node.Element("StudentID").Value);
App.Current.Properties["groupboxHeader"] = groupbox.Header;
groupbox.Width = 100;
groupbox.Height = 100;
groupbox.Margin = new Thickness(1);
Button btnFindStudent = new Button();
btnFindStudent.Click += this.btnGeneral_Click;
btnFindStudent.Name = Convert.ToString("btnViewStudent");
btnFindStudent.Tag = Convert.ToString("ViewStudent");
btnFindStudent.Content = Convert.ToString("View");
btnFindStudent.HorizontalAlignment = HorizontalAlignment.Right;
btnFindStudent.Height = 20;
btnFindStudent.Width = 36;
TextBlock textBlock = new TextBlock();
textBlock.Text = String.Format(node.Element("FirstName").Value + " " + (node.Element("LastName").Value));
textBlock.TextAlignment = TextAlignment.Center;
TextBlock textBlock1 = new TextBlock();
textBlock1.Text = (DateTime.Parse(node.Element("TimeAdded").Value)).ToString("d");
String.Format("{0:d/M/yyyy}", DateTime.Parse(node.Element("TimeAdded").Value));
textBlock1.TextAlignment = TextAlignment.Center;
textBlock1.VerticalAlignment = VerticalAlignment.Bottom;
StackPanel stackPanel = new StackPanel();
stackPanel.Children.Add(groupbox);
stackPanel.Children.Add(btnFindStudent);
stackPanel.Children.Add(textBlock);
stackPanel.Children.Add(textBlock1);
stackPanel.Margin = new Thickness(5);
stackPanel.MouseEnter += new MouseEventHandler(stackpanel_MouseEnter);
stackPanel.MouseLeave += new MouseEventHandler(stackpanel_MouseLeave);
MainArea1.Children.Add(stackPanel);
}
}
And the output looks like this:
Notice the GroupBox.header states the UserID.
So this brings me to the problem... if you notice in the image I also dynamically set a button (code above). This button loads a usercontrol applie named ThisUser :) but the problem I am having is being able to:
1) First get GroupBox.Header(UserID) of the currently clicked
student (SOLVED by Darins answer below)
2) Send that UserID to the usercontrol "ThisUser"
To explain question 1):
Looking at the code above is there a way in which I can "Get" the groubox.header of that user who has been clicked ("view" button in the image of my app)
And for question 2)
Somehow "send" from my current app to the usercontrol "ThisUser" or "make available" for the usercontrol to then consume.
I hope this has been nice and clear and layed out well to provide the best possible answer, if there is anything else you wish to know (code, explanation) please dont hesitate to ask.
Code to send data on btnGeneralClick (view button):
public FindStudent()
{
InitializeComponent();
}
private void btnGeneral_Click(object sender, RoutedEventArgs e)
{
ViewStudent myusercontrol = new ViewStudent();
String id = (String)((Button)sender).Tag;
myusercontrol.StudentID = id;
PanelMainContent.Children.Add(myusercontrol);
}
UserControl:
public partial class ViewStudent : UserControl
{
public ViewStudent()
{
InitializeComponent();
}
private string _studentID;
public string StudentID
{
get
{
return _studentID;
}
set
{
_studentID = value;
}
}
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
groupBox1.Header = StudentID;
//or
textBlock1.Text = StudentID;
}
}

Well, first, I'd put the StudentID in the btnFindStudent.Tag
btnFindStudent.Tag = node.Element("StudentID").Value.ToString();
When the ClickEvent is fired, you can pull the ID out of the Tag and send it off to your ThisUser UserControl.
String id = (String)((Button)sender).Tag;
I'd just create a public property in the UserControl called "StudentID" and set it there. It's hard to say without seeing your UserControl. :(
More Information:
Add this to ViewStudent:
#region StudentID
public static readonly DependencyProperty StudentIDProperty = DependencyProperty.Register("StudentID", typeof(String), typeof(ViewStudent), new PropertyMetadata(OnStudentIDChanged));
public string StudentID
{
get { return (string)GetValue(StudentIDProperty); }
set { SetValue(StudentIDProperty, value); }
}
static void OnStudentIDChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as ViewStudent).OnStudentIDChanged(e);
}
void OnStudentIDChanged(DependencyPropertyChangedEventArgs e)
{
groupBox1.Header = StudentID;
textBlock1.Text = StudentID;
}
#endregion
And get rid of that other StudentID Property we made earlier.
ViewStudent myUserControl = new ViewStudent();
myUserControl.StudentID = (String)((Button)sender).Tag;
Give that a shot. (:

Related

C# Winforms App requires two clicks on Button_Click Event

The button requires two clicks to fire up the event. Here is an image and the code.There is a combobox which triggers the button with different items, but when I click the button to show an item in a panel on the page, I have to click it twice so it can trigger the event. After selecting an item once by twice-clicking it, every next time i click it works with one click, just like it should.
Here is the image of the combobox which triggers the button
And there is the code :
namespace Carbon
{
public partial class ucAnaliza : MetroFramework.Controls.MetroUserControl
{
static ucAnaliza _instance;
public static ucAnaliza Instance3
{
get
{
if (_instance == null)
_instance = new ucAnaliza();
return _instance;
}
}
public MetroFramework.Controls.MetroPanel MetroAnaliza
{
get { return mPanelAnaliza; }
set { mPanelAnaliza = value; }
}
public ucAnaliza()
{
InitializeComponent();
}
private void ucAnaliza_Load(object sender, EventArgs e)
{
}
private void mPotvrdiElementi_Click(object sender, EventArgs e)
{
switch (((ComboBox)mDropAnaliza).SelectedItem.ToString())
{
case "Главна рамка":
_instance = this;
ucGlavna uc = new ucGlavna();
uc.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc);
break;
case "Челна рамка":
_instance = this;
ucCelna uc2 = new ucCelna();
uc2.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc2);
break;
case "Подолжна рамка":
_instance = this;
ucPodolzna uc3 = new ucPodolzna();
uc3.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc3);
break;
}
}
}
}
Here is the code from the designer for the button :
// mPotvrdiElementi
//
this.mPotvrdiElementi.BackColor = System.Drawing.Color.Transparent;
this.mPotvrdiElementi.CausesValidation = false;
this.mPotvrdiElementi.Cursor = System.Windows.Forms.Cursors.Hand;
this.mPotvrdiElementi.ForeColor = System.Drawing.SystemColors.MenuBar;
this.mPotvrdiElementi.Image = global::Carbon.Properties.Resources.Checked_Checkbox_24px;
this.mPotvrdiElementi.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.mPotvrdiElementi.ImageSize = 24;
this.mPotvrdiElementi.Location = new System.Drawing.Point(758, 34);
this.mPotvrdiElementi.Name = "mPotvrdiElementi";
this.mPotvrdiElementi.Size = new System.Drawing.Size(80, 25);
this.mPotvrdiElementi.Style = MetroFramework.MetroColorStyle.Orange;
this.mPotvrdiElementi.TabIndex = 4;
this.mPotvrdiElementi.TabStop = false;
this.mPotvrdiElementi.Text = "Потврди";
this.mPotvrdiElementi.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.mPotvrdiElementi.UseCustomBackColor = true;
this.mPotvrdiElementi.UseCustomForeColor = true;
this.mPotvrdiElementi.UseSelectable = true;
this.mPotvrdiElementi.UseStyleColors = true;
this.mPotvrdiElementi.Click += new System.EventHandler(this.mPotvrdiElementi_Click);
I know it is a long time ago but I was having the same problem...
But I found a solution to the problem and is working every time and not killing the usability.
private int focusFlag = 0;
private void MainForm_MouseEnter(object sender, EventArgs e)
{
if (focusFlag < 1)
{
this.FocusMe();
++focusFlag;
}
}
This will not always try to focus on that form when trying to go to other forms or something else, it will just focus once and that is enough... after that it will behave normally :)
It seems the MetroForm doesn´t get Focus until you click within the form and it is just a bug from the developers of the MetroFramework when using certain Metro Controls within the Form.
I have seen others posting the same problem when they are using the MetroFramework.
Hopefully this will help.

C# Object Panels

The problem I'm having is where I can successfully use the RoomID of the Room object I've created, but not the panel. Here is the function where I set the label to the name of the room. (This is my Form1.cs)
public partial class Form1 : Form
{
Room ThisRoom = new Room();
public Form1()
{
InitializeComponent();
this.Text = "Aquinas College Master Controller";
}
private void roomDesignerToolStripMenuItem_Click(object sender, EventArgs e)
{
new RoomDesigner().Show();
}
private void button1_Click(object sender, EventArgs e)
{
roomsToolStripMenuItem.DropDownItems.Clear();
foreach (var Room in Global.Aquinas.Aquinas)
{
ToolStripMenuItem NewItem = new ToolStripMenuItem(Room.RoomID);
NewItem.Name = Room.RoomID;
NewItem.Click += new EventHandler(ItemClick);
roomsToolStripMenuItem.DropDownItems.Add(NewItem);
}
}
void ItemClick(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
label2.Text = item.Name;
foreach (var Room in Global.Aquinas.Aquinas)
{
if (Room.RoomID == item.Name)
{
ThisRoom = Room;
break;
}
}
Panel RoomPanel = ThisRoom.Panel;
RoomPanel.Size = new Size(607, 304);
RoomPanel.Location = new Point(144, 27);
RoomPanel.BackColor = Color.White;
this.Controls.Add(RoomPanel);
}
}
This is some of my code for the room object. (This is in Room.cs)
public class Room
{
private List<CtrlComputer> _Computers;
public List<CtrlComputer> Computers
{
get { return _Computers; }
set { _Computers = value; }
}
private string _RoomID;
public Room()
{
_Computers=new List<CtrlComputer>();
}
public string RoomID
{
get
{
return _RoomID;
}
set
{
_RoomID = value;
}
}
public Panel Panel
{
get
{
return _Panel;
}
set
{
_Panel = value;
}
}
private Panel _Panel;
}
And here is where I register the panel which I have just put my designs on, to a new room. (This is some of my RoomDesigner.cs)
public partial class RoomDesigner : Form
{
Room NewRoom = new Room();
Panel RoomDesignerPanel = new Panel();
public RoomDesigner()
{
InitializeComponent();
RoomDesignerPanel.Size = new Size(607, 304);
RoomDesignerPanel.Location = new Point(144, 27);
RoomDesignerPanel.BackColor = Color.White;
this.Controls.Add(RoomDesignerPanel);
textBox1.ForeColor = SystemColors.GrayText;
textBox1.Text = "Enter Computer ID Here";
this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter);
textBox2.ForeColor = SystemColors.GrayText;
textBox2.Text = "Enter Room ID Here";
this.textBox2.Leave += new System.EventHandler(this.textBox2_Leave);
this.textBox2.Enter += new System.EventHandler(this.textBox2_Enter);
}
private void textBox1_Leave(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0)
{
textBox1.Text = "Enter Computer ID Here";
textBox1.ForeColor = SystemColors.GrayText;
}
}
private void textBox1_Enter(object sender, EventArgs e)
{
if (textBox1.Text == "Enter Computer ID Here")
{
textBox1.Text = "";
textBox1.ForeColor = SystemColors.WindowText;
}
}
private void textBox2_Leave(object sender, EventArgs e)
{
if (textBox2.Text.Length == 0)
{
textBox2.Text = "Enter Room ID Here";
textBox2.ForeColor = SystemColors.GrayText;
}
}
private void textBox2_Enter(object sender, EventArgs e)
{
if (textBox2.Text == "Enter Room ID Here")
{
textBox2.Text = "";
textBox2.ForeColor = SystemColors.WindowText;
}
}
private void button1_Click(object sender, EventArgs e)
{
CtrlComputer NewComputer = new CtrlComputer();
NewComputer.ComputerID = textBox1.Text;
NewComputer.Text = NewComputer.ComputerID;
NewComputer.Parent = RoomDesignerPanel;
RoomDesignerPanel.Controls.Add(NewComputer);
NewRoom.Panel = RoomDesignerPanel;
NewRoom.Add(NewComputer);
}
When I try to reload that panel, the RoomID of that object is returned fine, yet the panel is not. Any ideas?
Edit: Sorry for the lack of clarity in my post, first posts aren't always easy.
The panel is created in the designer, but I tried private Panel Panel1 = new Panel(); with no luck. My current problem is that in Form1.cs, the program will throw "Cannot access a disposed object", but I followed the guide here https://msdn.microsoft.com/en-us/library/82785s1h(v=vs.110).aspx .
What you are looking to do is swap between a collection of Panel objects (kind of like your own version of TabPages). To do this you need to create each of the Panel objects (You do this in RoomDesigner, which is fine, but there is a catch: see * below). With that collection in hand, when you want to show it on the form, you need to remove the panel1 the designer code created (Controls.Remove(panel1)) and insert your new one (Controls.Add(RoomPanel)).
Since Controls utilize system resources, make sure to Dispose any control you will no longer use. Here is an example method:
//Initial case where no room has been displayed yet and panel1 is still valid
private Panel CurrentPanel = null;
public void SwapPanel(Panel p)
{
//If no panel has been placed yet, get rid of the default one
if (panel1 != null)
{
//we should never need the designer panel again, so dispose it
panel1.Dispose();
this.Controls.Remove(panel1);
}
else
{
//we may return to this panel later, so don't dispose it
this.Controls.Remove(CurrentPanel);
}
CurrentPanel = p;
this.Controls.Add(p);
}
When you finally close out and no longer need your Panels stored in your Room collection, make sure to Dispose them. (However, if your whole program is exiting at that point then it doesn't matter, but do it anyway as good practice)
*When you close any RoomDesigner form (or any form), all controls it contains in 'Controls' will be disposed, even if you are using them elsewhere. To prevent this, remove the controls you want to preserve from the RoomDesigner Controls collection before it is closed.

Passing Button_Click Tag to another page WPF C#

Hi have a button with click event & tag value binding from an XmlDataProvider;
<Button x:Name="Open" Tag="{Binding XPath=Id}" Content="Open" Click="OpenProject_Click" />
and in my xaml.cs click event code (creates new TabItem, with other page content);
private void Open_Click(object sender, RoutedEventArgs e)
{
var ID = ((Button)sender).Tag;
TabItem tabitem = new TabItem();
tabitem.Header = ID;
tabitem.Tag = ID;
Frame tabFrame = new Frame();
Pages.Views.View newTab = new Pages.Views.View(ID);
tabFrame.Content = newTab;
tabitem.Content = tabFrame;
AppTabs.Items.Add(tabitem);
tabitem.Focus();
}
Below is my Other Page code;
public partial class View : Page
{
Object valueFromPage1;
public View()
{
InitializeComponent();
}
public View(Object val)
: this()
{
valueFromPage1 = val;
this.Loaded += new RoutedEventHandler(View_Loaded);
}
void View_Loaded(object sender, RoutedEventArgs e)
{
text.Text = "Value passed from page1 is: " + valueFromPage1;
}
}
Problem is that the value that appears on the other page as System.Xml.XmlElement when it need to be the ID Value (which is a number). Is there away to convert the System.Xml.XmlElement back to the button.Tag value?
That the value appears as "System.Xml.XmlElement" indicates that the Tag is an XmlElement. It has an attribute InnerText that contains its value (which should be the Id you are looking for).
var ID = (((Button)sender).Tag as XmlElement).InnerText;

Using text from dynamically created TextBoxes

I've got a question regarding textboxes in C#. I've made a button that will create textboxes when clicked:
private void helloButton_Click(object sender, EventArgs e)
{
TextBox txtRun = new TextBox();
TextBox txtRun2 = new TextBox();
txtRun2.Name = "txtDynamic2" + c++;
txtRun.Name = "txtDynamic" + c++;
txtRun.Location = new System.Drawing.Point(40, 50 + (20 * c));
txtRun2.Location = new System.Drawing.Point(250, 50 + (20 * c));
txtRun2.ReadOnly = true;
txtRun.Size = new System.Drawing.Size(200, 25);
txtRun2.Size = new System.Drawing.Size(200, 25);
this.Controls.Add(txtRun);
this.Controls.Add(txtRun2);
}
How can I pull the text which the user types into these newly generated textboxes to use it as arguments for a different function (which will be called by a different button)? I'm quite new at this and could use the help.
Thanks in advance.
var matches = this.Controls.Find("txtDynamic2", true);
TextBox tx2 = matches[0] as TextBox;
string yourtext = tx2.Text;
This will return an array of controls by the name txtDynamic2, in your case the first one would be the control you are looking for unless you create more controls having the same name.
This will allow you to fully access the textbox if you found it.
var text = (TextBox)this.Controls.Find("txtDynamic2", true)[0];
If you'd like to use the instantiated textboxes in other methods then you can achieve that by either passing them to the method, or storing them as members of your class.
Example of storing them in your class below.
public class YourForm
{
private TextBox txtRun;
private TextBox txtRun2;
private void helloButton_Click(object sender, EventArgs e)
{
txtRun = new TextBox();
txtRun2 = new TextBox();
// removed less interesting initialization for readability
this.Controls.Add(txtRun);
this.Controls.Add(txtRun2);
}
public void DoStuffWithTextBoxes()
{
if (txtRun != null && txtRun2 != null)
{
// Retrieve text value and pass the values to another method
SomeOtherMagicMethod(txtRun.Text, txtRun2.Text);
}
}
private void SomeOtherMagicMethod(string txtRunText, string txtRun2Text)
{
// Do more magic
}
}
You can do it very easily:
//get the text from a control named "txtDynamic"
string text = this.Controls["txtDynamic"].Text;
Just remember to make sure that your controls have unique Name property, otherwise You'll get the text from the first control that's found with the specified name.

How to access a class from a listbox item?

I build a class that stores values from an XML file. I am loading that data into a ListBox like this:
private void LoadBookXML(string bookXML, ListBox theBookListBox)
{
XDocument loadedData = XDocument.Load(bookXML);
var data = from query in loadedData.Descendants("book")
select new Books
{
Title = (string)query.Attribute("title").Value.ToUpper(),
Theme = (string)query.Attribute("theme").Value,
Abbreviation = (string)query.Attribute("abbr").Value,
Chapters = (string)query.Attribute("chapters").Value,
Writer = (string)query.Attribute("writer").Value,
Place = (string)query.Attribute("place").Value,
Completed = (string)query.Attribute("completed").Value,
Time = (string)query.Attribute("time").Value,
Summary = (string)query.Attribute("summary").Value,
Link = (string)query.Attribute("link").Value,
Number = (string)query.Attribute("number").Value,
WriterAndPlace = "Written by " + (string)query.Attribute("writer").Value + " in " + (string)query.Attribute("place").Value,
};
theBookListBox.ItemsSource = data;
GestureService.GetGestureListener(theBookListBox).Hold += new EventHandler<GestureEventArgs>(theBookListBox_Hold);
When I press and hold an item in the ListBox, I am calling a function that brings up context menu like this:
private void theBookListBox_Hold(object sender, GestureEventArgs e)
{
AppState state = ThisApp._appState;
if (e.OriginalSource is TextBlock)
{
if (((string)((e.OriginalSource as TextBlock).Name)) == "btBookName")
{
UpdateLayout();
_cmReader = new ContextMenu();
MenuItem item = new MenuItem();
item.IsEnabled = true;
item.IsHitTestVisible = true;
TextBlock block = new TextBlock();
block.Text = "HELP, I AM TRYING TO GET DATA FROM THE BOOKS CLASS!!!";
block.TextDecorations = TextDecorations.Underline;
item.Header = block;
MenuItem item5 = new MenuItem
{
Header = "book info"
};
item5.Click += new RoutedEventHandler(bookInfo_Click);
_cmReader.Items.Add(item);
_cmReader.Items.Add(item5);
OpenContextMenu();
}
}
}
So my question is how can I access the data from the Books class that is stored into each of the ListBoxItems? I am using C#.
is that what you mean?
Books book = theBookListBox.SelectedItem as Books;
//All
foreach(Books book in theBookListBox.Items)
{
}
I think your class is inside of the form.
So its better do this...
private void LoadBookXML()
{
string bookXmL = "";
listBox theBookListBox = new ListBox();
Your other code here..
}
then.....
private void theBookListBox_Hold(object sender, GestureEventArgs e)
{
AppState state = ThisApp._appState;
if (e.OriginalSource is TextBlock)
{
if (((string)((e.OriginalSource as TextBlock).Name)) == "btBookName")
{
LoadBookXML();
Your other code here...
}
}
}

Categories

Resources