Calling .cs file/program upon click of Button - winforms

//compares text to the id number below
}
CLASS BEING CALLED IS A SEPERATE .CS FILE ALL IN THE SAME PROJECT AND DESCRIBED AS BELOW
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing;
using System.Data.OleDb;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Collections.Generic;
namespace Eagle_Eye_Class_Finder
{
class GetSchedule
{
class IDnumber
{
public string Name { get; set; }
public string ID { get; set; }
public string year { get; set; }
public string class1 { get; set; }
public string class2 { get; set; }
public string class3 { get; set; }
public string class4 { get; set; }
}
//
// Displays the Students Class Schedule.
//
Console.WriteLine("--- Students Class Schedule ---");
foreach (IDnumber IDnumber in IDnumbers)
{
Console.Write(IDnumber.Name);
Console.Write(": ");
Console.WriteLine(IDnumber.ID);
Console.WriteLine(IDnumber.year);
Console.WriteLine(IDnumber.class1);
Console.WriteLine(IDnumber.class2);
Console.WriteLine(IDnumber.class3);
Console.WriteLine(IDnumber.class4);
//get { return this.label1.Text; }
//set { this.label1.Text = class1; }
//get { return this.label2.Text; }
//set { this.label2.Text = class2; }
//get { return this.label3.Text; }
//set { this.label3.Text = class3; }
//get { return this.label4.Text; }
//set { this.label1.Text = class4; }
}
// Clear first two elements in IDnumbers array.
Array.Clear(IDnumbers, 0, Math.Min(2, IDnumbers.Length));
}
}
}

