|
How to detect invalid character input in ListView control?
I want to detect invalid character input while editing a ListBox's item and show a tooltip with warning.
I know how to do it for TextBox control:
private void textBox1_TextChanged(object sender, EventArgs e)
{
ToolTip myTooltip = new ToolTip();
if (textBox1.Text.Contains("Invalid Character"))
{
myTooltip.Show("You entered an invalid character", textBox1);
}
}
How to make it for a ListBox control?
Thank you.
|
|
|
|
|
|
Thank you for the reply.
But I can't make use of this method as I need a real-time check for user input.
|
|
|
|
|
You can combine two events to achieve the goal.
First create a flag, like isEditing = false;
On BeforeLabelEdit Event change it to true, meanwhile on AfterLabelEdit Event change it to false.
Then on KeyDown Event check first if isEditing == true, then process the key.
May this be the solution?
EDIT:
On BeforeLabelEdit Event you can store the value and on AfterLabelEdit Event get the value and see if it is changed then rise an custom event? Can this be a better way?
|
|
|
|
|
I found plenty of exmaples of serializing a class to XML, but the resulting XML doesn't seem to have the same format as other XML data I see in the table.
Anyone have an example of the right way to do this?
Thanks
Everything makes sense in someone's mind
|
|
|
|
|
Would you like to elaborate on what you consider to be the "right" way? What are you trying to do with the serialized class?
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Store the data in SQL
Everything makes sense in someone's mind
|
|
|
|
|
Do you want to store it as XML or in a proper data structure.
If xml, do you need to search the data or can it be stored as simple text?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I'm trying to serialize a class to SQL, thn later deserialize it back to the class. No need to search
Everything makes sense in someone's mind
|
|
|
|
|
Then you can just store the result of the serialisation in a TEXT column.
|
|
|
|
|
Ok, so I think I have it almost figured out. I'm now trying to create generic Serialize and Deserialize methods:
private static string serialize<T>(T ObjToSerialize)
{
string retVal = string.Empty;
XmlSerializer serializer = new XmlSerializer(ObjToSerialize.GetType());
System.Text.StringBuilder sb = new System.Text.StringBuilder();
using (System.IO.StringWriter writer = new System.IO.StringWriter(sb))
{
serializer.Serialize(writer, ObjToSerialize);
}
retVal = sb.ToString();
return retVal;
}
private static T deserialize<T>(string XmlString) where T : new()
{
T type = new T();
object retVal;
XmlDocument doc = new XmlDocument();
doc.LoadXml(XmlString);
XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
XmlSerializer ser = new XmlSerializer(type.GetType());
object obj = ser.Deserialize(reader);
retVal = (T)obj;
return retVal;
}
The Deserialize isn't compiling. It's telling me that I can't cast T to an object. What's wrong here?
Everything makes sense in someone's mind
|
|
|
|
|
Kevin Marois wrote: It's telling me that I can't cast T to an object.
Why do you even need to do that? Can't you just do something like this?
T retVal = null;
XmlDocument doc = new XmlDocument();
doc.LoadXml(XmlString);
XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
XmlSerializer ser = new XmlSerializer(typeof(T));
retVal = (T)ser.Deserialize(reader);
return retVal;
Also, you can probably simplify it by just deserializing using a StringReader instead of loading an XmlDocument then deserializing from that
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Here is the VB.NET version I had laying around (should be fairly simple to convert to C#):
Public Shared Function ToXml(Of T)(ByVal item As T) As String
If item Is Nothing Then
Return String.Empty
End If
Dim serializer As New XmlSerializer(GetType(T), String.Empty)
Dim stream As New MemoryStream
Dim settings As New XmlWriterSettings
settings = New XmlWriterSettings
settings.OmitXmlDeclaration = True
settings.Indent = True
Dim writer As XmlWriter = XmlWriter.Create(stream, settings)
Dim ns As New XmlSerializerNamespaces
ns.Add(String.Empty, String.Empty)
serializer.Serialize(writer, item, ns)
stream.Position = 0
Dim xml As String = New StreamReader(stream).ReadToEnd
writer.Close()
stream.Close()
Return xml
End Function
Public Shared Function FromXml(Of T)(ByVal xml As String) As T
If String.IsNullOrEmpty(xml) Then
Return CType(Nothing, T)
End If
Dim serializer As New XmlSerializer(GetType(T))
Dim reader As New StringReader(xml)
Dim xmlReader As New XmlTextReader(reader)
Dim item As T = DirectCast(serializer.Deserialize(xmlReader), T)
reader.Close()
Return item
End Function
|
|
|
|
|
One reason for that of course could be that the source of the other XML does not use C# serialization for creation.
|
|
|
|
|
Hi world
i have to draw 2D forms (ellipse, lines, arcs...)in WPF using code.
i don't know from where i should start xD ..........
do you have an idea O_o !!! if yes tell me pleaaaaze, cause i have to finish my application this month.
thanks.
|
|
|
|
|
|
thank you that's very interesting.
|
|
|
|
|
And if you run into snags, there's a WPF message board[^] here
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi,
Iam using C#. WinForms 2010. In C# coding time while I press Ctrl+F then I can able to see
Find Window.
Similarly I have DataGridView and its DataSource is MyTable1. When the user presses Ctrl+F then I would like to locate on Particular Row,Column in DGV's DataSource column.
Is it possible? Or any other way for search...
Thanks For The Ideas.
|
|
|
|
|
|
Thanks
|
|
|
|
|
I made a custom UserControl and in it i put a Listbox.
In Form1, in the properties of the UserControl I want to see the [Items] property of the Listbox that is inside UserControl.
How to do that?
thanks.
|
|
|
|
|
Create the property yourself, and delegate the get/set methods to the property on the listbox.
Regards,
Rob Philpott.
|
|
|
|
|
If I was knowing how to make it, i did not cry for help.
give me an example please.
|
|
|
|
|
Ok something like (in your user control):
public ListBox.Collection Items
{
get { return listbox1.Items; }
set { listbox1.Items = value; }
}
... (edit) although you probably don't want the setter thinking about it..
Regards,
Rob Philpott.
|
|
|
|