|
Simon Stevens wrote: What are you counting? Pairs of letters?
All of them, yes!
|
|
|
|
|
leppie wrote: Simon Stevens wrote:
What are you counting? Pairs of letters?
All of them, yes! [Poke tongue]
But seemingly only explicit pairs (the 'ccc's are ignored).
In which case, loop through each character. For each character, if it matches the next one, but not the one after, then count it and move on. If you reach a repeated string of the same chars, jump over them all, and don't count them.
Simon
|
|
|
|
|
public static System.Collections.Generic.List<string>
FindSequences
(
string Chars
,
uint Length
)
{
System.Collections.Generic.List<string> result =
new System.Collections.Generic.List<string>() ;
char current = '\0' ;
int count = 0 ;
foreach ( char c in Chars )
{
if ( c != current )
{
if ( count == Length )
{
result.Add ( new string ( current , count ) ) ;
}
current = c ;
count = 0 ;
}
count++ ;
}
if ( count == Length )
{
result.Add ( new string ( current , count ) ) ;
}
return ( result ) ;
}
Though it'll fail when Length==0
|
|
|
|
|
Hi,
I'm new to C# and .Net architecture , so sorry about the silly question.... .
I'm using a grid control in order to display data from the data base.
My problem is when the user press the CTRL+C in a specific cell in order to copy the data
to a different cell.
The result is that the new cell contain the data of the entire row , instead of the data of the copied cell.
note that if I use the mouse right click and "copy" <-> "paste" it is working fine.
My question is how can I capture the CTRL+C and CTRL+V with the keyboard?
With best regards,
Eli
|
|
|
|
|
Are you talking about the DataGridView control? I don't seem to be having any sort of problem with CTRL-C, CTRL-V to copy and paste from cell to cell with that control.
Anyways, you might take a look at the grid's KeyDown event and see if you can use the e.Modifiers to capture the Control key, and e.KeyCode to capture the 'C' or 'V', but, to be honest, I don't think that event will trigger in an editable cell of the DataGridView. The other option I can think of is to override ProcessCmdKey and see if you can capture the key combinations there.
|
|
|
|
|
Some methods in the framework take the name of a method as a string, e.g. ObjectDataSource and most of WPF. I don't like this as the compiler and IDE tools can't link the string and the method. So I want to generate the string from the token. I came up with this:
class TestClass
{
public static void Test()
{
string s = GetName<string,int>( Method );
}
static int Method( string s ) { return 0; }
static string GetName<T,TResult>( Func<T,TResult> method )
{
return method.Method.Name;
}
...
GetName overloads for Actions and Funcs
...
}
This is ok, but I don't like having to specify the generic types when calling GetName .
I don't think there's a way around this, because method groups don't participate in type inferencing.
So my question is: is there a better way?
Thanks
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
Although not exactly what you are looking for, but at least a bit more generic:
class TestClass
{
public static void Test()
{
string s = GetName( new Func<string,int>(Method));
}
static int Method(string s) { return 0; }
static string GetName(Delegate method)
{
return method.Method.Name;
}
}
I will try think of a better way
|
|
|
|
|
Thanks leppie, that is better.
You can also just use a cast:
string s = GetName( ( Func<string,int> ) Method );
But this doesn't work:
string s = GetName( ( Delegate ) Method );
----------------------------------
Be excellent to each other
|
|
|
|
|
Are you trying to get the method name from the method itself?
If so:
System.Reflection.MethodBase.GetCurrentMethod().Name
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Yes, but then he would have to call the method!
|
|
|
|
|
Hi there,
I am well aware of the usage of Control.Invoke() in a Form (System.Windows.Forms) to be able to access UI components in a thread-safe manner so that it is the thread that holds the window handle the one that executes it. I was wondering, if I don't have a reference to the Form but instead to the window thread (as a Thread object) is there a way to use that Thread object (or anything similar that can be extracted from the execution context) to do the same as Control.Invoke() ?
Background: I have a C# assembly (utils library) that calls into C++, but callbacks are returned in a separate thread, and I want the assembly to be application-independent but to return the callbacks (using events) in the same thread that the requests were made in.
Thanks in advance!
Carles
|
|
|
|
|
Say your thread calls MyCallbackObject.Callback().
Then you can test whether the object can synchronize the call and call it's Invoke() method if it does:
ISynchronizeInvoke synchronizeInvoke = MyCallbackObject As System.ComponentModel.ISynchronizeInvoke;
if (synchronizeInvoke == null)
{
MyCallbackObject.Callback()
}
else
{
synchronizeInvoke.Invoke(myDelegate);
}
where myDelegate is a delegate to a method wrapping MyCallbackObject.Callback().
|
|
|
|
|
Hello,
Useful classes are the System.ComponentModel AsyncOperationManager and AsyncOperation. I've pasted in some code that performs a directory search in a background thread. When complete the background thread raises on event on the thread that called BeginSearch.
This calling thread does not need to consider the source of the event and may use the information received in the EventArgs directly in any control.
Alan.
using System;
using System.ComponentModel;
using System.Threading;
using System.IO;
namespace SpecTest {
class DirectorySearchCP {
private AsyncOperation operation;
private String startDir;
private DirectoryInfo[] dirlist;
public event EventHandler<DirListEventArgs> SearchComplete;
public void BeginSearch(String startDir) {
this.startDir = startDir;
operation = AsyncOperationManager.CreateOperation(null);
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProcedure), null);
}
private void SearchDirectory() {
DirectoryInfo s = new DirectoryInfo(startDir);
dirlist = s.GetDirectories("*.*", SearchOption.TopDirectoryOnly);
}
private void ThreadProcedure(Object stateInfo) {
try {
SearchDirectory();
} finally {
operation.PostOperationCompleted(new SendOrPostCallback(OnSearchComplete), dirlist);
}
}
private void OnSearchComplete(Object state) {
EventHandler<DirListEventArgs> handler = SearchComplete;
if (handler != null) {
DirListEventArgs args = new DirListEventArgs();
args.dirlist = (DirectoryInfo[])state;
handler(this, args);
}
}
}
public class DirListEventArgs : System.EventArgs {
public DirectoryInfo[] dirlist;
}
}
|
|
|
|
|
Managed code .NET 3.5 C# WinForms application runs OK from Vista when in local drive.
When application is copied to mapped network drive or started from \\othercmp\c\myapp\myapp.exe folder , it does not start: Vista shows
MyApp has stopped working
Windows is checking for a solution to the problem..
How to fix ?
Andrus
|
|
|
|
|
|
If you want your app to automatically just work from a network share you need to design it so it doesn't require full trust
I need that in MDI child form Escape key closes form, Ctrl+F1 cycles and
Ctrl+F10 toggles maximizing. For this I use code below in form base class which requires full trust.
How to implement those features without full trust ?
Andrus.
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.Escape:
if (this != FormManager.MainForm)
{
Close();
return true;
}
break;
case Keys.Control | Keys.F1:
for (int i = 0; i < FormManager.MainForm.MdiChildren.Length; i++)
{
if (FormManager.MainForm.MdiChildren[i] == this)
{
if (i < FormManager.MainForm.MdiChildren.Length - 1)
FormManager.MainForm.MdiChildren[i + 1].Focus();
else
FormManager.MainForm.MdiChildren[0].Focus();
return true;
}
}
return true;
case Keys.Control | Keys.F10:
if (WindowState == FormWindowState.Maximized)
WindowState = FormWindowState.Normal;
else
WindowState = FormWindowState.Maximized;
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
Andrus
|
|
|
|
|
Handle the forms keypress or keydown events instead of overriding the ProcessCmdKey method. Use code in their to max/min/cycle your forms based on the key pressed.
Simon
|
|
|
|
|
|
Ahh...interesting.
Have you tried the _forms_ previewkeydown event? (You have to set keypreview to true)
Simon
|
|
|
|
|
|
Sorry then, I'm out of ideas. There should be a way though. Detecting a key press barely seems like something that should require full trust.
Simon
|
|
|
|
|
No, there are no ways.
Detecting those keypresses in winForms requires application to run in full trust mode.
Andrus
|
|
|
|
|
I ran with adminstrator rights in Vista
CasPol.exe -m -ag 1.2 -url file://z:/myapp/* FullTrust
C:\...crosoft.NET\Framework\v2.0.50727>g 1.2 -url file:
Microsoft (R) .NET Framework CasPol 2.0.50727.1434
Copyright (c) Microsoft Corporation. All rights reserved.
The operation you are performing will alter security policy.
Are you sure you want to perform this operation? (yes/no)
y
Added union code group with "-url" membership condition to the Machine level.
Success
However
z:\myapp\myapp.exe
still shows immediately
Myapp as stopped working
Andrus
|
|
|
|
|
Sorry, I've no idea about this. Try catching and logging exceptions at the top level to see what's going wrong.
Simon
|
|
|
|
|
Exception which occurs in does not show .NET Winforms unhandled exception dialog.
So this exception cannot be catched in .NET code.
Andrus
|
|
|
|