Modify the definition of GetSchedule to Public Class and Public Static Main and then David's code will work.
The default access level for a class defined with just class is internal (C#)/ friend (VB).

The obvious answer is
Eagle_Eye_Class_Finder.GetSchedule.Main()
But, it seems to me that you're hoping to do something with the value
900456317
For that to work, you'd have to declare an additional function which receives that value as a parameter, and then does something useful with it. Maybe something like:
static void ProcessNumber(IDNumber myNum)
{
StringBuilder myData = new StringBuilder();
myData.AppendLine(IDnumber.Name);
myData.AppendLine(": ");
myData.AppendLine(IDnumber.ID);
myData.AppendLine(IDnumber.year);
myData.AppendLine(IDnumber.class1);
myData.AppendLine(IDnumber.class2);
myData.AppendLine(IDnumber.class3);
myData.AppendLine(IDnumber.class4);
MessageBox.Show(myData);
}
Then you can call it like:
if (text == "900456317")
{
Eagle_Eye_Class_Finder.GetSchedule.ProcessNumber(new IDnumber() { Name = "Joshua Banks",ID = "900456317", year = "Senior", class1 = "TEET 4090", class2 = "TEET 3020", class3 = "TEET 3090", class4 = "TEET 4290" });
}
Might be easier if you could give more detail on exactly what you're hoping to accomplish.
EDIT
What you need to do for that to work is move the concept of the IDnumbers array out of the function call itself into a class member. For example, consider adding the following code:
IDnumber[] IDnumbers = new IDnumber[3];
public GetSchedule()
{
IDnumbers[0] = new IDnumber() { Name = "Joshua Banks",ID = "900456317", year = "Senior", class1 = "TEET 4090", class2 = "TEET 3020", class3 = "TEET 3090", class4 = "TEET 4290" };
IDnumbers[1] = new IDnumber() { Name = "Sean Ward", ID = "900456318", year = "Junior", class1 = "ENGNR 4090", class2 = "ENGNR 3020", class3 = "ENGNR 3090", class4 = "ENGNR 4290" };
IDnumbers[2] = new IDnumber() { Name = "Terrell Johnson",ID = "900456319",year = "Sophomore", class1 = "BUS 4090", class2 = "BUS 3020", class3 = "BUS 3090", class4 = "BUS 4290" };
}
This will cause that code to run every time you create a new instance of your class. Then, you can have a function
public string GetDataFromNumber(string ID)
{
foreach (IDnumber idCandidateMatch in IDnumbers)
{
if (IDCandidateMatch.ID == ID)
{
StringBuilder myData = new StringBuilder();
myData.AppendLine(IDnumber.Name);
myData.AppendLine(": ");
myData.AppendLine(IDnumber.ID);
myData.AppendLine(IDnumber.year);
myData.AppendLine(IDnumber.class1);
myData.AppendLine(IDnumber.class2);
myData.AppendLine(IDnumber.class3);
myData.AppendLine(IDnumber.class4);
return myData;
}
}
return "";
}
And then your method call from Form1 changes to:
public void button2_Click(object sender, System.EventArgs e)
{
string text = textBox1.Text;
Mainform = this;
this.Hide();
GetSchedule myScheduleFinder = new GetSchedule();
string result = myScheduleFinder.GetDataFromNumber(text);
if (!string.IsNullOrEmpty(result))
{
MessageBox.Show(result);
}
else
{
MessageBox.Show("Enter A Valid ID Number!");
}
}
The idea here is to break your program into a series of smaller 'parts', each of which is responsible for doing one thing, and doing it well. In this case, the class 'GetSchedule' represents the part of your program which, given an ID number, is able to retrieve a description of that user. The line of code above that reads
GetSchedule myScheduleFinder = new GetSchedule();
basically says "I want a new copy of my GetSchedule class, and I want to keep track of it with the name 'myScheduleFinder'". Whenever you see the word 'new' in C#, that's what's happening. When the word new is followed by the classname and parenthesis, its invoking what's called the 'constructor'. Constructors are basically special functions that are called every time the class is created; in your case, its the code we placed in the method
public GetSchedule()
Now, given that we have a copy of the GetSchedule class, properly initialized by our constructor, we can call the ProcessNumber function on that class, passing in the number we're searching for. The 'if' statement basically makes sure that of all of our possible records, we're only using the one that has the same ID. We then take that record, convert it to a string, and return it. Then we show it in a nifty little message box, although you can obviously do with it whatever you want at that point.

This should do it:
Eagle_Eye_Class_Finder.GetSchedule.Main()

Related

Access treeviewitem from HierarchicalDataTemplate in WPF

I am trying to access the treeviewitem created using HierarchicalDataTemplate based on the Name of the header. Also i want to access the control inside (in this case rectangle) the treeviewitem and change its color. I tried many ways but no success. Below is my code. I am generating Treeview using custom class and xml.
public partial class Window1 : Window
{
ObservableCollection<Step> TreeViewTemplate;
public Window1()
{
TreeViewTemplate = new ObservableCollection<Step>();
InitializeComponent();
SetDataTemplate("NEWSITECOPPER_PROPOSAL", "Proposal");
tvMain.ItemsSource = TreeViewTemplate;
getTreeViewItem();
}
private void getTreeViewItem()
{
TreeViewItem item = (TreeViewItem)(tvMain.ItemContainerGenerator.ContainerFromItem(tvMain.Items[3]));
}
private void SetDataTemplate(string ProcessName, string journeyName)
{
try
{
TreeViewTemplate.Clear();
//XDocument xDoc = XDocument.Load(#"C:\Users\606347769\Desktop\Hemil\Others\TreeView\TreeView\Data.xml");
XDocument xDoc = XDocument.Load(#"C:\Users\606347769\Documents\Visual Studio 2008\Projects\TestAPplication\WpfApplication1\ProcessJourneyCriteria.xml");
var JourneySteps = xDoc.Elements("ProcessAreas").Elements("Process").Where(x =>
x.Attribute("name").Value == ProcessName).Select(y =>
y.Elements("Journey").Where(k => k.Attribute("name").Value == journeyName));
var FinalSteps = JourneySteps.FirstOrDefault();
FinalSteps.Elements("Step").ToList<XElement>().ForEach(x =>
{
string key = x.Attribute("name").Value;
ObservableCollection<ChildStep> value = new ObservableCollection<ChildStep>();
x.Elements("ChildStep").ToList<XElement>().ForEach(y =>
{
ObservableCollection<GrandChildStep> GC = new ObservableCollection<GrandChildStep>();
y.Elements("GrandChildStep").ToList<XElement>().ForEach(k =>
{
GC.Add(new GrandChildStep { Name = k.Attribute("name").Value });
});
value.Add(new ChildStep { Name = y.Attribute("name").Value, GrandChildStep = GC });
});
TreeViewTemplate.Add(new Step { Name = key, ChildStep = value });
});
}
catch (Exception)
{
}
}
}
Below is the custom class i have created
class Step
{
public string Name { get; set; }
public System.Collections.ObjectModel.ObservableCollection<ChildStep> ChildStep { get; set; }
}
class ChildStep
{
public string Name { get; set; }
public System.Collections.ObjectModel.ObservableCollection<GrandChildStep> GrandChildStep { get; set; }
}
class GrandChildStep
{
public string Name { get; set; }
}
You should expose everything you want to access on your (view) model and just bind to it, like the Background of the shape or the IsSelected property of the item (needs to be bound in ItemContainerStyle).
If you need to "access UI controls" in WPF you are usually doing something wrong.
name the child,for eg. in your case x:Name="Rect" to Rectangle
then
Declare this helper method in your Code
T GetVisualChild<t>(DependencyObject parent, string name) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<t>(v, name);
}
if (child != null)
{
break;
}
}
return child;
}
then
just declare Rectangle Rect=new Rectangle(); in constructor or loaded event.
and when you want to access the child. use that declared helper method. for eg.
Rect=GetVisualChild<treeview>(this, "Rect")
Note: here "treeview" is the name of parent You may give the name of parent accessing the child directly.

