How to Bind TextBox from Multiple Other Controls - c#

What I Have
I have a form with 3 UserControls that contain source fields to be used to calculate another field in a target UserControl.
UserControl1 (uc1)
StartDate DatePicker (dpStart)
EndDate DatePicker (dpEnd)
UserControl2 (uc2)
Rate TextBox (txtRate)
UserControl3 (uc3)
Amount TextBox (txtAmount)
UserControl 4 (uc4)
CalculatedValue TextBox (txtCalc)
Formula to calculate txtCalc is below
txtCalc.Text = (dpEnd.Value - dpStart.Value).TotalDays * txtRate.Text * txtAmount.Text
What I Want To Achieve
Whenever I change a value in any of the first 3 UserControls, the Text in txtCalc should update with calculated value.
What I Have Tried
I have tried DataBindings but it appears to be only for a single field.
e.g.
uc4.txtAmount.DataBindings.Add("Text", uc2.txtRate, "Text", true, DataSourceUpdateMode.Never);
Would really appreciate your inputs.

There are a few things you may want to consider to bind the date pickers and the text boxes as you want. In addition there are a couple of ways you can do this.
One possible problem is that each of the UI “controls” (date pickers and text boxes) are spread out across four (4) different UserControls. This is not a big problem and about the only thing you need to make sure is that each of the “controls” (DTP and TextBoxes) in each of the UserControls is “exposed” to the Main Form.
In other words, if you simply place a TextBox in a UserControl and then place that UserControl onto the Main Form… then the Main Form will NOT be able to access that TextBox … UNLESS it is “exposed.” This can be remedied by simply setting the TextBoxes Modifier property to Public. Then the Main Form WILL have access to the text box in the UserControl.
Therefore, step one is make sure each UI “control” in each of the UserControls that we want to access in the Main Form has its Modifier property set to Public. The reason for this, is that you want to be able to access the data in those controls from the Main Form. In addition to the fact that we want to subscribe to those controls “Events” FROM the Main Form.
What we will do inside the Main Form after the UserControl is placed onto the form is subscribe to the UI Control’s (DTP or TextBox) TextChanged event. Then we can “capture” when one of the controls text changes… and we can capture this change INSIDE the Main Form.
Another possible issue is how the code is calculating the “total days” amount from the two DateTimePickers. The current code looks something like…
(dpEnd.Value - dpStart.Value).TotalDays
This will work, however, if you debug the code and look closely at the result of the calculation you may note that if date 1 is 1/25/2021 and date 2 is 1/26/2021… then you apply the code above… there is a good chance that you may get a result like 0.933 … which will obviously become a 0 when converted to an int and this is not what we would expect. The reason for this is because when you add or subtract two DateTime objects… the calculation is INCLUDING the Time portion of the DateTime object.
Therefore, to get the correct int value… you need to “qualify” that you only want the “Date” difference between the two dates. Fortunately the DateTime object has a Date property that we can use to ignore the Time portion. Therefore only a small change is needed to fix this and may look something like…
(dpEnd.Value.Date - dpStart.Value.Date).TotalDays
As suggested in the comments, using Events is probably the easiest to implement and it is not difficult to understand. Basically, we would subscribe (wire-up) each of the controls in each of the UserControls to the SAME event. Inside that event we would “update” the calculated value.
Typically, you would wire up each control to its own event, however, since you want to simply “update” a single text box when ANY of the control’s changes we can simplify this and create a single method to “update” the calculated text box when ANY of the other controls changes. I hope that makes sense.
To help, and I highly recommend you also (in the near future) do the same… is properly NAME your variables. Naming the controls uc1, uc2, uc3 and uc4 is well … not a good idea. You can do it, but it makes it difficult to tell “what” the control is. Looking at the names… without more research, I have no idea “which” control has the “Rate” text box. Name your variables to something meaningful to avoid any ambiguity. In the example below, for the UserControls I named them like… UC_StartEndDate, UC_Rate etc…
Another possible issue is that since you are wanting to perform a “calculation,” you will need to parse the string values in the TextBoxes to int values. In other words… the code…
txtRate.Text * txtAmount.Text
May well work without an error, however I am confident it will not give you the result you want since both sides of the “*” multiplier are “TEXT/string” values and we will need to parse those string values to int values to do any mathematical calculations.
NOTE the int.TryParse(UC_Rate.txtRate.Text, out int rate); line of code would typically be wrapped in an if statement since it will return true if the parse succeeded and false if it fails. If the parse fails, then the out variable rate will be set to zero (0) and that is ok with me… if it fails then use a zero (0) as the value. You may want to do something different.
private void UpdateUC_Total() {
int tdays = (int)(UC_StartEndDate.dpEnd.Value.Date - UC_StartEndDate.dpStart.Value.Date).TotalDays;
int.TryParse(UC_Rate.txtRate.Text, out int rate);
int.TryParse(UC_Amount.txtAmount.Text, out int amt);
int total = tdays * rate * amt;
UC_Calculated.txtCalc.Text = total.ToString();
}
Now all we have to do is subscribe (wire-up) to the individual UI controls TextChanged event. As already mentioned, since we want ALL the controls to do the same calculation, this simplifies things and we can have ALL the controls subtribe to the SAME event. This one event would simply call our method above and may look something like…
private void UC_ValueChanged(object sender, EventArgs e) {
UpdateUC_Total();
}
In the forms Load event, we could subscribe the controls in each of the UserControls to our event above and it may look something like…
private void Form1_Load(object sender, EventArgs e) {
UC_StartEndDate.dpStart.TextChanged += UC_ValueChanged;
UC_StartEndDate.dpEnd.TextChanged += UC_ValueChanged;
UC_Rate.txtRate.TextChanged += UC_ValueChanged;
UC_Amount.txtAmount.TextChanged += UC_ValueChanged;
}
That is pretty much it. If any of the date pickers or rate or amount text boxes change, then the calculated text box text will “update” automatically. You may need to “leave” the control to see the updated value.
Another approach is to use each control’s DataBindings property to “Bind” each control to something. Currently you have the code…
uc4.txtAmount.DataBindings.Add("Text", uc2.txtRate, "Text", true, DataSourceUpdateMode.Never);
The problem here is that the data bindings is looking for a DataSource of some type like a DataTable or a List<T> or in the example below a Class object. The code “appears” to be using uc2.txtRate as a DataSource and this probably will not work as txtRate is a TextBox and is not necessarily a DataSource.
This can get tricky, and to keep things simple, I will often simply “create” a DataSource to make it easier to set the controls DataBindings to work as I want. The code below shows how you could do this “DataBinding” using your current example. Bear in mind this will work, however, it may create a little more work for you.
So step 1 is to “create” a simple DataSource we can use for ALL the controls (DTP and TextBoxes). In this case I will implement a single Class with the properties we need. Then, we would instantiate only ONE of these objects and then use that object as a DataSource when setting each controls DataBindings property. This UC_Helper class may look something like…
public class UC_Helper {
public DateTime DP_Start { get; set; }
public DateTime DP_End { get; set; }
public string Rate { get; set; }
public string Amount { get; set; }
public int CalcAmount {
get {
int tdays = (int)(DP_End.Date - DP_Start.Date).TotalDays;
int.TryParse(Rate, out int rate);
int.TryParse(Amount, out int amt);
return tdays * rate * amt;
}
}
}
We will be able to instantiate a single UC_Helper object with the values from our user control “controls” and then simply set each control’s DataBinding property to “point” to the proper property in the instantiated UC_Helper object.
Therefore in the form load event, you would instantiate a new UC_Helper object and then “bind” each of the controls in each of the UserControls to one of the properties in our UC_Helper object. This code in the forms Load event may look something like…
UC_Helper ControlHelper;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
ControlHelper = new UC_Helper {
DP_Start = UC_StartEndDate.dpStart.Value.Date,
DP_End = UC_StartEndDate.dpEnd.Value.Date,
Rate = UC_Rate.txtRate.Text,
Amount = UC_Amount.txtAmount.Text
};
UC_Amount.txtAmount.DataBindings.Add("Text", ControlHelper, "Amount");
UC_Rate.txtRate.DataBindings.Add("Text", ControlHelper, "Rate");
UC_Calculated.txtCalc.DataBindings.Add("Text", ControlHelper, "CalcAmount");
UC_StartEndDate.dpStart.DataBindings.Add("Text", ControlHelper, "DP_Start");
UC_StartEndDate.dpEnd.DataBindings.Add("Text", ControlHelper, "DP_End");
}
Sorry for the long post. I hope this helps and I suggest you pick you own poison as to which approach to use. I tend to favor the event approach, however if there is a good DataSource available I may go with the data binding. Good Luck.

