I wish I knew it before, it would save me few hours (don't ask me why).
Apparently it is possible to make variable number of parameters in method using keyword params.
There are obvious limitations, it can be only the last parameters of the same time. But nevertheless, I think it's cool.
There is positive result for me not knowing that. Now I finally figured out how to work with regular expression, see Regex class. This is really powerful tool to have too.
Discovering .NET
Here I will post problems I and my colleagues met and solutions we found.
Friday, October 17, 2008
Variable number of parameters in C#
Friday, September 19, 2008
ComboBox - DisplayMemberPath or TextSearch.TextPath
Probably it's just too late already, and I am slow, but I was confused with DisplayMemberPath and TextSearch.TextPath properties.
First, I added some objects to ComobBox.Items. Not using XML, I did it dynamically from C#. It worked find on Vista computer, but then it didn't work under XP. I just got empty values instead of value of ToString() method.
OK, I looked in my favorite book WPF Unleashed, and found that TextSearch.TextPath should be used. Well, it didn't help. It took me a while until I got to try old familiar DisplayMemberPath property and then it worked.
So, the question I have now is why TextSearch.TextPath didn't work? Or if asked in more general way, what are the rules. When I am supposed to use DisplayMemberPath and when TextSearch.TextPath. Anybody?
Wednesday, September 10, 2008
Hyperlinks in WPF Continued
I wrote before about using hyperlinks in WPF. For my new side project I need to switch from hypertext to the simple text depending on availability of URL. Switching to simple text seemed easy, just assigning text value to the Text property worked. Switching back was not obvious for me.
Then I found that TextBlock has Inlines property. The code became this:
if (!string.IsNullOrEmpty(uri))
{
if (!string.IsNullOrEmpty(value))
tHypertext.Inlines.Add(value);
tHypertext.NavigateUri = new Uri(uri);
tTextBlock.Inlines.Clear();
tTextBlock.Inlines.Add(tWord);
}
else
{
tTextBlock.Text = value;
tHypertext.NavigateUri = null;
}
Where TextBlock and Hypertext are from previouse example
Thursday, September 04, 2008
Keyboard.Modifiers sometimes doesn't work
There are many examples how to implement keyboard hooks in .NET. The one I used as example is http://blogs.vertigo.com/personal/ralph/Blog/archive/2007/02/12/wpf-low-level-keyboard-hook-sample.aspx
There is very important comment there that says Keyboard.Modifier property does not return correct values. First, I wanted to avoid linking to System.Windows.Form assembly and didn't pay much attention go this comments. Well, it didn't work. I had come back to using System.Windows.Forms.Control.ModifierKeys. To make interface more WPF compatible I just converted value this way:
System.Windows.Forms.Keys m = System.Windows.Forms.Control.ModifierKeys;
ModifierKeys m2 = ModifierKeys.None;
if ((m & System.Windows.Forms.Keys.Control) != 0)
m2 = m2 | ModifierKeys.Control;
if ((m & System.Windows.Forms.Keys.Alt) != 0)
m2 = m2 | ModifierKeys.Alt;
if ((m & System.Windows.Forms.Keys.Shift) != 0)
m2 = m2 | ModifierKeys.Shift;
if ((m & System.Windows.Forms.Keys.Apps) != 0)
m2 = m2 | ModifierKeys.Windows;
Wednesday, August 20, 2008
SystemEvents.PowerModeChanged Event and services
As I posted before I wrote small application to know how much time my daughter logged on computer. To do this I used SENS events. And then I noticed that numbers are to big. And the problem was that these events did not trigger when computer went to sleep or hibernate modes. And service did not stop either. .NET has SystemEvents.PowerModeChanged event that I decided to use. Surprise, it doesn't work from service. The solution was found in System.Management name space and ManagementEventWatcher class.
Here is the fragment of the code I used:
WqlEventQuery query = new WqlEventQuery("Win32_PowerManagementEvent");
_watcher = new ManagementEventWatcher(query);
_watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
_watcher.Start();
when my service started.
_watcher.Stop();
when it stopped.
void watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
try
{
int eventType = Convert.ToInt32(e.NewEvent.Properties["EventType"].Value);
switch (eventType)
{
case 4:
Sleep();
break;
case 7:
Resume();
break;
}
}
catch (Exception ex)
{
Log(ex.Message);
}
}
- event handler.
Sunday, August 17, 2008
InkEdit does not work when there are no recognition package for your language
It was a midnight when I got pinged by my boss from Mexico. The Ink control that we inserted into our application didn't work. And we used InkEdit.
It was tested before, everything worked fine, there were no recent changes that could break it, and I didn't have Tablet PC at home to duplicate it. We had terminal session and I just couldn't get it. It worked before.
Finally, we realized that standard pop up control (with virtual keyboard) didn't work either. And then we noticed that current language was Spanish, and then we switched to English and everything worked.
So, what they (customer) did - they changed default keyboard input to Spanish, and InkEdit stopped working. Which makes some sense, InkEdit supposed to be used to recognize what you entered, not just store pictures. If you need just store everything was scrubbled without attempting to recognize it, InkPicture should be used.
Getting File size using ContentLength when downloading from FTP server
There is a property ContentLength of class FtpWebResponse. When I wrote the code similar to example, provided by MSDN, this property always returned -1. As it turned out, it was important to read fine print. I had to set method to GetFileSize to get proper value. So, what I got in result that worked was this:
// Get file size
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_uri);
request.Method = WebRequestMethods.Ftp.GetFileSize;
request.Credentials = credential;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
long fileSize = response.ContentLength;
// Download file
request = (FtpWebRequest)WebRequest.Create(_uri);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = credential;
response = (FtpWebResponse)request.GetResponse();
And then getting stream from response etc.
Saturday, August 09, 2008
How to implement LinkControl in WPF
There is no LinkControl in WPF, so if you need to have hyperlink in your form, different approach is needed. I used this:
XAML:
<TextBlock>Go<Hyperlink NavigateUri="http://counttime.alexeyev.org" Click="Hyperlink_Click">http://counttime.alexeyev.org</Hyperlink></TextBlock>
C#:
private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
Hyperlink link = (Hyperlink)sender;
Process.Start(link.NavigateUri.AbsoluteUri);
}