Execute RaiseCanExecuteChanged from 'subclass'

So I have the following setup:
PLANNING:
public class Planning : ViewModelBase
{
public Planning()
{
AddNewActivityCommand = new RelayCommand(AddActivity, CanAddActivity);
}
public ObservableCollection<PlanningItem> PlanningItems { get; set; }
public PlanningItem SelectedPlan { get; set; }
#region AddNewActivity
public RelayCommand AddNewActivityCommand { get; private set; }
private bool CanAddActivity()
{
if (!PlanningItems.Any())
{
return true;
}
if (string.IsNullOrEmpty(PlanningItems[PlanningItems.Count - 1].Activities) != true ||
PlanningItems[PlanningItems.Count - 1].DhpRepresentativeSelected != null)
{
return true;
}
return false;
}
private void AddActivity()
{
PlanningItems.Add(new PlanningItem());
AddNewActivityCommand.RaiseCanExecuteChanged();
}
#endregion
}
PLANNING ITEM:
public class PlanningItem : ViewModelBase
{
private string _activity;
public ObservableCollection<OutreachUser> DhpRepresentativeSource
{
get
{
var userSource = new ObservableCollection<OutreachUser>();
using (var context = new Outreach_Entities())
{
var query = from a in context.UserInfoes
join b in context.PersonalInfoes on a.UserIdentity equals b.PersonIdentity
join c in context.PersonalTitles on b.TitleLink equals c.TitleIdentity into cGroup
from c in cGroup.DefaultIfEmpty()
select new OutreachUser
{
PersonLink = a.UserIdentity,
Username = a.Username,
FirstName = b.FirstName,
MiddleInitial = b.MiddleInitial,
LastName = b.LastName
};
foreach (var result in query)
{
userSource.Add(result);
}
return userSource;
}
}
}
public OutreachUser DhpRepresentativeSelected { get; set; }
public DateTime PlanningDate { get; set; }
public TimeSpan PlanningStart { get; set; }
public TimeSpan PlanningEnd { get; set; }
public int PlanningTotalHours { get; set; }
public string Activities
{
get
{
return _activity;
}
set
{
_activity = value;
RaisePropertyChanged(nameof(Activities), "", _activity, true);
}
}
}
I have a ListBox bound to the PlanningItems Observable Collection.
I want to be able to add a new item to the list if the following criteria are met:
The Planning Items Collection is empty.
The last item in the Planning Items Collection has a DhpRepresentativeSelected that is not null.
The last item in the Planning Items Collection has some text in the Activities string.
The first item is easy enough because I call AddNewActivityCommand.RaiseCanExecuteChanged(); after I add a new item from an empty list.
Now I need to call the AddNewActivityCommand.RaiseCanExecuteChanged(); from within the PlanningItem ViewModel, but it does not have access rights to the command.
Clueless pointed me to the answer.
What I did was inside of my Planning ViewModel I created an internal Method that called the AddNewActivityCommand.RaiseCanExecuteChanged() method. I think called that method from within the PlanningItems ViewModel.

