330 likes | 461 Vues
BAZELE PROGRMARII PE OBIECTE PROIECTE IN VISUAL BASIC. Conf univ dr Botezatu Cezar INFORMATICĂ MANAGERIALĂ Anul I. STUDII DE LICEN ŢĂ. EXERCITIUL 1.
E N D
BAZELE PROGRMARII PE OBIECTEPROIECTE IN VISUAL BASIC Conf univ dr Botezatu Cezar INFORMATICĂ MANAGERIALĂ Anul I. STUDII DE LICENŢĂ
EXERCITIUL 1 Să se proiecteze şi să se realizeze o aplicaţie care să permită analiza unui text introdus de la tastatură, afişarea primului cuvânt şi apoi la cerere a următoarelor cuvinte din text. Să se afişeze deasemenea numărul de caractere din text.
EXERCITIUL 1 Rezolvare: Definim o formă
EXERCITIUL 1 Public Class Form1 Dim cuv As String Dim lung As Integer Dim i1 As Integer Dim i As Integer Dim a As Integer Dim texta as string
EXERCITIUL 1 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click End End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Text = LTrim(Me.TextBox1.Text) lung = Len(Text)
EXERCITIUL 1 Me.TextBox3.Text = Len(Text) a = InStr(Text, " ") i = 1 cuv = Mid(Text, i, a) Me.TextBox2.Text = cuv i = i+a Me.Button1.Enable=FALSE End Sub
EXERCITIU Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Text = LTrim(Me.TextBox1.Text) iF I >0 then I1=I Else i1=1 endif a = InStr(Text, " ") If i1< lung - a Then a = InStr(Mid(Text, i1 + 1, lung – i1), " ") cuv = Mid(Text, i1, a) Me.TextBox2.Text = cuv Me.Refresh() i 1= i1 + a Else cuv = Mid(Text, i1, a) Me.TextBox2.Text = cuv Me.Refresh() End If End Sub End Class
EXERCITIUL 1 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click a = InStr(Text, " ") If i < lung - a Then a = InStr(Mid(Text, i + 1, lung - i), " ") cuv = Mid(Text, i, a) Me.TextBox2.Text = cuv Me.Refresh() i = i + a Else
EXERCITIUL 1 Else cuv = Mid(Text, i, a) Me.TextBox2.Text = cuv Me.Refresh() End If End Sub End Class
EXERCITIUL 1 Rezultatul:
EXERCITIUL 2 . Să se realizeze un editor de texte, care să permită introducerea unui text de la tastatură şi apoi salvarea lui pe disc prin intermediul unei ferestre de dialog. Editorul trebuie să permită şi deschiderea unui fişier existent în vederea editării. Rezolvare: Definim trei forme: • Forma de lucru”Editor de texte”; • Forma OPEN • Forma SAVE
EXERCITIUL 2 EDITOR DE TEXTE
EXERCITIUL 2 Forma OPEN
EXERCITIUL 2 Forma SAVE
EXERCITIUL 2 Codul sursă aferent primei forme: Imports System Imports System.IO Public Class Form1 Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click End End Sub
EXERCITIUL 2 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System. EventArgs) Handles Button3.Click TextBox1.Text = "" End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System. EventArgs) Handles Button1.Click Form2.Show() Form2.TextBox1.Text = "" Form2.TextBox2.Text = ""
EXERCITIUL 2 On Error Resume Next : Me.Hide() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System. EventArgs) Handles Button2.Click Form3.Show() Me.Hide() End Sub End Class
EXERCITIUL 2 Forma OPEN Codul sursă aferent formei a II-a: Imports System Imports System.IO Public Class Form2 Private Sub DriveListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DriveListBox1.SelectedIndexChanged
EX 2 / OPEN Try DirListBox1.Path=DriveListBox1.Drive Catch End Try TextBox1.Text = DirListBox1.Text End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System. EventArgs) Handles Button2.Click Form1.Show() Me.Hide() End Sub
EX 2 / OPEN PrivateSubDirListBox1_SelectedIndex Changed(ByVal sender As System. Object, ByVal e As System.EventArgs) Handles DirListBox1.SelectedIndex Changed Try FileListBox1.Path =DirListBox1.Path Catch End Try TextBox1.Text = DirListBox1.Text End Sub
EX 2 / OPEN Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.TextBox1.Text =Me.TextBox1.Text & Me.TextBox2.Text & Me.FileListBox1. SelectedItem Try Using fisier As StreamReader = New StreamReader(Me.TextBox1.Text) Dim linie As String
EX 2 / OPEN Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.TextBox1.Text = Me.TextBox1.Text & Me.TextBox2.Text & Me.FileListBox1.SelectedItem Try Using fisier As StreamReader = New StreamReader(Me.TextBox1.Text) Dim linie As String Do linie = fisier.ReadLine() Form1.TextBox1.Text = Form1.TextBox1.Text & linie Loop Until linie Is Nothing fisier.Close() End Using Catch End Try Form1.Show() Me.Hide() End Sub Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TextBox2.Text = "" TextBox1.Text = "" Me.DirListBox1.Refresh() Me.FileListBox1.Refresh() Me.DriveListBox1.Refresh() End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged FileListBox1.Pattern = Me.ComboBox1.Text End Sub End Class
EX 2 / OPEN linie = fisier.ReadLine() Form1.TextBox1.Text = Form1.TextBox1. Text & linie Loop Until linie Is Nothing fisier.Close() End Using Catch End Try Form1.Show() Me.Hide() End Sub
EX 2 / OPEN Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TextBox2.Text = "" TextBox1.Text = "" Me.DirListBox1.Refresh() Me.FileListBox1.Refresh() Me.DriveListBox1.Refresh() End Sub
EX 2 / OPEN Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged FileListBox1.Pattern = Me.ComboBox1.Text End Sub End Class
EX 2 / SAVE Codul aferent formei a treia se prezintă astfel: Imports System Imports System.IO Public Class Form3 Private Sub DriveListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DriveListBox1.SelectedIndexChanged
EX 2 / SAVE Try DirListBox1.Path = DriveListBox1.Drive Catch End Try TextBox2.Text = DirListBox1.Text End Sub Private Sub DirListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DirListBox1.SelectedIndexChanged
EX 2 / SAVE Try FileListBox1.Path = DirListBox1.Path Catch End Try TextBox3.Text = Me.TextBox3.Text & Me.DirListBox1.SelectedItem & "\" End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
EX 2 / SAVE ComboBox1.SelectedIndexChanged FileListBox1.Pattern = Me.ComboBox1. Text End Sub Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" End Sub
EX 2 / SAVE Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System. EventArgs) Handles Button2.Click Form1.Show() Me.Hide() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click TextBox1.Focus()
EX 2 / SAVE If TextBox1.Text = "" Then MessageBox.Show("Nu ati introdus numele fisierului!!!", "Atentie", Message BoxButtons.OK, MessageBoxIcon.Warning) Else TextBox4.Text = Me.TextBox2.Text & TextBox3.Text & TextBox1.Text Using fisier As StreamWriter = New StreamWriter(TextBox4.Text) fisier.WriteLine(Form1.TextBox1.Text) fisier.WriteLine(DateTime.Now)
EX 2 / SAVE fisier.Close() MessageBox.Show("Fisierul a fost salvat in " & Me.TextBox4.Text, "Save", Message BoxButtons.OK,MessageBoxIcon.Question) End Using End If End Sub End Class