insert picturebox into picturebox vb6 - vb6

I have a code from some article. (picture inner inside the picture outer)
The result of the code is when i click the place in the picture outer, the picture inner will show in the place i clicked, but in diagonal place.
It wasnt in the right place i click. I want the picture inner will show
in the spot i clicked
picturebox1 name = PictOuter
picturebox2 name = PictInner
Dim pos As String
Dim bos As String
pos = Format(x / PictOuter.Width * 100, "0")
bos = Format(y / PictOuter.Height * 100, "0")
PictInner.Left = PictOuter.Width * pos / 100
PictInner.Top = PictOuter.Height * bos / 100
PictInner.Visible = True
Your information will so helpfull, thanks for your attention

If you simply want the top left corner of the inner box to be where you click, you can use the MouseDown event of the outer PictureBox, which would look like this:
Private Sub PictOuter_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
PictInner.Left = X
PictInner.Top = Y
End Sub
You can also choose to perform whatever calculations you'd like on X and Y to center the inner PictureBox however you'd like.

Related

How to automatically resize or reposition controls on a form when the form is resized?

So I'm trying to make my form fit to all monitors. Some have different display resolution and scale.
I can resize my form to fit to the display but all properties of its contents don't adjust to that new size.
What I want is if the form is scaled to fit to the display, the controls on the Form should adjust as well. Specifically properties like Left, Top, Width, Height, and so one, on every control.
The size could be scaled down or up.
It's possible to iterate through all of the controls on the form (mostly) programmatically, rather than having to explicitly adjust each control. You may have to put in some exceptions for some types of controls (such as timers, which I've put in the example), but generally you can use something like:
Option Explicit
Private Type ControlInfo_type
Left As Single
Top As Single
Width As Single
Height As Single
FontSize As Single
End Type
Dim ControlInfos() As ControlInfo_type
Private Sub Form_Load()
Dim ThisControl As Control
ReDim Preserve ControlInfos(0 To 0)
ControlInfos(0).Width = Me.Width
ControlInfos(0).Height = Me.Height
For Each ThisControl In Me.Controls
ReDim Preserve ControlInfos(0 To UBound(ControlInfos) + 1)
On Error Resume Next ' hack to bypass controls with no size or position properties
With ControlInfos(UBound(ControlInfos))
.Left = ThisControl.Left
.Top = ThisControl.Top
.Width = ThisControl.Width
.Height = ThisControl.Height
.FontSize = ThisControl.FontSize
End With
On Error GoTo 0
Next
End Sub
Private Sub Form_Resize()
Dim ThisControl As Control, HorizRatio As Single, VertRatio As Single, Iter As Integer
If Me.WindowState = vbMinimized Then Exit Sub
HorizRatio = Me.Width / ControlInfos(0).Width
VertRatio = Me.Height / ControlInfos(0).Height
Iter = 0
For Each ThisControl In Me.Controls
Iter = Iter + 1
On Error Resume Next ' hack to bypass controls
With ThisControl
.Left = ControlInfos(Iter).Left * HorizRatio
.Top = ControlInfos(Iter).Top * VertRatio
.Width = ControlInfos(Iter).Width * HorizRatio
.Height = ControlInfos(Iter).Height * VertRatio
.FontSize = ControlInfos(Iter).FontSize * HorizRatio
End With
On Error GoTo 0
Next
End Sub
I tested this with the default form with a CommandButton, Frame, Timer, and TextBox, and it seemed to work OK. You'll probably want to tune the limits on the minimum and maximum sizes for appearance, and my handling of the font is very crude; this could be optimized also. But perhaps this could be a starting point.
This code depends upon the controls iterating the same way each time, which could conceivably break. One way around this would be to use a Collection or other data structure with the name of the control as a key; when iterating in the .Resize event, each control would be looked up by name. Additional structure will be necessary if any of the controls are themselves arrays, and even more if controls are loaded or unloaded dynamically.

Generating random non repeating colors to labels in Visual Basic 6

I want to call a random color between 8 different colors and display it in a label as its backcolor in Visual Basic. How can I display the colors without repeating a color that has been called out on a specific label?
For example, if color red is called out and displayed in labelA1, how can I make sure that color red won't be called out and displayed in labelB1, labelC1 or labelD1 but can be called out in labelA13 or labelB16?
Below is a picture to help understand the example above.
Use this code to make a list of colors then knock them off the list every time one is used.
Private Function RandomizeLabelColors() As Integer
Randomize()
Dim listOfColors As List(Of Color) = {Color.Red, Color.Blue, Color.Green, Color.Orange}.ToList
Dim labels As List(Of Label) = {Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8,
Label9, Label10, Label11, Label12, Label13, Label14, Label15, Label16}.ToList
Dim i As Integer = 0
Do Until listOfColors.Count = 0
Dim targetIndex As Integer = Int(Rnd() * listOfColors.Count)
labels(i).BackColor = listOfColors(targetIndex)
labels(i + 4).BackColor = listOfColors(targetIndex)
labels(i + 8).BackColor = listOfColors(targetIndex)
labels(i + 12).BackColor = listOfColors(targetIndex)
listOfColors.RemoveAt(targetIndex)
i += 1
Loop
Return 0
End Function
I have my labels in a 4x4 grid.
-Mg

how to set the scale to logarithmic and linear when i right click on the particular figure in GUI matlab

