Extended control value is not setting from UI - c#

Hi I'm trying to extend the TextBox use that extends one in my code. in the extended control, By default, it will trim start and end.
the code I have tried
public class TextboxTrimSpaceing : TextBox
{
private string myVar;
new public string Text
{
get { return myVar; }
set { myVar = value.TrimEnd().TrimStart(); }//Control is not coming here
}
}
UI
<local:TextboxTrimSpaceing x:Name="TrimSpaceing" Text=" avi aaa "></local:TextboxTrimSpaceing>
var i = TrimSpaceing.Text; //Getting Null
Here why my control is not going to the setter and why I'm getting Null result
Note: it's displaying proper string in UI and I know I can maintain it in code. but I need like this.

If you want to trim the text, you could override the OnTextChanged method:
public class TextboxTrimSpacing : TextBox
{
private bool _trim = true;
protected override void OnTextChanged(TextChangedEventArgs e)
{
base.OnTextChanged(e);
if(_trim)
{
_trim = false;
Text = Text?.Trim();
_trim = true;
}
}
}
It makes no sense to define a new Text property.

Related

Get selected/highlighted Text out of GridView

When I highlight/mark a text with my mouse in a GridView, is it possible to get access to this selected Text? I want to save it in a string variable when I click for example on a button. It's not important if I get that text via a GridView access or something else, I need simply the marked text. I tried to trigger ctrl+c by clicking the button to safe it in clipboard, but I had problems with System.Windows.Forms. I didn't find any events like gridview.GetSelectedText for example.
I made a controller and first of all I want to get access to the selected text in the gridView:
namespace XPress.Module.Controllers
{
public class TransactionAddStringToCategoryKeywordController : ObjectViewController<ObjectView, Transaction>
{
private const string addStringToCategoryKeywordId = "AddStringToCategoryKeyword";
private readonly SimpleAction addStringToCategoryKeyword;
public TransactionAddStringToCategoryKeywordController()
{
addStringToCategoryKeyword = new SimpleAction(this, addStringToCategoryKeywordId, DevExpress.Persistent.Base.PredefinedCategory.Edit);
addStringToCategoryKeyword.ImageName = "Action_Export";
//addStringToCategoryKeyword.TargetObjectsCriteria = "Not IsNullOrEmpty([NameFull])";
addStringToCategoryKeyword.SelectionDependencyType = SelectionDependencyType.RequireMultipleObjects;
addStringToCategoryKeyword.Execute += TransactionAddStringToCategoryKeyword_Execute;
}
protected override void OnActivated()
{
base.OnActivated();
addStringToCategoryKeyword.Active["test"] = true;
}
}
}
And I have access to the GridView:
private void WinAlternatingRowsController_ViewControlsCreated(object sender, EventArgs e)
{
GridListEditor listEditor = ((ListView)View).Editor as GridListEditor;
if (listEditor != null)
{
DevExpress.XtraGrid.Views.Grid.GridView gridView = listEditor.GridView;
}
}

Passing Value on Property Change

