|
void ValidateForXml(ref string value) ... I do not like the idea so much, though it is syntactically correct. That function is meant to encode some data, so I'd declare it accordingly, i.e. with a return type:
string EncodeForXml(string input) : now its purpose is clear.
By the way, I'd look for some library for that purpose instead of trying to invent my own library - are you sure can enumerate all the special cases you have to take care of when doing it your self?
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
Hi there,
DataGridView.CellValidatin works fine if all operations are done with keyboard. However, if the user has a mouse click on another cell, the focus goes to the new cell directly and the CellValidating event is never fired.
How to force cellValidating to fire (and keep the focus on the cell if the validation failed)? I googled a lot but could not find a solution to this problem.
Thanks in advance.
Mars Lam
-- modified 26-Feb-18 22:39pm.
|
|
|
|
|
public Form1()
{
InitializeComponent();
var d = new List<Tuple<string, int>>() {
new Tuple<string, int>("test1", 12),
new Tuple<string, int>("test2", 16),
new Tuple<string, int>("test3", 20)
};
dataGridView1.AutoGenerateColumns = true;
dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
dataGridView1.DataSource = d;
dataGridView1.CellValidating += dataGridView1_CellValidating;
}
void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
e.Cancel = false;
MessageBox.Show(string.Format("CellValidating {0}, {1}", e.RowIndex, e.ColumnIndex));
}
Works as expected, and raises a CellValidating event each time you click outside the cell (even on closing the application).
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi Eddy,
Thanks for the sample program, it works and helped me found the cause of my problem.
It was because I had the below line in .CellValidating event, for trouble shooting another problem long time ago.
if (this.ActiveControl.Equals(sender)) return;
After removing this line, the program works as expected.
Thank you very much for your help!
Mars Lam
|
|
|
|
|
You're welcome
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Usually "row validation" at "save time" is sufficient.
If you are not careful, you can get into a deadlock when doing cross-edits at the field / cell level.
("Moving focus" in "forms" when necessary is simpler than in a grid).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I am trying to read bar code from barcode scanner device in Razor View textbox. But when i press scan button it opens the Bookmarks and put the barcode there, while focus is on textbox.
Please Help,
Thanks in Advance.
|
|
|
|
|
Check your scanner hardware setup: most come set up to act as a keyboard, but it's possible to set them to also add lead in / lead out characters to let you detect barcode input and direct it to a specific field. I'd suspect that the lead in or lead out setup has been configured to a sequence which opens the bookmarks in your browser.
See your manufacturer documentation or website for details, they are all different.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
hi all,
i would like a loop for this.....
textBox10.Text = arr[0].ToString();
textBox11.Text = arr[1].ToString();
textBox12.Text = arr[2].ToString();
textBox13.Text = arr[3].ToString();
textBox14.Text = arr[4].ToString()
etc.....
is it possible?
thanx,
jim.
|
|
|
|
|
|
.... edit ...
Jim wrote:Member 13070736 wrote: that seems like way too much effort just to slot an array into textboxes. It is an effort If you clarify what you are doing further, there may be simpler ways: are you using a database here ? As I sai to Luc:Quote: The need for a collection of TextBox Controls functioning as a kind of logical unit for inputting/validating a set of data is very common. I prefer to implement this in a UserControl (or Panel) that implements validation, provides options for either sequential entry, random entry, etc. ... or, in a Form shown as a Dialog. ... end edit ...
Sure, it's possible: there are several different ways. A key factor in choosing how to implement this is whether the values in your list will change, or, remain fixed.
Assuming they change, this is one technique:
using System;
using System.Collections.Specialized;
using System.Windows.Forms;
using System.Collections.ObjectModel;
namespace YourNameSpace
{
public class MapIntsToTextBoxes : ObservableCollection<int>
{
public MapIntsToTextBoxes(params int[] ints)
{
foreach (int i in ints)
{
this.Add(i);
}
}
}
} Here's a usage example:
using System;
using System.Collections.Specialized;
using System.Windows.Forms;
using YourNameSpace;
namespace YourWinFormProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private List<TextBox> TBoxes = new List<TBoxes>{
textBox10,
textBox11,
textBox12,
textBox13,
textBox14
};
private MapIntsToTextBoxes mapper = new MapIntsToTextBoxes(1,2,3,4,5);
private void Form1_Load(object sender, System.EventArgs e)
{
mapper.CollectionChanged += MapperOnCollectionChanged;
mapper[3] = 1000;
}
private void MapperOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Replace:
TBoxes[e.OldStartingIndex].Text = e.NewItems[0].ToString();
break;
case NotifyCollectionChangedAction.Add:
break;
case NotifyCollectionChangedAction.Remove:
break;
case NotifyCollectionChangedAction.Move:
break;
case NotifyCollectionChangedAction.Reset:
break;
}
}
}
} There's a potential problem with this: if you expose an Observable Collection, remember that there's no way to cancel the other Actions, like Add, Remove. You can find some work-arounds for this, but, imho, they are complex.
And, there are other ways of achieving this including binding, but,in Win Forms, thee binding facility ... inferior to WPF, imho ... is something I avoid based on difficulties I've had with it: but, hey, it might work for you.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
modified 26-Feb-18 5:28am.
|
|
|
|
|
I have two comments:
1. Controls, just like all variables, deserve a descriptive name; if they are too many to come up with decent names, you're not on the right track.
2. When your user interface requires a large number of Controls you probably should rethink your design, there are bound to be better ways to handle things. Maybe what you need here is a DataGridView with a TextBox column i.e. a DataGridViewTextBoxColumn[^]. And obviously a DGV offers a Rows collection which you can easily enumerate.
modified 25-Feb-18 18:39pm.
|
|
|
|
|
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
thanx for your replies...
well that seems like way too much effort just to slot an array into textboxes.
cheers.
jim.
|
|
|
|
|
It's not about effort now - it's about effort later.
Using Visual Studio default names for everything is a very poor idea - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
And having all names be very, very similar is a recipe for unreliable software as well - it's far, far to easy to get it wrong and not notice. Until of course you have mucked up the DB so badly it's going to take days or weeks of human effort to sort it all out.
And as regards the number of controls, that's good advice as well - it reduces the "clutter" and makes your code more "generic" - it's not fixed to a particular set of textboxes so additions and upgrades become easier - and code changes all the time! Plan for changes and life becomes easier. "Throw it together" without planning and maintenance becomes more difficult and fraught with potential bugs.
Take time to get it right: you won't get time to come back and do it properly later, so just like SQL Injection prone code it will bite you in the ass at a future date when you have forgotten most of how the software works.
It really does save you time and effort in the long run, as well as producing better code.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi Jim, Member 13070736 wrote: that seems like way too much effort just to slot an array into textboxes. I've added a paragraph to my original response as a comment on this statement.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
|
|
|
|
|
Hi, Luc, it's a pleasure to see you (someone I regard as a true mentor) posting, again !
On a general (pedagogic) level, I agree with your advocacy of strong, descriptive, names; however, in the case of a question like this one, how do you know the OP is not just prototyping a solution, and, when implementing it, will use strong naming ?
Luc Pattyn wrote: When your user interface requires a large number of Controls you probably should rethink your design There are circumstances that require a large number of data-entry Controls to have their values set by the user: the issue is not "wrong design," but how to control the UI so the user is not overwhelmed ... by organizing the input tasks into groups, presenting each group in a separate UI, etc. ... this can be as simple as using a TabControl.
Re: use of DataGridView: we don't know if a DataBase is involved, and, imho, the DataGridView is one ugly monster of an antique Control. FlowLayoutTable, and TableLayout panel are other optional uber-containers.
The need for a collection of TextBox Controls functioning as a kind of logical unit for inputting/validating a set of data is very common. I prefer to implement this in a UserControl (or Panel) that implements validation, provides options for either sequential entry, random entry, etc. ... or, in a Form shown as a Dialog.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12
modified 26-Feb-18 5:15am.
|
|
|
|
|
Hi Bill,
I never stopped visiting CodeProject, I'm here a few times a week reading an article and/or looking at the C# forum. My contributions are few, I only post when a thread remains unanswered, or something I consider important is missing in the replies.
We all take the given information, which unfortunately often is sparse and without much context, and we have been told not to look at the poster's screen, access his HDD, or read his mind. So we need to work with what is made available while assuming nothing much. However I tend to assume code shown is representative for actual code.
Of course having several TextBoxes isn't automatically wrong, or bad design; it may even be the right choice. Showing code for five, plus ellipsis, made me produce a warning. And then the poster's question left me wondering whether the Form pertained to one composite object, or to a collection of simple data items, hence a number of shoulds, probablies, and maybes. Due to the array I tended to the collection-of-items situation, and hence suggested a DGV, which is a bulky Control I first used in CP Vanity[^] if my memory serves me well. For the single-object situation, maybe I should have suggested a PropertyGrid.
Cheers
|
|
|
|
|
Luc Pattyn wrote: Of course having several TextBoxes isn't automatically wrong, or bad design; it may even be the right choice. Showing code for five, plus ellipsis, made me produce a warning.
Particularly when they start with "Textbox10" and go up from there...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
How about the following untested code:
TextBox tbs[] = { textBox10, textBox11, ..., textBox99 };
Object arr[tbs.Length];
for (int i = 0; i < tbs.Length; i++)
tbs[i].Text = arr[i].ToString();
or
TextBox tbs[] = { textBox10, textBox11, ..., textBox99 };
Object arr[tbs.Length];
int i = 0;
foreach (TextBox tb in tbs)
tb.Text = arr[i++].ToString();
|
|
|
|
|
hello..
I'm developing a software? for my thesis using C# winform application.. it is an offline system.. and we are not allowed to make a web application..
my goal is to show pdf files from MySQL database without using Adobe Acrobat reader or any pdf viewer that has a printing tool because I want to disable the printing process.. please help me.. Is it possible to show pdf file in a panel? if so, how?.. or even in a picturebox?..
I want to disable the printing process because I want to control my printing process with a coin acceptor and an ARDUINO..
thank you..I hope you could help me..
Shiela Amante
|
|
|
|
|
Is it possible? Sure. You just have to write the massive pile of code that interprets and renders the PDF.
USE AN EXISTING LIBRARY! Turning in an application for your thesis that isn't complete because you're wasting a couple of YEARS writing your own PDF interpreter and rendering engine is just f'ing STUPID!
|
|
|
|
|
To "show" a pdf (under control of an app) one typically uses the "print preview" option (of a given pdf printer driver) in the app via the "print / printer dialog".
(PdfSharp can generate pdf's / previews from raw content if you want to get fancy).
The "app" controls the printing in this case; not the user.
If your pdf source is "hidden", you have effectively taken control of the "printing process".
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
|
There is no library that forces you to implement a printing-button. You're not ready for a thesis.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|