I have created a Gui in matlab which has three axes and i would like to change the scale to log and linear by right click on the particular figure and select the scale options(1.logarithmic 2.linear). I tried using the UIcontextmenu. But i didn't get the result.
firstcycle_mass = handles.Data1{1,1}(1:6392,1);
firstcycle_ioncurrent = handles.Data1{1,2}(1:6392,1);
replace_with_dot_firstcycle_mass = strrep(firstcycle_mass, ',','.');
X1 = str2double(replace_with_dot_firstcycle_mass);
replace_with_dot_firstcycle_ioncurrent = strrep(firstcycle_ioncurrent, ',','.');
Y1 = str2double(replace_with_dot_firstcycle_ioncurrent);
axes(handles.axes1);
plotline= plot(X1,Y1,'r');
xlabel('Mass [amu]');
ylabel('Ion current [A]');
c=uicontextmenu;
plotline.UIContextMenu = c;
% Create menu items for the uicontextmenu
m1 = uimenu(c,'Label','logarithmic','Callback',#setlinestyle);
m2 = uimenu(c,'Label','linear','Callback',#setlinestyle);
function setlinestyle(source,callbackdata)
switch source.Label
case 'logarithmic'
set(gca,'yscale','log')
case 'linear'
set(gca,'yscale','linear')
end
end

Automatically resize form/controls according to each computer's resolution

I've created a program that is fully functional and I have sent it to some clients. Some of them have really old computers with really low resolution and they can't access it easily since the form and the controls are oversized for them. Is there an easy way for me to make it to automatically resize both form and controls according to the resolution?
As I've said in the title, this is for Visual Basic 6.0. Thanks to all of you in advance.
You can store size and location of each control on the form, and move or resize controls according to your needs.
In the code below, I use "TabIndex" property as unique id for each control (I can't remember in my old VB6 memory if that's the right thing to do...).
I store the size of the form, and the size and location of each control in the Form_Load event.
Private lWidth As Long
Private lHeight As Long
Private Enum ePROPERTY
ep_Top = 0
ep_Left = 1
ep_Width = 2
ep_Height = 3
End Enum
Private aControlSize() As Long
Private Sub Form_Load()
Dim ctlTmp As Control
lWidth = Me.Width
lHeight = Me.Height
ReDim aControlSize(3, Form1.Controls.Count)
For Each ctlTmp In Form1.Controls
aControlSize(ctlTmp.TabIndex, ep_Top) = ctlTmp.Top
aControlSize(ctlTmp.TabIndex, ep_Left) = ctlTmp.Left
aControlSize(ctlTmp.TabIndex, ep_Width) = ctlTmp.Width
aControlSize(ctlTmp.TabIndex, ep_Height) = ctlTmp.Height
Next
End Sub
Then each time the form is resized (Form_resize event), you'll have to move or resize each control.
Some of them need to be anchored to the right or to the bottom (or both). Some need to be resized and moved. Others don't need nothing.
Private Sub Form_Resize()
Dim ctlTmp As Control
For Each ctlTmp In Form1.Controls
Select Case LCase$(ctlTmp.Name)
Case "text1"
' Text1 is anchored to the left and right borders of the form :
ctlTmp.Width = Me.Width - (lWidth - aControlSize(ctlTmp.TabIndex, ep_Width))
Case "command1"
' Command1 is anchored to the right border of the form :
ctlTmp.Left = aControlSize(ctlTmp.TabIndex, ep_Left) - (lWidth - Me.Width)
Case "check1"
' check1 is anchored to the bottom border of the form :
ctlTmp.Top = aControlSize(ctlTmp.TabIndex, ep_Top) - (lHeight - Me.Height)
End Select
Next
End Sub
Form loaded :
Form Resized :
Please be advised that my code is largely perfectible...
There's probably a more elegant solution that goes through overload each Control and to add properties/methods like the existing ones in dotnet.

View Datagridview in Print Preview Window

I am creating an application in vb.net. I have a datagridview control in my VB form. I need to view it on printpreview window with the contents in it. I have other control like labels and textboxes in the form and I can view all in printpreview. In the case of Datagridview control, I have a working printpreview code which I got from net. My problem is, I need to change the x and y positions of datagridview control. With the following code, the datagridview control is displaying over the other controls. I dont know how to do it in this code. Please help me.
I need to change the x and y positions of DataGridView like given in the below code(50 and 225).
e.Graphics.DrawString(Label7.Text, Label7.Font, Brushes.Black, 50, 225)
The code I used to display gridview is given below.
Code :
Dim ColumnCount As Integer = DataGridView1.ColumnCount
Dim RowCount As Integer = DataGridView1.RowCount
Dim CellTopPos As Integer = PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Top
For Row = 0 To RowCount - 2
Dim CellLeftPos As Integer = PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Left
For Cell = 0 To ColumnCount - 1
Dim CellValue As String = DataGridView1.Rows(Row).Cells(Cell).Value.ToString()
Dim CellWidth = DataGridView1.Rows(Row).Cells(Cell).Size.Width + 10
Dim CellHeight = DataGridView1.Rows(Row).Cells(Cell).Size.Height
Dim Brush As New SolidBrush(Color.Black)
e.Graphics.DrawString(CellValue, New Font("arial", 9), Brush, CellLeftPos, CellTopPos)
e.Graphics.DrawRectangle(Pens.Black, CellLeftPos, CellTopPos, CellWidth, CellHeight)
CellLeftPos += CellWidth
Next
CellTopPos += DataGridView1.Rows(Row).Cells(0).Size.Height
Next
Use this class .. unfortunately there is characters limit so i can not able to post code
http://www.codeproject.com/Articles/18042/Another-DataGridView-Printer

Resources