Related

How can I continue adding an integer with a free response textbox?

I am working with c#.
My goal is to be able to add an integer to a free response textbox. Allow me to explain.
I have a textbox called (Red,Green,Blue). Each color will be replaced using the Replace(); method.
Like this:
private void RedTextBox_TextChanged(object sender, EventArgs e) //ActualFogColor is Result
{
string ActualColorFog = "game.Lighting.FogColor = Color3.new(Red,Green,Blue)";
string Red = RedTextBox.Text;
string FogColorRed = ActualColorFog.Replace("Red", Red);
ActualColorFogScript.Text = FogColorRed;
}
This repeats for all other colors and it works fine. The problem I have is that I have a brightness button that when you click it, it adds 1 to the inputted number, but of course I had to convert it into an integer. It is basically initial + 1 = the new color. Replace Initial with new color and print that on the textbox.
Unfortunately i can't do
public partial class main : Form
{
int red = Convert.ToInt32(RedTextbox.Text); }
This is at the very top of the code which is why when doing this, it doesn't recognize RedTextBox.
The reason I am trying to assign the integer to the textbox is so that when the " RedTextBox.Text = '5' " It will take that 5 and add 1(by a button) which then prints the sum which I set equal to Red in string ActualColorFog = "game.Lighting.FogColor = Color3.new(Red,Green,Blue)";
I hope this make sense to you all, if you are confused on my plan, please leave your question.
I'd suggest you take one step back and think about the usability of your inputs. What I mean if you allow the user to freely enter the text, you have to tackle the input values like negative numbers, literals, numbers outside of the range (0-255 I presume), non-integer input, etc. Furthermore it would be nice if the user can use mouse scroll to increase/decrease the value. Perhaps a numeric up-down would be solution for your problem? It's not a standard control but there are enough implementations of it freely available.
Should you insist on using the text input, do the following:
define 3 integer member variables in your code for the components.
provide the Changed event handler for each textbox where you int.TyParse the input, check the ranges and if all goes well and update the respective member variable if it differs from the new value
probably add mouse scroll event handler
your button's Click event handler will update the member variables and the text values in the textboxes
A nicer solution would be to use the dependency properties instead of member variables, the content of the textboxes is bound to the dependency variables one way and the textboxes' Checked event handler does the parsing.
I am not getting into that if your method is good or not but just how to solve this problem.
As i can see, ideal for you would be that you can assign int red = Convert.ToInt32(RedTextbox.Text); and you cannot because it is at top of your code?
It is not recognizing because that part of code is accessed before RedTextBox is even initialized. Solution for this is to put int red = -1; and then
public main() //Your form constructor
{
InitializeComponents();
// I guess you somehow add some value to RedTextbox on form
showing so it need to be before assigning red variable
red = Convert.ToInt32(RedTextbox.Text); //It is important to be after Initializecomponents()
}