I have 2 forms: Form A and Form B. I also have a property field class.
Form A contains the label I want changed when a property is changed. Form B contains code that will change the property field.
Property Class Code:
public class Controller
{
private static string _customerID;
public static string customerID
{
get { return _customerID; }
set
{
_customerID = value;
if (_customerID != "")
{
FormA.ChangeMe();
}
}
}
}
Form B Code:
private void something_Click(object sender, SomethingEventArgs e) {
Controller.customerID = "Cool";
}
Form A Code:
public static void ChangeMe()
{
var frmA = new FormA();
MessageBox.Show("Test: " + Controller.customerID); //This works! Shows Cool
frmA.lb2Change.Text = Controller.customerID; //This kind of works..
MessageBox.Show("Test2: " + frmA.lb2Change.Text); //This shows the correct value. Shows Cool
}
The property field value is passed (which I know from the MessageBox) however it does not update the value on the form label itself. Why is this? What am I doing wrong? I also believe there is a better alternative for achieving what ChangeMe() method is intended to achieve -- if so are there any suggestions?
You can do the following
To define a delegate
To Implement Property Change Notification
Delegate
public delegate void OnCustomerIDChanging(object sender,CancelEventArgs e);
public delegate void OnCustomerIDChanged(object sender,object value);
public class Controller
{
private static string _customerID;
public event OnCustomerIDChanging CustoerIDChanging;
public event OnCustomerIDChanged CustoerIDChanged;
public static string customerID
{
get { return _customerID; }
set
{
// make sure that the value has a `value` and different from `_customerID`
if(!string.IsNullOrEmpty(value) && _customerID!=value)
{
if(CustomerIDChanging!=null)
{
var state = new CancelEventArgs();
// raise the event before changing and your code might reject the changes maybe due to violation of validation rule or something else
CustomerIDChanging(this,state);
// check if the code was not cancelled by the event from the from A
if(!state.Cancel)
{
// change the value and raise the event Changed
_customerID = value;
if(CustomerIDChanged!=null)
CustomerIDChanged(this,value);
}
}
}
}
}
}
in your Form and when you are initiating the Controller Object
var controller = new Controller();
controller.CustomerIDChanging +=(sd,args) =>{
// here you can test if you want really to change the value or not
// in case you want to reject the changes you can apply
args.Cancel = true;
};
controller.CustomerIDChanged +=(sd,args) =>{
// here you implement the code **Changed already**
}
The above code will give you a great control over your code, also will make your controller code reusable and clean. Same
result you can get by implementing INotifyPropertyChanged interface
INotifyPropertyChanged
you might have a look on this article to get more information
In your static method ChangeMe you are creating a new Form every time, you want to Change the value. Instead of that you want to change the value of an existing form. Therefor your Controller needs an instance of this FormA. Try it like this:
public class Controller
{
//You can pass the form throught the constructor,
//create it in constructor, ...
private FormA frmA;
private string _customerID;
public string customerID
{
get { return _customerID; }
set
{
_customerID = value;
if (_customerID != "")
{
frmA.ChangeMe();
}
}
}
}
Now you donĀ“t need to be static in your FormA:
public void ChangeMe()
{
MessageBox.Show("Test: " + Controller.customerID);
this.lb2Change.Text = Controller.customerID;
}

how to make custom C# ComboBoxItem that inherits System.Windows.Forms.Control? if possible?

I need to create custom comboboxitem wich i can process among other controls that inherit System.Windows.Forms.Control class. So my comboboxitem must inherit System.Windows.Forms.Control because i use cast to that type and refer to Text property withn a loop.
There is some posts on how to create custom item but that one inherits Object class which is not working for me? I tried but it didnt work?Here is my try:
public class ComboItem : System.Windows.Forms.Control
{
public object Value { get; set; }
public override string Text { get; set; }
// public object Value { get; set; }
public ComboItem(string text) { this.Text = text; }
public ComboItem(string text, string value) { this.Text = text; this.Value = value; }
public override string ToString()
{
return Text;
}
}
nothing is displayed in combo box after following code
private void Form1_Load(object sender, EventArgs e)
{
ComboItem asd = new ComboItem("qweqwwqeq");
ComboItem asd2 = new ComboItem("2222222");
comboBox1.Items.Add(asd);
comboBox1.Items.Add(asd2);
comboBox1.SelectedIndex = 1;
}
this is context in which i need to use it:
System.Windows.Forms.Control ctrl = (System.Windows.Forms.Control)asd["Kontrola"];
ctrl.Text = (String)asd["Engleski"];
I assume you are not really talking about the ComboBoxItem Class, which is from WPF but simply of the Winforms Combobox which contains objects.
For some reason Controls don't get displayed in Collections unless they are actually placed in a Container Control.
So you probably have to wrap your Control in a minimal wrapper class like this:
class CtlWrapper
{
public Control theControl { get; private set; }
public CtlWrapper(string text)
{
theControl = new Control();
theControl.Text = text;
}
public CtlWrapper(Control control) { theControl = control; } // for fun
public override string ToString() { return theControl.Text; }
}
Not sure what you'll do with such a generic thing as Control in the ComboBox's Items list, but maybe you'll add some code to create different Control types..? With the wrapper the ToString text gets displayed as expected..
Of course you an add a Value string or whatever you need to the class as needed or you could use your own class as the type of 'theControl'..
Edit: For fun I have added a 2nd constructor to allow for adding existing Controls (of any type ;-)