Passing checkedlistbox.checked items cast as a List<BuiltInCategory> from a Form instance to a Revit class function

I am not having any issues making calls from the form instance to the Revit class. It's when I try to assign a List to the Revit class's function categoryList(), that I get a variable doesn't exist in the context error. I tried prefixing a reference to the instance of the form class "Form UF = new Form;" This doesn't work.
//The Revit Class
public Result Execute(ExternalCommandData commandData, ref string message,....)
{
User_Form UF = new User_Form(commandData);
UF.ShowDialog();
public List<BuiltInCategory> categoryList(Document doc, int intSwitch)
{
//list built in categories for built in filter_1
builtInCats_List = new List<BuiltInCategory>();
switch (intSwitch)
{
case (1):
...
case (3):
...
case (4):
{
builtInCats_List = newStateCb1;
return builtInCats_List;
}
default:
{
builtInCats_List = newStateCb1;
return builtInCats_List;
}
}
}
using Form = System.Windows.Forms.Form;
using WS = ModelAuditor_2014.WorksetSorter_2014;
using ModelAuditor_2014;
using System.Threading;
//The Form
namespace ModelAuditor_2014
{
public partial class User_Form : Form
{
//Constructor
WorksetSorter_2014 WS = new WorksetSorter_2014();
//Revit references
public Autodesk.Revit.UI.UIApplication rvtUiApp;
public Autodesk.Revit.UI.UIDocument rvtUiDoc;
public Autodesk.Revit.ApplicationServices.Application rvtApp;
//Global Variables
public List<BuiltInCategory> Filter01_CategoryList;
public List<BuiltInCategory> Filter02_CategoryList;
public int intSwitch;
public List<BuiltInCategory> newStateCb1;
public User_Form(ExternalCommandData commandData)
{
//Revit references
rvtUiApp = commandData.Application;
rvtUiDoc = rvtUiApp.ActiveUIDocument;
rvtApp = rvtUiApp.Application;
InitializeComponent();
}
public void User_Form_Load(object sender, EventArgs e)
{
//use rvtDoc = Doc
Autodesk.Revit.DB.Document rvtDoc = .....
//CheckedListBox for filter01
checkedListBox1.DataSource = WS.categoryList(rvtDoc, intSwitch = 1);
Filter01_CategoryList = new List<BuiltInCategory>();
Filter01_CategoryList = WS.RetrieveSchema(rvtDoc, false);
foreach (BuiltInCategory ChkedB1 in Filter01_CategoryList)
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.Items[i].ToString() == ChkedB1.ToString())
{
checkedListBox1.SetItemChecked(i, true);
}
}
}
public List<BuiltInCategory> returnNewStateCB1()
{
newStateCb1 = checkedListBox1.CheckedItems.Cast
<BuiltInCategory>().ToList<BuiltInCategory>();
return newStateCb1;
}
I passed the list from the win form to another public function in the revit app, I was able to access the list returned by this function.

WPF DataGrid - Can I decorate my POCOs with attributes to have custom column names?

I have a DataGrid in WPF and fill it with data like this:
public enum Sharing
{
Equal,
SurfaceBased,
}
public class Data
{
public bool Active { get; set; }
public string Name { get; set; }
public int Floor { get; set; }
public Sharing Sharing { get; set; }
}
public ObservableCollection<Data> _col = new ObservableCollection<Data>()
{
new Data(){Active = true, Name = "KRL", Floor = 0 },
new Data(){Name = "DAT", Floor = 1},
new Data(){Name = "TRE", Floor = 1},
new Data(){Name = "DUO", Floor = 2},
};
public MainWindow()
{
InitializeComponent();
grid.AutoGenerateColumns = true;
grid.DataContext = _col;
grid.ItemsSource = _col;
}
I was wondering if I could use some attributes on the enumerations and the POCO class so that the DataGrid displays them (instead of the variable names) on the headers and ComboCoxes.
Something like this:
public enum Sharing
{
[Name("This is a test")]
Equal,
[Name("This is a test 2")]
SurfaceBased,
}
Is this possible?
OK. Here is the way to do it for the Headers:
You add attributes, like Description attributes to your Properties.
public class MyPOCO
{
[Description("The amount you must pay")]
public float Amount { get; set; }
}
Then, in a class derived from DataGrid you do this:
protected override void OnAutoGeneratingColumn(DataGridAutoGeneratingColumnEventArgs e)
{
try
{
base.OnAutoGeneratingColumn(e);
var propDescr = e.PropertyDescriptor as System.ComponentModel.PropertyDescriptor;
e.Column.Header = propDescr.Description;
}
catch (Exception ex)
{
Utils.ReportException(ex);
}
}
For adding custom names to the members of the enumerations, you need to make a custom column. You can see a simple example here : https://stackoverflow.com/a/17510660/964053.

What is the right way to save and restore a disconnected entity using code first?

So that I can store the user's screen preferences, I have ScreenSettings entity that I want to retrieve when the program starts and save when the program ends.
For this reason I don't want to keep the context open.
I am wondering about the best way to do this.
I have tried the following
however I am not comfortable with the SaveSettings function because it deletes and re-adds the object.
How do I save changes to the object without actually replacing it?
namespace ClassLibrary1
{
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
//Domain Class
public class ScreenSetting
{
#region Properties
public int Id { get; set; }
[Required]
public int WindowLeft { get; set; }
[Required]
public int WindowTop { get; set; }
#endregion
}
// Context
public class Context : DbContext
{
#region Properties
public DbSet<ScreenSetting> ScreenSettings { get; set; }
#endregion
}
// UI
public class UI
{
#region Public Methods
// Get the settings object
public ScreenSetting GetSettings(int SettingsId)
{
var Db = new Context();
ScreenSetting settings = Db.ScreenSettings.Find(SettingsId);
if (settings == null)
{
settings = new ScreenSetting { Id = SettingsId, WindowTop = 100, WindowLeft = 100 };
Db.ScreenSettings.Add(settings);
}
Db.Dispose();
return settings;
}
// Save the settings object
public void SaveSettings(ScreenSetting settings)
{
var Db = new Context();
ScreenSetting oldSettings = Db.ScreenSettings.Find(settings.Id);
if (oldSettings == null)
{
Db.ScreenSettings.Add(settings);
}
else
{
Db.ScreenSettings.Remove(oldSettings);
Db.ScreenSettings.Add(settings);
}
Db.Dispose();
}
public void test()
{
ScreenSetting setting = this.GetSettings(1);
setting.WindowLeft = 500;
setting.WindowTop = 500;
this.SaveSettings(setting);
}
#endregion
#region Methods
private static void Main()
{
var o = new UI();
o.test();
}
#endregion
}
}
You ran into a common pattern, update or insert, which is so common that it's got a name: upsert. When a pattern is common, usually there also is a common solution.
In System.Data.Entity.Migrations there is an extension method AddOrUpdate that does exactly what you want:
public void SaveSettings(ScreenSetting settings)
{
using (var db = new Context())
{
db.ScreenSettings.AddOrUpdate(settings);
db.SaveChanges();
}
}

Resources