Recreate a group of Controls

Let's say that I have a panel with like... 3 controls in it. I may end up adding more controls to it or changing the positioning within that panel. When the program starts, I will programmatically HIDE the control. Eventually, the user can click a button that will create a duplicate of the original panel to populate an area on the form. The button should have the option for another click eventually, meaning that multiple instances of these can come about to populate this area. Remember that these controls may have text labels within them that can be individually set or altered later on, programmatically. I am assuming that to do this program, I need to make a List of controls, maybe a List of panels? I'm not exactly sure how to do this considering the fact that I need multiple controls duplicated multiple times.
Is there a nice, simple way to do this? I really don't want to do the duplication with any kind of 3rd-party package.
You will have to do it in code and therefore it'll be as nice as you can code ;-)
Seriously the course most often taken is to
create a UserControl which is a class related to a form, with the layout you want..
..and add more and more instances of it..
..often to a FlowLayoutPanel, often with AutoScroll
This is pretty nice and simple imo.
Here is a short walk-though..:
first we start, as usual, by picking a nice name for the UserObject class, maybe 'DataPanel' or 'BookItem'..
Next we create it: Go to the project explorer and right-click, choosing Add-New UserControl and give it the class name you chose. I'll use 'BookItem'.
Now you can see the Designer showing you a small empty control.
Look closer: You can also see that in the project explorer ther is now not only the new 'BookItem.cs' file but also the complementary 'BookItem.Designer.cs' and even a 'BookItem.resx' file; so this works very much like creating a new Form..
Let's add a few controls from the toolbox, I chose to add a PictureBox, four Labels and a NumericUpDown.
Have a look at the BookItem.Designer.cs file: Here you can see the very things you see in a Form.Desginer.cs file: All settings and all declarations for all controls you add to the layout. Note especially the declarations (at the bottom of the file): Just like for a Form, all controls by default are declared as private!
We can now work on the layout and script the controls. We also can add functions and properties to the UC, just like a Form.
Please note: Anything you need to access from outside, read from your form or its methods must be public! So if you want to access the NUpDown, let call it 'nud_quantity' you have a choice
You can change its declaration in the BookItem.Designer.cs from private to public or in the Designer by changing the Modifiers property
Or you can write a public function in the UC to get/set its value
Chosing between those two ways is a matter of taste; if other developers will work with the UC class, it will probably be better to put close control over what you expose by writing access methods.
After you have compiled the project you can see the new UC in the Toolbox.
You can now either add it from the Toolbox or
you can add it in code like any control you create dynamically.
Let's look at an example:
Imagine a simple order system in a bookstore: The customer has done a search on the books in our store and is presented with a list of books in a DataGridView 'dgv_bookList', readonly, multiselect. To the right there is a FlowLayoutPanel 'flp_cart' represeting a shopping cart. And we have a command button 'cb_addItems' to add selected books to the cart.
The Button might be scripted like this:
private void cb_addItems_Click(object sender, EventArgs e)
{
if (dgv_bookList.SelectedRows.Count <= 0) return;
foreach (DataGridViewRow row in dgv_bookList.SelectedRows)
{
BookItem book = new BookItem (row);
book.label1.Text = "#00" + book.label1.Text;
book.Name = book.label1.Text;
flp_cart.Controls.Add(book);
}
}
This will add one BookItem for each selected row in the DGV.
A few things to note on the above code:
I pass a DataGridViewRow into the constructor of the UC so it can directly set its labels! This means that, in addition to the parameterless contructor the desginer has built for us, we need to write a second contructor, maybe like this:
public bookItem()
{
InitializeComponent();
}
public bookItem(DataGridViewRow bookData)
{
InitializeComponent();
label1.Text = bookData.Cells[0].FormattedValue.ToString();
label2.Text = bookData.Cells[1].FormattedValue.ToString();
label3.Text = bookData.Cells[2].FormattedValue.ToString();
label4.Text = bookData.Cells[3].FormattedValue.ToString();
}
Instead you could write a public setData(DataGridViewRow bookData) function.
Also note how stupid my labels are named! You can do better than that, I hope!
Also note how I access 'label1' and modify its Text from a Button in the Form; to do that I had to change its declaration in the Desginer.cs file:
private System.Windows.Forms.PictureBox pb_cover;
public System.Windows.Forms.Label label1; // <<----expose this label !
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.NumericUpDown numericUpDown1;
Often preferrable: An access function, maybe like this:
public int quantity() { return (int) numericUpDown1.Value; }
Or, of course a Property:
public int quantity { get { return (int)numericUpDown1.Value; } }
Also note, that I set the Name of the BookData item to some variant of the 1st data item, my book id. This might as well, or better, happen in the constructor; and there should be a check to prevent adding the same item twice..
All in all one can say, that using UserControls is very much like working with Forms, including all the usual ways or tricks for inter-form communication: keep references, expose members, create properties and functions..
One final Note: Like with forms or subclassed controls there is one catch: By placing them in the designer, you assign the designer the responsiblity to display your UC during design time.
This is normally just fine; however it is also possible to introduce subtle mistakes which make it impossible for the designer to display the control. You need to correct these problems before the designer will be able to show a control or any form that contains it. Let have a look at a simple example of such a problem:
Let's script the Paint event of the PictureBox 'pb_cover' in the UC:
public Brush myBrush = null;
private void pb_cover_Paint(object sender, PaintEventArgs e)
{
if (pb_cover.Image == null)
{
Size s = pb_cover.ClientSize;
e.Graphics.FillRectangle(myBrush, 0, 0, s.Width, s.Height);
e.Graphics.DrawLine(Pens.Red, 0, 0, s.Width, s.Height);
e.Graphics.DrawLine(Pens.Red, s.Height, 0, 0, s.Width);
}
}
And let's modify the code in the Add button:
BookItem book = new BookItem (row);
book.label1.Text = "#00" + book.label1.Text;
book.myBrush = Brushes.OliveDrab;
flp_cart.Controls.Add(book);
Now, if you run the program all will be fine. Even if you try to look at the UC in the designer there may or may not be problems. But once you try to open a Form on which the UC was placed, the Desginer will crash and tell you that it can't work, since the Brush is null. Here the remedy is simple: add a default value to the Brush declaration and all is well. Other situations may need a little more thinking..
I don't even run into the problem btw, since I have not placed an instance of BookItem on the Form; they are only created in the Add Button..
I hope that gets you started!