How to create custom TextBox control?

I want to perform Trim() method on each TexBox control on my page, before value is returned. I dont' want to hard-code the same code for each TexBox control, I want to do it in more elegant way.
I've found made the following class
namespace System.Web.UI.WebControls
{
public partial class TrimmedTextBuox : TextBox
{
private string text;
public override string Text
{
get { return string.IsNullOrEmpty(text) ? text : text.Trim(); }
set { text = value; }
}
}
}
but it fails, while debuggind the compiler doesn't get inside get{} and set{}.
After that, I created a UserControl item, but it must be deriverd from System.Web.UI.UserControl, not System.Web.UI.WebControls.TextBox to get it work (there's an exception which points to that)
So, how can I do that ?
First you have to register your control in your .aspx page like that:
<%# Register TagPrefix="customControls" Namespace="WebApplication.Custom.Controls" Assembly="WebApplication"%>
Then you can call it using the markup
<customControls:TrimmedTextBuox ID="txtTrim" runat="server"/>
Plus you don't have to create another "text" property in your custom TextBox. Instead, it can be done like that:
namespace WebApplication.Custom.Controls
{
public class TrimmedTextBuox : TextBox
{
public override string Text
{
get
{
return base.Text;
}
set
{
if (!String.IsNullOrEmpty(value))
base.Text = value.Trim();
}
}
}
}
This will trim recursively all text boxes before inserting.
public static void trimRecursive(Control root)
{
foreach (Control control in root.Controls)
{
if (control is TextBox)
{
var textbox = control as TextBox;
textbox.Text = textbox.Text.Trim();
}
else
{
trimRecursive(control);
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
trimRecursive(Page);
}
Simple Solution to your problem is to hide the Text property of your base class by using new keyword. sample code...
public class TrimmedTextBox : TextBox
{
public new string Text
{
get
{
var t = (string) GetValue(TextProperty);
return t != null ? t.Trim() : string.Empty;
}
}
}
For more info about how new keyword with property works refrer to this SO Question

Trim all user input strings in ASP.NET Web Forms

I am looking for a way to trim all user input in ASP.NET without calling Trim() on every string instance. I came across extending the DefaultModelBinder for MVC. Is there a way to do this in web forms? What options are available? As a less desirable option, is there a way to incorporate this into the set method of a class?
You could create a custom TextBox which always returns a trimmed version of the text:
public class CustomTextBox : TextBox
{
public override string Text
{
get { return base.Text.Trim(); }
set { base.Text = value; }
}
}
Then just use this instead of the normal TextBox anywhere you need this behavior.
Here is the utility method to trim all TextBoxes in a page (or a parent control) recursively.
public static void TrimTextBoxesRecursive(Control root)
{
foreach (Control control in root.Controls)
{
if (control is TextBox)
{
var textbox = control as TextBox;
textbox.Text = textbox.Text.Trim();
}
else
{
TrimTextBoxesRecursive(control);
}
}
}
Usage
protected void Button1_Click(object sender, EventArgs e)
{
TrimTextBoxesRecursive(Page);
}
You have to call this extension method from the appropriate parent, e.g. Page.TrimTextControls
public static void TrimTextControls(this Control parent, bool TrimLeading)
{
foreach (TextBox txt in parent.GetAllControls().OfType<TextBox>())
{
if (TrimLeading)
{
txt.Text = txt.Text.Trim();
}
else
{
txt.Text = txt.Text.TrimEnd();
}
}
}

Categories