|
It's a generic "infrastructure" helper "pattern"; nothing to do with "MVVM".
Plumbing.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Not initially no. But as soon as I had the treeview up and running with binding:
<TreeView x:Name="tree">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:HierarchyBase}" ItemsSource="{Binding Children}" >
<TextBox Text="{Binding Name}"/>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
This was very neat, but now how to bind the canvas to the code behind? I thought this was very messy.
public abstract class HierarchyBase : INotifyPropertyChanged
{
public HierarchyBase()
{
Delete = new RelayCommand( CanDelete, DeleteThis);
children.CollectionChanged +=ItemChildren_CollectionChanged;
}
public Canvas myCanvas;
private HierarchyBase _parent;
private void ItemChildren_CollectionChanged(object sender,NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (var item in e.NewItems)
{
((HierarchyBase)item)._parent = this;
((HierarchyBase)item).myCanvas = this.myCanvas;
}
}
if (e.Action == NotifyCollectionChangedAction.Replace)
{
}
if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (var item in e.OldItems)
{
((HierarchyBase)item)._parent = null;
((HierarchyBase)item).myCanvas = null;
}
}
if (e.Action == NotifyCollectionChangedAction.Move)
{
}
}
private ObservableCollection<HierarchyBase> children = new ObservableCollection<HierarchyBase>();
public ObservableCollection<HierarchyBase> Children
{
get { return children; }
set { children = value; }
}
public ICommand Delete { get; set; }
private bool CanDelete(object obj)
{
return IsEnabled;
}
private void DeleteThis(object obj)
{
this._parent.Children.Remove(this);
}
public string Name { get; set; }
private bool pIsEnabled = true;
public bool IsEnabled
{
get { return pIsEnabled; }
set { pIsEnabled = value;
OnPropertyChanged();
}
}
private bool pIsVisible = true;
public bool IsVisible
{
get { return pIsVisible; }
set {
pIsVisible = value;
OnPropertyChanged();
}
}
private void StateUpdated()
{
if (this is IDrawing)
{
((IDrawing)this).Redraw(IsVisible);
}
foreach (HierarchyBase child in Children)
{
if (!this.IsEnabled || !this.IsVisible)
{
child.IsVisible = false;
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName]string caller = "")
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(caller));
StateUpdated();
}
}
}
Any ideas on how to bind the canvas to the code behind in XAML? All I came up with was to bind the canvas manually to the first HierarchicalBase item when I loaded the program. I just thought I was messy and ugly.
|
|
|
|
|
What we are doing is "shadowing"; we are just going about it in different ways depending on the circumstances.
Your "hierarchyBase" is "view" support code; in particular, Canvas; infrastructure code.
Not sure how your "tree" relates to your earlier "ball"; so I can't visualize or comment on it.
Your architecture is "component based" (versus "view" based); or "views with components"; it should use appropriate patterns.
I would consider integrating with Unity.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
modified 3-Aug-18 14:18pm.
|
|
|
|
|
I have a data file called Access 2003 Table The question "TABCAUHOI" has the following fields:
ID int;
NOIDUNG memo;
The "NOIDUNG" field has data type as memo, I pour data from excel text file into this table in this access file to check the contents are full as excel file, in C # I query read data into DataTable dt = query 1 message of a content, when debugging Debug.Print (dt.Rows [0] ["NOIDUNG"]. ToString ()); Only get 255 characters in the first string, characters after 255 characters later lost, want to get everything into the DataTable how to do ?
|
|
|
|
|
Start by checking your data: look directly at the DB content using Access itself and ensure that the data as entered is complete, and longer than 255 characters. Do not assume it is right: check it.
If it isn't, look at the software you used to enter it.
If it is, then use the debugger to look directly at the DataTable as soon as you have filled it, and check exactly what is in there - it should be the same.
If it isn't, check your loading code and make sure you are referring to the same DB file you checked earlier.
If it is, put the value from the DB into a variable and look at it using the debugger:
object o = dt.Rows [0] ["NOIDUNG"];
string s = o.ToString (); Check exactly what you have there.
We can't do that for you!
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I checked the data from excel to access is good but from Table Access to DataTable is not good. From Table access to the DataTable get the first 255 characters, the characters after 255 are lost. You note: I have no excel data errors on the table access, I have errors from the Table access into the C # winform DataTable and use the OleDb connection.
I tried the cases below to give the one result:
Debug.Print(dt.Rows[0]["NOIDUNG"].ToString());
Debug.Print(Convert.ToString(dt.Rows[0]["NOIDUNG"]));
object o = dt.Rows[0]["NOIDUNG"];
string s = o.ToString();
Debug.Print(s);
|
|
|
|
|
MS Access itself limits the 'char' data type to 255 characters.
To "pour' the data from excel to MS Access you must be using some sort of conversion. If the conversion only allows for 'char' or if you explicitly set it to 'char' then it will only move 255 characters.
|
|
|
|
|
He's using a "Memo"; which supposedly holds 64K.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
And yet he specifically noted that the actual process was producing only and exactly 255 characters.
I was suggesting that the evidence strongly suggested that the conversion process was not in fact using Memo but Char. So something in the steps, presumably multiple, that comprise the entire process was introducing Char.
|
|
|
|
|
You haven't shown any of your "pouring" code.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Hello first time poster, so thanks for any help.
what I'm attempting to do is read a text file that consists of one row of numbers then a space and then a row of names, what I need to do is first read the numbers in the row save them to an int and then read the names and save to string. so i can loop them and compare information.
heres a sameple
1579 ConnerSmith
8880 JaneSmith
4792 ChesterCheto
|
|
|
|
|
Two ways:
1) Use an IndexOf and SubString:
int space = input.IndexOf(' ');
string intPart = input.Substring(0, space);
string strPart = input.Substring(space);
2) Use a regex to separate them:
Match m = Regex.Match(input, @"^(?<int>\d+)\s(?<str>.*)$");
if (m.Success)
{
string intPart = m.Groups["int"].Value;
string strPart = m.Groups["str"].Value;
}
You can then use int.TryParse to convert the number part to an int.
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi...I really need help for a runtime bug.
I have made a custom panel and custom textbox with some properties and one method.
Both Classes(panel and textbox) have one public method called AddMe, witch will add panel to the splitcontainer on form1 and textbox on panel.
My problem is that, at first it worked just fine, panel was added to the splitcontainer and textbox was added to panel. But after i added a second textbox witch also was gonna be added to panel, then the first textbox wont show anymore, nor the the second textbox. I did´nt make the big change on the code. I try to remove the referrence to the second textbox, but nothing helped.
Hope anyone can help...Best regards...
Here is the sourecode...
private void InitializeInputPanel()
{
InputPanel.Width = PnlWidth;
InputPanel.Height = Pnlheight;
InputPanel.BackColor = pnlbackgroundcolor;
InputPanel.BorderStyle = Pnlborderstyle;
InputPanel.Font = Pnlfont;
InputPanel.Dock = Pnldockstyle;
InputPanel.Location = PnlPoint;
InpbBeskrivelseTxtBox inpb = new InpbBeskrivelseTxtBox();
inpb.AddMe(InputPanel);
}
class InpbBeskrivelseTxtBox
{
TextBox BeskrivelseTextBox =new TextBox();
#region Fields
private int txtwidth;
private int txtheight;
private Color txtbackgroundcolor;
private BorderStyle txtborderstyle;
private Font txtfont;
private DockStyle txtdockstyle;
private Point txtpoint;
private string txtText;
private HorizontalAlignment txtAlign;
#endregion
#region Constructors
public void InpbBeskrivelseTextbox()
{
txtwidth = 321;
txtheight = 21;
txtbackgroundcolor = Color.Red;
txtborderstyle = BorderStyle.Fixed3D;
txtfont=new Font("Century",8);
txtdockstyle = DockStyle.Top;
txtpoint = new Point(0, 0);
txtText = "Beskrivelse";
TxtAlign = HorizontalAlignment.Center;
}
#endregion
#region properties
public int Txtwidth { get => txtwidth; set => txtwidth = value; }
public int Txtheight { get => txtheight; set => txtheight = value; }
public Color Txtbackgroundcolor { get => txtbackgroundcolor; set => txtbackgroundcolor = value; }
public BorderStyle Txtborderstyle { get => txtborderstyle; set => txtborderstyle = value; }
public Font Txtfont { get => txtfont; set => txtfont = value; }
public DockStyle Txtdockstyle { get => txtdockstyle; set => txtdockstyle = value; }
public Point Txtpoint { get => txtpoint; set => txtpoint = value; }
public string TxtText { get => txtText; set => txtText = value; }
public HorizontalAlignment TxtAlign { get => txtAlign; set => txtAlign = value; }
#endregion
#region Private Methods
private void InitializebeskrivelsesTextBox()
{
BeskrivelseTextBox.Width = Txtwidth;
BeskrivelseTextBox.Height = Txtheight;
BeskrivelseTextBox.BackColor = Txtbackgroundcolor;
BeskrivelseTextBox.BorderStyle = Txtborderstyle;
BeskrivelseTextBox.Font = Txtfont;
BeskrivelseTextBox.Dock = Txtdockstyle;
BeskrivelseTextBox.Location = Txtpoint;
BeskrivelseTextBox.Text = TxtText;
BeskrivelseTextBox.TextAlign = TxtAlign;
BeskrivelseTextBox.BringToFront();
}
#endregion
#region Public Methods
public void AddMe(Control control)
{
InitializebeskrivelsesTextBox();
control.Controls.Add(BeskrivelseTextBox);
}
#endregion
}
|
|
|
|
|
Hi,
your code is pretty weird. I'll skip many details, and make only a few remarks.
1.
For one, InpbBeskrivelseTxtBox is not a TextBox , it isn't even a Control . Yes, it has a field that holds a TextBox , but that does not turn it into a TextBox.
Therefore
control.Controls.Add(BeskrivelseTextBox); will never succeed, as the AddMe() method expects a Control . So I guess your code won't compile.
What you really want is have class InpbBeskrivelseTxtBox implement a TextBox , a specialized one.
So you should start off with
class InpbBeskrivelseTxtBox : TextBox {...} and remove the TextBox field inside that class; the class itself now is a TextBox, which you can add to a parent Control .
When you do this, InpbBeskrivelseTxtBox automatically inherits all the Properties a TextBox has, so you'll have to organize your initialisation a bit differently, and maybe assign appropriate values in the constructor.
Custom Control: Numeric TextBox: TextBox that alow you to enter only numbers[^] is a short CodeProject article that demonstrates how things could be done. I would suggest you read it.
2.
If you are new to Windows user interface programming, I'd recommend buying and studying a book on the matter, most any C# book should do. Learning from a book may seem a slow process, but trust me it is the fastest way to get acquainted with the proper way of doing things.
3.
Visual Studio offers a choice what to do when compile errors occur; one option is to run the previous version, it once was the default, maybe it still is, I don't know. This is very bad, as it executes an exe that does not match your current source files. Look for the option (probably menu Tools/Options/Projects and Solutions/Build and Run/On Run when errors occur), and make sure it is set to "Do not launch". That will avoid lots of grief, enforcing compilable source code before you can even attempt to run or debug.
modified 28-Jul-18 21:55pm.
|
|
|
|
|
Just to add to Luc's excellent and comprehensive answer.
I'd be a little wary of a control method that adds that control instance to a specific form - from a OOPs point of view that is very wrong: a control should not need to know about it's container as that restricts it's usability. Plus there is the fun and games of working out which Form1 instance it is supposed to be attached to...
A Form should add it's Controls, not the other way around.
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
You could've just passed a reference to the "text box"; instead of duplicating every property.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I want to read the text file on a specific line and perform a task according to the information.
example: I have a string with the value ON, in case the program will read the text file online, if you have the information ON on line 4 performs a task, if OFF accomplishes another task can anyone help me?
|
|
|
|
|
With what part of this? What do you mean by "online"?
|
|
|
|
|
Text files don't have "lines" - they are just a stream of characters, where a specific character (or pair of characters, it's OS dependant) are interpreted by some functions as an "End of Line" character.
As such, the only ways to find "line 4" are to:
1) Read the whole text into a string and scan it for '\n' characters yourself.
2) Use the ReadLine methods provided with text streams to skip lines 1 to 3.
3) Read the whole text into an array of strings using the File.ReadAllLines method, and access each line via the array index.
But ... You are talking about "online" so it gets more complex: you can only read the file if you have access to it, and that means pretty much only on the computer that the software is running on. For C# in a website, that means the server not the client - C# does not run on the client at all, and server code has no direct access to client files.
You may have to bear this in mind when you design your system, as it will probably work in development (where the client and the server are the same physical computer) but fail spectacularly in production.
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
He's going to think he has to do all 3.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Hi, My name is Wish and I have recently coded an application that recognizes commands I ask it to do(open notepad, gives time, etc.) I now want to be able to ask it a question like "who is Barack Obama" and it will respond with the first paragraph of the wikipedia article.
I have this code so far which requires me to manually type in what I want to search for.
This is that code
<pre> var webClient = new WebClient();
var pageSC = webClient.DownloadString("http://en.wikipedia.org/w/api.php?format=xml&action=query&prop=extracts&titles=" + textBox1.Text + "&redirects=true");
XmlDocument doc = new XmlDocument();
doc.LoadXml(pageSC);
var fnode = doc.GetElementsByTagName("extract")[0];
try
{
string ss = fnode.InnerText;
Regex regex = new Regex("\\<[^\\>]*\\>");
string.Format("Before:{0}", ss);
ss = regex.Replace(ss, string.Empty);
string result = String.Format(ss);
richTextBox2.Text = result;
}
catch
{
richTextBox2.Text = "error";
}
Any and all help will be greatly appreciated.
If this is too vauge of a question please tell me and I will try to explain in more detail.
Thanks,
Wish
|
|
|
|
|
You haven't really said what the problem is that you are facing. I assume that the issue you are having is that you don't know how to do the voice recognition part. Would that be a fair assumption?
This space for rent
|
|
|
|
|
yeah thats basically it..
|
|
|
|
|
What are you using for recognizing spoken commands? If it is the MS-agent technology, then it will not accept words that are not defined as a command.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I figured out how to use my voice to search for stuff but now I need to get it to Speak the first paragraph...
modified 28-Jul-18 2:22am.
|
|
|
|