Add and read Multiple textboxes

What is the best way to create and read multiple textboxes in a windows form? In my application, I have a windows form where customer can enter multiple addresses, email addresses
Right now I have a form like this,
TextBoxAddress1 TextBoxEmail1
TextBoxAddress2 TextBoxEmail2
.....
.....
.....
TextBoxAddressN TextBoxEmailN
For this I dragged and dropped multiple controls on a form and named each one of them.
If I use this method I had to write lengthy code to see if first row (TextBoxAddress1 TextBoxEmail1) is filled for validation and even for reading I had to write many lines of code.
Is there a better to way achieve this?
You can use the following code to add a TextBox dynamically to your form:
private int m_CurrTexboxYPos = 10;
private List<TextBox> m_TextBoxList = new List<TextBox>();
private void CreateCheckBox()
{
m_CurrTexboxYPos += 25;
TextBox textbox = new TextBox();
textbox.Location = new System.Drawing.Point(0, m_CurrTexboxYPos);
textbox.Size = new System.Drawing.Size(100,20);
Controls.Add(textbox);
m_TextBoxList.Add(textbox);
}
I would have a listbox/listview with your emails and Add/Edit/Delete buttons which show a popup form - the logic for validating emails, etc. would then be in the one place and your list can grow without you ever needing to add controls to the form.
You could dynamically create textboxes - but you end up writing code to make sure they layout nicely on the form, etc. - having some type of list is easier IMO and also lends itself to binding (e.g. to an email object)
Dynamically adding controls is pretty simple, provided you can use DockStyle and an exclusive container for them (e.g. a Panel). If you can't use DockStyle, then you need to write logic to determine Location and Size (which isn't fun).
On a simple form, I have two buttons and a panel, Button1 adds a new TextBox to Panel1, Button2 iterates through the controls in Panel1 and then checks that they are the correct type or throws an exception. This is where you you would put validation or reading logic. Panel1 needs to have AutoScroll = true; otherwise you will run controls off of the viewable screen.
This concept can be switched for anything that inherits from UserControl (all .Net native controls or your own custom controls).
private void button1_Click(object sender, EventArgs e)
{
TextBox NewEmailBox = new TextBox();
NewEmailBox.Name = "NewEmailBox" + this.panel1.Controls.Count;
NewEmailBox.Dock = DockStyle.Top;
this.panel1.Controls.Add(NewEmailBox);
}
private void button2_Click(object sender, EventArgs e)
{
foreach (Control item in this.panel1.Controls)
{
if (item is TextBox)
{
//Do your reading/validating here.
}
else
{
throw new InvalidCastException(string.Format("{0} was in Panel1 and is of type {1} not TextBox!", item.Name, item.GetType()));
}
}
}
Write a user control for each of the groupings you need. at least one One for address, one for email etc. then all of your validation, calls to your database access is contained in a single location
That is just good design. this way if you have multiple tabs for things like Home Information, Work Information, Emergency Contact Information, you can just place them on the form. This is pretty common for a user profile.
Then a listview for each grouping on a user profile page or whatever, that has edit/delete/add then popup a dialog with the appropriate user control in it.
Most simply, ListBox adove TextBox with Button.
Also you can use DataGridView, BuiltIn functionality for Add\Edit\Delete.
Here using DataGridView (ShowHeader set to false, EditMode to On Enter, with one Column with AutoSizeMode in Fill property)
The less of repeatable code you have, the better programmer you are.
Whenever you see a pattern (something what is repeatable), you could and you should try to optimize it. Unless it's something too small to worry.
In your case, determine first what is the basic of repeatable thing. Do you always have to enter address and email address? Then combine them into a control, which can carry out validation. Do you have to use this control often (or repeat N times)? Then maybe it make sense to switch to a list instead (ListBox, ListView or DataGridView).
Are you too lazy to bother configuring things? Then just optimize something what is obviously going to repeat: put validation into common method and call it from each TextBox event. Or make own TextBox with method build-in. Or do validation at once in the Ok button event by using loop.. or not by using loop.
To find best method you have to first decide best for who. Because customer want something shiny,easy to use, animated, with cats and boobs.. ok, without cats and boobs. The point is: how much work are you willing to put to have it best for the customer.
If I would have to enter table data (or data which form table), I'd go with DataGridView so it would looks like this.. or better:

