这是C#获取控件在窗体上的位置的反问题。
给定窗体内的Point位置,我如何找出在该位置用户可见的控件?
我目前正在使用HelpRequested表单事件来显示一个单独的帮助表单,如MSDN中所示:MessageBox.Show Method。
在MSDN示例中,事件sender
控件用于确定帮助消息,但sender
在我的情况下,始终是表单而不是控件。
我想使用HelpEventArgs.MousePos来获取表单内的特定控件。
如果我有一个返回某种指针的函数,我会在错误时将返回值设置为NULL来检查错误.
char *foo(void) {
//If stuff doesn't go okay
return NULL;
}
char *bar = foo();
if(!bar) return 1;
Run Code Online (Sandbox Code Playgroud)
这很有效,因为我知道在这种情况下我永远不会想要返回NULL.
但是,有时候我会有返回整数的函数(在这种情况下,我从配置文件中读取int).但是,问题是现在无法检查返回的值是否有错误,因为任何值(包括0)都可能是真的.
一些解决方法:
这两个问题都是我有一组函数,它们都做同样的事情,但对于不同的类型,我想维护一个常规接口,以便它们可以以相同的方式使用.
是否有其他解决方案不涉及更改功能的接口?处理这种情况最常见的方法是什么?
选择解决方案
感谢您对此的所有想法和答案.
最后我决定,如果函数是为了返回某些数据,则只能通过error参数返回错误.否则,将直接返回错误.
我选择了这个根,因为通常我发现当返回更复杂的数据形式时,潜在错误的数量几乎总是大于1.这意味着使用NULL作为错误数据的唯一来源是不切实际的,因为它意味着没有确定错误实际是什么的方法.对于将数据作为int返回的函数,也不可能将多个不同的错误代码与有效数据区分开来.
当然,对于没有真正返回任何数据的函数,情况也是如此,在这种情况下,我可以自由地将返回值用作错误代码.
我也喜欢上述模式明确区分返回数据的函数和不返回数据的函数.
这是我的带有数据注释的Test类:
class Test
{
[Required, StringLength(10)]
public string MyProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制台测试程序:
class Program
{
static void Main(string[] args)
{
var test = new Test {
MyProperty = "this is way more than 10 characters and should fail."
};
var context = new ValidationContext(test, null, null);
// No exception here! (why not?)
Validator.ValidateObject(test, context);
test.MyProperty = null;
// Exception here, as expected
Validator.ValidateObject(test, context);
}
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,当字符串长度太长时,我没有得到验证异常.当我将属性设置为null并重新验证时,我确实得到了验证异常(如预期的那样).我的字符串长度注释没有被强制执行的任何想法?
我有一个带有大量注释(3000+)的地图视图,当用户缩放到合理的水平时,一切都很好而且快速.
虽然当用户缩小并且大量注释进入视图时,由于一次显示大量注释,因此存在大量减速.处理这个问题的最佳方法是什么?
我正在使用的当前解决方案:
- (void)mapView:(MKMapView *)_mapView regionDidChangeAnimated:(BOOL)animated {
NSArray *annotations = [_mapView annotations];
MapAnnotation *annotation = nil;
for (int i=0; i<[annotations count]; i++)
{
annotation = (MapAnnotation*)[annotations objectAtIndex:i];
if (_mapView.region.span.latitudeDelta > .010)
{
[[_mapView viewForAnnotation:annotation] setHidden:YES];
warningLabel.hidden = NO;
}
else {
[[_mapView viewForAnnotation:annotation] setHidden:NO];
warningLabel.hidden = YES;
}
}
}
Run Code Online (Sandbox Code Playgroud)
虽然由于环路的大小很大,但在放大和缩小以及滚动时会导致很多速度变慢.我似乎无法想到一个更好的方法来处理这个问题,有没有办法只循环当前正在显示的注释或沿着那些线的东西来减少循环的大小?
我有一个实用程序函数用于通过CLI(cmd,bash等)执行程序.它返回的3项的数组:STDOUT
,STDERR
和EXIT CODE
.
到目前为止,它一直很好地没有问题.事实上,我遇到的问题并没有真正阻碍它的功能,但我关注的是性能.
问题是在某些情况下,PHP会多次运行相同的命令(在我的情况下是3次),即使它只应该执行一次.
/**
* Executes a program and waits for it to finish, taking pipes into account.
* @param string $cmd Command line to execute, including any arguments.
* @param string $input Data for standard input.
* @param boolean $log Whether to log execution failures or not (defaults to true).
* @return array Array of "stdout", "stderr" and "return".
*/
public static function execute($cmd,$stdin=null,$log=true){
//static $once=true; if(!$once)die; $once=false;
$proc=proc_open($cmd, array(
0=>array('pipe','r'),
1=>array('pipe','w'), …
Run Code Online (Sandbox Code Playgroud) 我的理解是Microsoft Visual Studio被重写为使用WPF.我仍然不清楚为什么,但承认我对WPF的了解非常有限.
我的问题是,是否有人知道WPF对C++有多少支持,以及Visual Studio是否仍然用C++编写.
就个人而言,WPF主要似乎是.NET/VB/C#的东西.是否有人使用它与C++?
我的机器上有Python 2.x和3.x(Mac OS X 10.6).对于某些我想要使用版本2的东西,但对于其他我想要版本3.我喜欢IDLE软件进行编辑/运行,但它总是使用版本3.
有没有办法更改IDLE使用的解释器版本?
谢谢!
我正在查看本教程http://asp-umb.neudesic.com/mvc/tutorials/validating-with-a-service-layer--cs,了解如何围绕包装器包装我的验证数据.
我想使用依赖注入.我正在使用ninject 2.0
namespace MvcApplication1.Models
{
public interface IValidationDictionary
{
void AddError(string key, string errorMessage);
bool IsValid { get; }
}
}
Run Code Online (Sandbox Code Playgroud)
//包装
using System.Web.Mvc;
namespace MvcApplication1.Models
{
public class ModelStateWrapper : IValidationDictionary
{
private ModelStateDictionary _modelState;
public ModelStateWrapper(ModelStateDictionary modelState)
{
_modelState = modelState;
}
#region IValidationDictionary Members
public void AddError(string key, string errorMessage)
{
_modelState.AddModelError(key, errorMessage);
}
public bool IsValid
{
get { return _modelState.IsValid; }
}
#endregion
}
}
Run Code Online (Sandbox Code Playgroud)
//控制器
private IProductService _service;
public ProductController()
{ …
Run Code Online (Sandbox Code Playgroud) 我有一个问题,我无法在我的控制器中加载我的库:S
我收到此错误:消息:未定义属性:Profil :: $ profileWall
我的图书馆:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ProfileWall
{
private $CI;
public function __construct()
{
$this->CI =& get_instance();
}
public function wallShow()
{
$this->CI->load->model('profil_model');
return $this->CI->profil_model->wallGet($this->CI->uri->segment(3));
}
}
Run Code Online (Sandbox Code Playgroud)
和我的控制器
function index()
{
$this->load->model('profil_model');
$data['query'] = $this->profil_model->vis_profil($this->uri->segment(3));
//Henter lib profilwall så man kan vise wall beskeder i profilen
$this->load->library('profileWall');
$data['queryWall'] = $this->profileWall->wallShow();
$data['content'] = 'profil_view';
$this->load->view('includes/template', $data);
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我的javascript代码是这样的:
var myOptions = {
scrollwheel:false,
zoom: 15,
center: the_lat_long,
mapTypeControl:false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
Run Code Online (Sandbox Code Playgroud) c# ×2
c ×1
c++ ×1
codeigniter ×1
controller ×1
controls ×1
editor ×1
google-maps ×1
iphone ×1
javascript ×1
jquery ×1
k2f ×1
load ×1
mkmapview ×1
modelstate ×1
ninject ×1
objective-c ×1
php ×1
proc-open ×1
python ×1
python-idle ×1
ria ×1
validation ×1
version ×1
windows ×1
winforms ×1
wpf ×1