我有一个表单,可以用鼠标在其中移动标签。我想做的是确保不要将一个标签移到另一个控件使用的空间中。
是否有比我在下面使用的方法更好,更有效的方法:
for each c as control in me.controls
if c.location.x > testcontrol.location.x and c.location.x < testcontrol.location.x + testcontrol.height and c.location.y > testcontrol.location.y and c.location.y < testcontrol.location.y + testcontrol.width then
'testcontrol shares space with control c
<do stuff>
End if
next
我发现上述内容过于笨重,难以理解/阅读。只是希望有一些VB魔术可以更有效地做到这一点。
从我的搜索中,我看到C#具有“ intersectswith”功能,但似乎找不到VB的类似功能。此post显示了vs使用intersectwith的方法,但是我无法将其用于我的工作。也许是其他版本的VB?
答案 0 :(得分:0)
您忘记添加Bounds
:
// c++ test ConsoleApplication2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
using namespace std;
#include<iostream>
#include<vector>
#include<unordered_set>
#include "c++ test ConsoleApplication2.h"//this file only contain one line"#pragma once"
std::unordered_set<std::unique_ptr<Employee >> scanned_set;
std::unordered_set<std::unique_ptr<Employee >> to_scan_set;
int countt;
// Employee info
class Employee {
public:
int getImportance(vector<std::unique_ptr<Employee >> employees, int id)
{
for (auto it = employees.begin(); it != employees.end(); ++it)
{
if ((*it)->id == id)
{
countt += (*it)->importance;
std::unique_ptr<Employee > testPointer(new Employee(1, 2, vector<int>()));
scanned_set.insert(testPointer); //error occurs here
scanned_set.insert(*it);
if (!(*it)->subordinates.empty())
{
for (auto to_scan_iterator = to_scan_set.begin(); to_scan_iterator != to_scan_set.end(); ++to_scan_iterator) {
to_scan_set.insert((*to_scan_iterator));
}
return getImportance();
}
}
}
}
int getImportance();
Employee(int id, int importance, vector<int>& subordinates) {
this->id = id;
this->importance = importance;
this->subordinates = subordinates;
}
// It's the unique ID of each node.
// unique id of this employee
int id;
// the importance value of this employee
int importance;
// the id of direct subordinates
vector<int> subordinates;
};
inline int Employee::getImportance()
{
if (to_scan_set.empty())
{
return 0;
}
else
{
for (auto it = to_scan_set.begin(); it != to_scan_set.end(); ++it)
{
if (scanned_set.find((*it)) == to_scan_set.end())
{
countt += (*it)->importance;
scanned_set.insert(*it);
if (!(*it)->subordinates.empty())
{
for (auto to_scan_iterator = to_scan_set.begin(); to_scan_iterator != to_scan_set.end(); ++to_scan_iterator)
{
if (scanned_set.find((*it)) == to_scan_set.end())
{
to_scan_set.insert((*to_scan_iterator));
}
return getImportance();
}
}
}
}
}
}
IntersectsWith
不是Control
的方法,而是Rectangle
的方法。