Best way to handle controls automatically updating each other?

I have a question regarding updating Controls in .Net in such a way so that if the user updates one field, the other field will be updated with some data automatically and vice versa. I am using two NumericUpDown controls which convert some data.
The problem I am having is that, I am using the ValueChanged event. Because of this, sometimes these controls get stuck in a loop where one controls updates the other and the other tries to update the first one. The result is somewhat random.
So, what would be the best approach to handle this situation? In short, I only want to update the other control, if the first one was modified by a user himself.
Any help would be greatly appreciated.
Just use a boolean guard in the class to check whether you are inside an update method.
While you are updating, all future events fired from the NUDs will be ignored.
private boolean updating = false; // Class level variable
void event_handler(...) // The function hooked up to the ValueChanged event
{
if( !updating )
{
updating = true;
// Do your calculations and update the NUDs
updating = false;
}
}
I would recommend that you use data binding and bind to an object which serves as your model. Your model then is where the logic goes that alters the other value based on changing of a property. The model also raises IPropertyChanged / IPropertyChanging events, which the UI will pick up on. Not only will this prevent the issue you describe, it also keeps this business logic outside of your UI layer should you move to something else (say from WinForms to WPF, or Asp.Net MVC).
If method Foo handles the event of one control and method Bar handles the event for the other, then Foo should change the values of Bar's control and vice-versa. But you should use a control variable somewhere (say, a reference to the control that fired the event is a good idea). So that if Foo is called:
Foo updates the value of Bar's control;
Bar's control fires its event, and Bar is called;
Bar checks the reference for the control that shot first, sees that it's not its control, and does nothing.
Same logic applies to Bar.
that way you don't get an infinite loop.
In code, it'd, look like this:
nud1.ValueChanged += new Eventhandler(Foo);
nud2.ValueChanged += new Eventhandler(Bar);
NumericUpDown shooter = null;
private void Foo (object sender, EventArgs e)
{
if (this.shooter == null)
{
this.shooter = nud1;
nud2.Value = nud1.Value;
}
else this.shooter = null;
}
private void Bar (object sender, EventArgs e)
{
if (this.shooter == null)
{
this.shooter = nud2;
nud1.Value = nud2.Value;
}
else this.shooter = null;
}
Of course, this is a crude example (for example, it assumes the value of both controls is always changing. Adapt to your situation.
I like Andy's response about going with an MVC pattern, but if that's too radical of a change for this specific situation, you should set the values only if the current value is different than the value being assigned. That would prevent the ValueChanged event from firing again and stop the infinite loop the first time recursion happens.
// Inside your value changed handler for Control2,
// instead of directly setting the value of Control1, do this:
if(Control1.Value != valueBeingSet)
{
Control1.Value = valueBeingSet;
}

Apply Input Mask for a ToolStripComboBox C#

I was just wondering if anyone has come across how to apply an input mask for a Tool Strip Combo Box in C#?
My drop down box gets populated over time with IP addresses, and I would like to restrict what the users are able to write in (Ex: can only input 3 '.'s, only numbers, etc).
So if anyone is able to help out I would really appreciate it!
Thanks in advance.
Edit
My design has changed so I now need to have a ToolStripComboBox
You could try catching the KeyUp event, then check that the input is valid. If not revert it to the last valid input. You would probably want to do something similar with the Validating event (make sure CausesValidation is true).
Another option would be to create a MaskedTextBox and place it so it covers the text box portion of the drop down menu. You would then need to wire up the events so the two form controls remained synced.
You could also look into the ErrorProvider class.
There are a couple of other ways (like a timer which runs ever .3 seconds), but those are usually performance hogs or difficult to maintain.
Update for regular expression comment:
If I was to do this I might use a regular expression or I might manually parse the string.
Either way the KeyUp and Validating events is where I would check the validation of the control. The KeyUp event gives me the option to check as they type while the Validating event allows me to validate when the control loses focus. Which you use will depend on what you want the user experience to be.
If you do not use the KeyUp event to validate, you could add a timer which runs 5 seconds after the last key press. This way the control would not have to lose focus for the error to show.
Update for edited question and comment:
You could not use Format event as your question was on how to format user input, not how things are added to the list. As such that solution does not work with ToolStripComboBox or with ComboBox.
After reading the documentation for ToolStripControlHost, you might be able to cast ToolStripComboBox to ComboBox. If not then you could use the ToolStripControlHost to place the ComboBox onto your form. - This is incorrect or unnecessary, please see update below the quote.
ToolStripControlHost is the abstract base class for ToolStripComboBox, ToolStripTextBox, and ToolStripProgressBar. ToolStripControlHost can host other controls, including custom controls, in two ways:
Construct a ToolStripControlHost with a class that derives from Control. To fully access the hosted control and properties, you must cast the Control property back to the actual class it represents.
Extend ToolStripControlHost, and in the inherited class's default constructor, call the base class constructor passing a class that derives from Control. This option lets you wrap common control methods and properties for easy access in a ToolStrip.
Use the ToolStripControlHost class to host your customized controls or any other Windows Forms control.
To customize a ToolStripItem, derive from ToolStripControlHost and create a custom implementation. You can override methods such as OnSubscribeControlEvents to handle events raised by the hosted controls, and you can put custom functionality into properties to enhance the hosted control.
Update:
According to the ToolStripComboBox documentation you can access the underlying ComboBox through ToolStripComboBox's ComboBox property.
This is why I usually read the documentation on a control before I use it. I might not understand it, but at least I will have an idea what to look for. :)
You should create Format event like this:
private void comboBox1_Format(object sender, ListControlConvertEventArgs e)
{
e.Value = GetFullIpFormat((string)e.Value);
}
And here is code for formating values:
string GetFullIpFormat(string value)
{
string[] ip = new string[4];
for (int i = 0; i < ip.Length; i++)
{
ip[i] = GetIpPart(i, value);
}
return string.Format("{0:###}.{1:###}.{2:###}.{3:###}", ip[0], ip[1], ip[2], ip[3]);
}
string GetIpPart(int partNumber, string ip)
{
string result = "000";
int iLen = 3;
ip = ip.Replace(".", "");
int iStart = partNumber * iLen;
if (ip.Length > iStart)
{
result = ip.Substring(iStart);
if (result.Length > iLen)
{
result = result.Substring(0, iLen);
}
}
return result;
}
This will do formating for you.
Alternativly you can check input on same event for numbers.
This will do the job for you, happy coding! :)

Categories

Resources