Border as control
-
Hi all , i want to clear my form in one loop. i did is that i put one outer canvas "OuterCanvas" example: clearing text box TextBox tx = new TextBox(); foreeach(control ctr in OuterCanvas.controls) if(ctr.Gettype() == tx.Gettype()) ((TextBox)ctr).text = ""; here my problem is that if i have a border in the canvas i am unable to cast that one to the control. i am getting error; any idea any help please
-
Hi all , i want to clear my form in one loop. i did is that i put one outer canvas "OuterCanvas" example: clearing text box TextBox tx = new TextBox(); foreeach(control ctr in OuterCanvas.controls) if(ctr.Gettype() == tx.Gettype()) ((TextBox)ctr).text = ""; here my problem is that if i have a border in the canvas i am unable to cast that one to the control. i am getting error; any idea any help please
Hi, you might want to consult the help about the VisualTreeHelper class. I think this provides the functionality you're looking for. If you need further assinstance, please provide some more concrete code/XAML. Cheers Jürgen
-
Hi all , i want to clear my form in one loop. i did is that i put one outer canvas "OuterCanvas" example: clearing text box TextBox tx = new TextBox(); foreeach(control ctr in OuterCanvas.controls) if(ctr.Gettype() == tx.Gettype()) ((TextBox)ctr).text = ""; here my problem is that if i have a border in the canvas i am unable to cast that one to the control. i am getting error; any idea any help please
Hema Bairavan wrote:
if(ctr.Gettype() == tx.Gettype())
Why dont you just put in a condition to check for and avoid a
border
.foreeach(control ctr in OuterCanvas.controls) if(ctr.Gettype() == tx.Gettype() && ctr.Gettype() != System.Windows.Border) ((TextBox)ctr).text = "";
My signature "sucks" today
-
Hi, you might want to consult the help about the VisualTreeHelper class. I think this provides the functionality you're looking for. If you need further assinstance, please provide some more concrete code/XAML. Cheers Jürgen
_strTemp = _strEmpty; TextBox txtbox = new TextBox(); PasswordBox pwbox = new PasswordBox(); CheckBox ChkBox = new CheckBox(); foreach (Control Ctrl in cvInput.Children) { if (Ctrl.GetType() == txtbox.GetType()) ((TextBox)(Ctrl)).Text = _strTemp; if (Ctrl.GetType() == pwbox.GetType()) ((PasswordBox)(Ctrl)).Password = _strTemp; if (Ctrl.GetType() == ChkBox.GetType()) ((CheckBox)(Ctrl)).IsChecked = false; } this is my code.. i am getting error in the sentence.. foreach (Control Ctrl in cvInput.Children) the exception is.. Unable to cast object of type 'System.Windows.Controls.Border' to type 'System.Windows.Controls.Control'. I am getting the same error for the Image also.. what may be the reason>>??
-
_strTemp = _strEmpty; TextBox txtbox = new TextBox(); PasswordBox pwbox = new PasswordBox(); CheckBox ChkBox = new CheckBox(); foreach (Control Ctrl in cvInput.Children) { if (Ctrl.GetType() == txtbox.GetType()) ((TextBox)(Ctrl)).Text = _strTemp; if (Ctrl.GetType() == pwbox.GetType()) ((PasswordBox)(Ctrl)).Password = _strTemp; if (Ctrl.GetType() == ChkBox.GetType()) ((CheckBox)(Ctrl)).IsChecked = false; } this is my code.. i am getting error in the sentence.. foreach (Control Ctrl in cvInput.Children) the exception is.. Unable to cast object of type 'System.Windows.Controls.Border' to type 'System.Windows.Controls.Control'. I am getting the same error for the Image also.. what may be the reason>>??
Hi, your problem has nothing to do with WPF; it's about C# fundamentals. You might want to take a look at the inheritance tree. There you can see why you get that error. Neither Border nor Image derive from Control. To be honest (really no flaming or insulting intended): The best advice I can give you, is to take a good book about C#- or .Net basics and a little time. In case I'm mistaken, I'll give you some clues (which hopefully will only help you if I'm mistaken): 1. ugly solution: use the VisualTreeHelper class. 2. ugly solution: use a common ancestor 3. good solution: use data binding Both ugly solutions (as well as the provided code) imply that you will never use your own controls - or you'll get an endless list of "if ... GettYpe()". Solution #2 (as well as the provided code) still has to deal with the stacking of WPF objects (you will find the Border inside the Canvas, but you will not find the TextBox inside the Border). Unfortunately I can't recommend any C# book, but if it's up to WPF (and accordingly data binding), I've learned a lot from "WPF unleashed". Cheers Jürgen