Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1490 connectés 

  FORUM HardWare.fr
  Programmation
  C#/.NET managed

  [VB .NET]Problème gestion d'onglets

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[VB .NET]Problème gestion d'onglets

n°1556602
moimael
Posté le 07-05-2007 à 16:42:58  profilanswer
 

Bonjour, dans le cadre du devellopement d'un logiciel, j'ai besoin d'onglets que l'utilisateur puisse ouvrir et fermer a volonté, un peu comme dans notepad++ ou firefox. C'est pour un editeur de texte. Dans chacun de ces onglets il y a une richtextbox ainsi qu'un bouton. J'ai donc plusieurs problèmes :
 
- Comment, quand j'ouvre un fichier texte, l'ouvrir dans la bonne richtextbox, celle qui correspond à l'onglet selectionné ?
 
- Comment mettre du code à executer pour tous les nouveaux bouton créés (à chaque fois le meme code, pour tous les boutons) ?
 
Un petit screen pour que vous compreniez mieux :
http://img153.imageshack.us/img153/103/screenkh9.th.png
 
ainsi que le code :

Code :
  1. Imports System.IO
  2. Imports mRibbon
  3. Imports Vista_Api
  4. Public Class Twinotes
  5.     Dim nbonglet As Integer = 0
  6.     Private Sub bandeau_Options_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bandeau_Options.Click
  7.         Me.Visible = False
  8.         Accueil.Text = "wTwin"
  9.     End Sub
  10.     Private Sub Themebleu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Themebleu.Click
  11.         RibbonControl.ColorScheme = mRibbon.ColorScheme.Blue
  12.         bandeau_Options.Invalidate()
  13.         bandeau_Texte.Invalidate()
  14.         Dim SW As StreamWriter
  15.         SW = File.CreateText("C:\Program Files\wTwin\config.ini" )
  16.         SW.WriteLine("0" )
  17.         SW.Close()
  18.     End Sub
  19.     Private Sub Themegris_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Themegris.Click
  20.         RibbonControl.ColorScheme = mRibbon.ColorScheme.Gray
  21.         bandeau_Options.Invalidate()
  22.         bandeau_Texte.Invalidate()
  23.         Dim SW As StreamWriter
  24.         SW = File.CreateText("C:\Program Files\wTwin\config.ini" )
  25.         SW.WriteLine("1" )
  26.         SW.Close()
  27.     End Sub
  28.     Private Sub Themechoix_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Themechoix.Click
  29.         If ColorDialog1.ShowDialog() = DialogResult.OK Then
  30.             RibbonControl.ColorScheme = mRibbon.ColorScheme.Custom
  31.             RibbonControl.Color = ColorDialog1.Color
  32.             bandeau_Options.Invalidate()
  33.             bandeau_Texte.Invalidate()
  34.             Dim SW As StreamWriter
  35.             SW = File.CreateText("C:\Program Files\wTwin\config.ini" )
  36.             SW.WriteLine("2" )
  37.             SW.Close()
  38.         End If
  39.     End Sub
  40.     Private Sub Nouveau_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Nouveau.Click
  41.         Dim Tabp As New TabPage
  42.         With Tabp
  43.             .Text = "Nouveau" & " " & nbonglet
  44.             .Name = "onglet" & nbonglet
  45.         End With
  46.         bandeau_Texte.Controls.Add(Tabp)
  47.         Dim rtb As New RichTextBox
  48.         With rtb
  49.             .Size = New System.Drawing.Size(786, 412)
  50.             .Location = New System.Drawing.Point(0, 0)
  51.             .Name = "Textenote" & nbonglet
  52.             .ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedVertical
  53.         End With
  54.         Tabp.Controls.Add(rtb)
  55.         Dim EnvoiTwin As New CommandLink
  56.         With EnvoiTwin
  57.             .Location = New System.Drawing.Point(558, 347)
  58.             .Name = "EnvoiTwin" & nbonglet
  59.             .Size = New System.Drawing.Size(205, 60)
  60.             .Text = "Envoyer vers le Twin"
  61.         End With
  62.         rtb.Controls.Add(EnvoiTwin)
  63.     End Sub
  64.     Private Sub EnvoiTwin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnvoiTwin.Click
  65.         Dim texte, titre As String
  66.         texte = Textenote.Text
  67.         titre = Onglet1.Text
  68.         For Each di As DriveInfo In DriveInfo.GetDrives()
  69.             If di.IsReady Then
  70.                 If di.VolumeLabel = "MYPHONE" Then
  71.                     If Not texte = Nothing Then
  72.                         Dim lettre As String
  73.                         lettre = di.Name
  74.                         Dim SW As StreamWriter
  75.                         SW = File.CreateText(lettre & titre & ".txt" )
  76.                         SW.WriteLine(texte$)
  77.                         SW.Close()
  78.                     Else : MsgBox("Vous n'avez pas renseigné tous les champs disponibles", MsgBoxStyle.Exclamation)
  79.                     End If
  80.                 End If
  81.             End If
  82.         Next
  83.     End Sub
  84.     Private Sub tabremoved_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles bandeau_Texte.ControlRemoved
  85.         Dim i As Integer
  86.         i = bandeau_Texte.TabCount - 2
  87.         If i = 1 Then
  88.             Nbonglets.Text = i & " " & "onglet ouvert"
  89.         ElseIf i > 1 Then
  90.             Nbonglets.Text = i & " " & "onglets ouverts"
  91.         End If
  92.         bandeau_Texte.SelectTab(i)
  93.     End Sub
  94.     Private Sub tabadded_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles bandeau_Texte.ControlAdded
  95.         Dim i As Integer
  96.         i = bandeau_Texte.TabCount - 1
  97.         If i = 1 Then
  98.             Nbonglets.Text = i & " " & "onglet ouvert"
  99.         ElseIf i > 1 Then
  100.             Nbonglets.Text = i & " " & "onglets ouverts"
  101.         End If
  102.         bandeau_Texte.SelectTab(i)
  103.         nbonglet += 1
  104.     End Sub
  105.     Private Sub Ouvrir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ouvrir.Click
  106.         If Not Textenote.Text = "" Then
  107.             Form1.Label1.Text = bandeau_Texte.SelectedTab.Text & " " & "a des modifications non enregistrées. Que voulez vous faire ?"
  108.             Form1.ShowDialog()
  109.         ElseIf Textenote.Text = "" Then
  110.             OpenFileDialog1.Filter = "Texte (*.txt)|*.txt"
  111.             If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
  112.                 Dim CheminFichier As String
  113.                 CheminFichier = OpenFileDialog1.FileName
  114.                 Try
  115.                     Dim Fichier As New System.IO.StreamReader(CheminFichier)
  116.                     Textenote.Text = Fichier.ReadToEnd
  117.                     Fichier.Close()
  118.                 Catch ex As System.Exception
  119.                     MsgBox("Impossible d'ouvrir le fichier, il est peut être en cours d'utilisation", MsgBoxStyle.Critical)
  120.                 Finally
  121.                 End Try
  122.                 bandeau_Texte.SelectedTab.Text = Path.GetFileNameWithoutExtension(CheminFichier)
  123.             End If
  124.         End If
  125.     End Sub
  126.     Private Sub Suppronglet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Suppronglet.Click
  127.         If Not Textenote.Text = "" Then
  128.             Form2.Label1.Text = bandeau_Texte.SelectedTab.Text & " " & "a des modifications non enregistrées. Que voulez vous faire ?"
  129.             Form2.ShowDialog()
  130.         ElseIf Textenote.Text = "" Then
  131.             Dim i As Integer
  132.             i = bandeau_Texte.TabCount
  133.             If bandeau_Texte.TabCount > 2 Then
  134.                 bandeau_Texte.TabPages.RemoveAt(bandeau_Texte.SelectedIndex)
  135.             End If
  136.         End If
  137.     End Sub
  138.     Private Sub Annuler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Annuler.Click
  139.         Textenote.Undo()
  140.     End Sub
  141.     Private Sub Suivant_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Suivant.Click
  142.         Textenote.Redo()
  143.     End Sub
  144.     Private Sub RibbonItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RibbonItem1.Click
  145.         Textenote.Copy()
  146.     End Sub
  147.     Private Sub RibbonItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RibbonItem2.Click
  148.         Textenote.Paste()
  149.     End Sub
  150. End Class


 
Merci de votre aide.

Message cité 1 fois
Message édité par moimael le 18-05-2007 à 20:57:12
mood
Publicité
Posté le 07-05-2007 à 16:42:58  profilanswer
 

n°1557315
moimael
Posté le 09-05-2007 à 13:05:25  profilanswer
 

:bounce:


Message édité par moimael le 09-05-2007 à 13:05:50
n°1557362
MagicBuzz
Posté le 09-05-2007 à 14:27:51  profilanswer
 

Sans avoir testé, je dirais :
- Créer un userControl correspondant au contenu de l'onglet
- Associer chaque onglet à un de ces contrôles (stockés dans une collection par exemple)
 
PS : J'aime bien ton interface :)

n°1558360
moimael
Posté le 10-05-2007 à 18:00:02  profilanswer
 

Tout dabord, merci pour ta reponse, je vais tenter ce que tu me suggere. Par contre pourrait tu etre un peu plus précis sur la manière de s'y prendre ?
Merci :)

n°1558562
pot2yaourt
Posté le 11-05-2007 à 08:17:17  profilanswer
 

moimael a écrit :

j'ai besoin d'onglets que l'utilisateur puisse ouvrir et fermer a volonté, un peu comme dans notepad++ ou firefox.


 
Je pense avoir trouvé ce que tu recherches :
 
http://www.codeproject.com/vb/net/MDITabControl.asp
 
Lionel.

n°1562220
moimael
Posté le 18-05-2007 à 20:58:11  profilanswer
 

Merci de ta réponse, mais ce ne sont pas le songlets que je cherche mais comment les utiliser.

n°1562257
MagicBuzz
Posté le 18-05-2007 à 23:59:47  profilanswer
 

Arf, merde, j'avais pas fait gaffe que t'étais en VB.NET :D
 
Bon, voilà un exemple complet (sans trop de bugs j'espère) et fonctionnel en C#.
Il reproduit le fonctionnement des onglets de Mozilla (ben ouais, parceque dans IE y'a un bouton fermer dans l'onglet et je me sens pas d'attaque ce soir pour en faire un ;))
 
Form1.cs

Code :
  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4.  
  5. namespace tabs
  6. {
  7.    public partial class Form1 : Form
  8.    {
  9.        public Form1()
  10.        {
  11.            InitializeComponent();
  12.        }
  13.  
  14.        private void Form1_Load(object sender, EventArgs e)
  15.        {
  16.            this.tabControl1.TabPages.Add(new NewTab());
  17.        }
  18.  
  19.        private void button1_Click(object sender, EventArgs e)
  20.        {
  21.            OpenFileDialog ofd = new OpenFileDialog();
  22.            if (ofd.ShowDialog() == DialogResult.OK)
  23.            {
  24.                ((FileTab)tabControl1.SelectedTab).FileName = ofd.FileName;
  25.                ((FileTab)tabControl1.SelectedTab).Load();
  26.  
  27.            }
  28.        }
  29.  
  30.        private void button2_Click(object sender, EventArgs e)
  31.        {
  32.            SaveFileDialog sfd = new SaveFileDialog();
  33.            if (sfd.ShowDialog() == DialogResult.OK)
  34.            {
  35.                ((FileTab)tabControl1.SelectedTab).FileName = sfd.FileName;
  36.                ((FileTab)tabControl1.SelectedTab).Save();
  37.            }
  38.        }
  39.  
  40.        private void button3_Click(object sender, EventArgs e)
  41.        {
  42.            int oldIndex = tabControl1.SelectedIndex;
  43.            int newIndex = Math.Max(oldIndex, tabControl1.TabCount - 2);
  44.            tabControl1.SelectedIndex = newIndex;
  45.            tabControl1.TabPages.RemoveAt(oldIndex);
  46.        }
  47.    }
  48.  
  49.    public class NewTab : TabPage
  50.    {
  51.        public NewTab()
  52.        {
  53.            this.Text = "+";
  54.            this.Enter += new EventHandler(NewTab_Enter);
  55.        }
  56.  
  57.        public void NewTab_Enter(object sender, EventArgs e)
  58.        {
  59.            TabControl parent = (TabControl)this.Parent;
  60.            FileTab fileTab = new FileTab(string.Empty);
  61.            parent.TabPages.Insert(parent.TabPages.Count - 1, fileTab);
  62.            parent.SelectedIndex = parent.TabPages.Count - 2;
  63.        }
  64.    }
  65.  
  66.    public class FileTab : TabPage
  67.    {
  68.        private string filename = string.Empty;
  69.  
  70.        public TextBox FileEditor;
  71.        public string FileName
  72.        {
  73.            get
  74.            {
  75.                return this.filename;
  76.            }
  77.            set
  78.            {
  79.                this.Text = "<Nouveau>";
  80.                this.filename = value;
  81.                if (value != string.Empty)
  82.                {
  83.                    FileInfo f = new FileInfo(value);
  84.                    if (f.Exists)
  85.                    {
  86.                        this.Text = f.Name;
  87.                    }
  88.                }
  89.            }
  90.        }
  91.  
  92.        public FileTab(string filename)
  93.        {
  94.            this.FileName = filename;
  95.            FileEditor = new TextBox();
  96.            this.Controls.Add(FileEditor);
  97.        }
  98.  
  99.        public void Save()
  100.        {
  101.            FileInfo f = new FileInfo(this.filename);
  102.            if (f.Exists)
  103.            {
  104.                f.Delete();
  105.            }
  106.            StreamWriter sw = f.CreateText();
  107.            sw.Write(this.FileEditor.Text);
  108.            sw.Close();
  109.        }
  110.  
  111.        public void Load()
  112.        {
  113.            FileInfo f = new FileInfo(this.filename);
  114.            if (f.Exists)
  115.            {
  116.                StreamReader sr = f.OpenText();
  117.                this.FileEditor.Text = sr.ReadToEnd();
  118.                sr.Close();
  119.            }
  120.        }
  121.    }
  122. }


 
Form1.Designer.cs

Code :
  1. namespace tabs
  2. {
  3.    partial class Form1
  4.    {
  5.        /// <summary>
  6.        /// Required designer variable.
  7.        /// </summary>
  8.        private System.ComponentModel.IContainer components = null;
  9.  
  10.        /// <summary>
  11.        /// Clean up any resources being used.
  12.        /// </summary>
  13.        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14.        protected override void Dispose(bool disposing)
  15.        {
  16.            if (disposing && (components != null))
  17.            {
  18.                components.Dispose();
  19.            }
  20.            base.Dispose(disposing);
  21.        }
  22.  
  23.        #region Windows Form Designer generated code
  24.  
  25.        /// <summary>
  26.        /// Required method for Designer support - do not modify
  27.        /// the contents of this method with the code editor.
  28.        /// </summary>
  29.        private void InitializeComponent()
  30.        {
  31.            this.tabControl1 = new System.Windows.Forms.TabControl();
  32.            this.button1 = new System.Windows.Forms.Button();
  33.            this.button2 = new System.Windows.Forms.Button();
  34.            this.button3 = new System.Windows.Forms.Button();
  35.            this.SuspendLayout();
  36.            //
  37.            // tabControl1
  38.            //
  39.            this.tabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  40.                        | System.Windows.Forms.AnchorStyles.Left)
  41.                        | System.Windows.Forms.AnchorStyles.Right)));
  42.            this.tabControl1.Location = new System.Drawing.Point(12, 42);
  43.            this.tabControl1.Name = "tabControl1";
  44.            this.tabControl1.SelectedIndex = 0;
  45.            this.tabControl1.Size = new System.Drawing.Size(268, 212);
  46.            this.tabControl1.TabIndex = 0;
  47.            //
  48.            // button1
  49.            //
  50.            this.button1.Location = new System.Drawing.Point(12, 12);
  51.            this.button1.Name = "button1";
  52.            this.button1.Size = new System.Drawing.Size(75, 23);
  53.            this.button1.TabIndex = 1;
  54.            this.button1.Text = "Ouvrir";
  55.            this.button1.UseVisualStyleBackColor = true;
  56.            this.button1.Click += new System.EventHandler(this.button1_Click);
  57.            //
  58.            // button2
  59.            //
  60.            this.button2.Location = new System.Drawing.Point(93, 13);
  61.            this.button2.Name = "button2";
  62.            this.button2.Size = new System.Drawing.Size(75, 23);
  63.            this.button2.TabIndex = 2;
  64.            this.button2.Text = "Enregistrer";
  65.            this.button2.UseVisualStyleBackColor = true;
  66.            this.button2.Click += new System.EventHandler(this.button2_Click);
  67.            //
  68.            // button3
  69.            //
  70.            this.button3.Location = new System.Drawing.Point(174, 12);
  71.            this.button3.Name = "button3";
  72.            this.button3.Size = new System.Drawing.Size(75, 23);
  73.            this.button3.TabIndex = 3;
  74.            this.button3.Text = "Fermer";
  75.            this.button3.UseVisualStyleBackColor = true;
  76.            this.button3.Click += new System.EventHandler(this.button3_Click);
  77.            //
  78.            // Form1
  79.            //
  80.            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  81.            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  82.            this.ClientSize = new System.Drawing.Size(292, 266);
  83.            this.Controls.Add(this.button3);
  84.            this.Controls.Add(this.button2);
  85.            this.Controls.Add(this.button1);
  86.            this.Controls.Add(this.tabControl1);
  87.            this.Name = "Form1";
  88.            this.Text = "Form1";
  89.            this.Load += new System.EventHandler(this.Form1_Load);
  90.            this.ResumeLayout(false);
  91.  
  92.        }
  93.  
  94.        #endregion
  95.  
  96.        private System.Windows.Forms.TabControl tabControl1;
  97.        private System.Windows.Forms.Button button1;
  98.        private System.Windows.Forms.Button button2;
  99.        private System.Windows.Forms.Button button3;
  100.    }
  101. }


 
C'est évidement très grandement améliorable, et j'ai fait un peu à la va-vite (pour ne pas dire à la porcinet :D)


Message édité par MagicBuzz le 19-05-2007 à 00:33:18
n°1562258
MagicBuzz
Posté le 19-05-2007 à 00:11:50  profilanswer
 

J'ai répondu qu'à moitié au problème tiens...
 
Bon, je continue l'exemple avec le bouton ;)

n°1562264
MagicBuzz
Posté le 19-05-2007 à 00:31:01  profilanswer
 

Le constructeur de "FileTab" devient (en plus c'est plus joli comme ça ;) :

Code :
  1. public FileTab(string filename)
  2.        {
  3.            this.FileName = filename;
  4.  
  5.            PopupButton pb = new PopupButton();
  6.            this.Controls.Add(pb);
  7.            pb.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
  8.            pb.Location = new Point(this.Width - pb.Width - 5, this.Height - pb.Height - 5);
  9.  
  10.            FileEditor = new TextBox();
  11.            this.Controls.Add(FileEditor);
  12.            FileEditor.Multiline = true;
  13.            FileEditor.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
  14.            FileEditor.Size = new Size(this.Width, this.Height - pb.Height - 10);
  15.        }


 
Et la nouvelle classe :

Code :
  1. public class PopupButton : Button
  2.    {
  3.        public PopupButton()
  4.        {
  5.            this.Click += new EventHandler(PopupButton_Click);
  6.            this.Text = "Popup";
  7.        }
  8.  
  9.        public void PopupButton_Click(object sender, EventArgs e)
  10.        {
  11.            MessageBox.Show(((FileTab)this.Parent).FileEditor.Text);
  12.        }
  13.    }

n°1562511
moimael
Posté le 19-05-2007 à 23:34:10  profilanswer
 

Vais me pencher un peu sur ton code qui a l'air très interressant, merci bcp d'avoir pris le temps de me repondre :)


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C#/.NET managed

  [VB .NET]Problème gestion d'onglets

 

Sujets relatifs
Probleme d'installation code block sous Ubuntu[ASP .Net] XForms
probleme de slot avec qt et de QTimer[C#/.NET 2.0] DropDownList SqlDataSource et Sécurité
FPDF problème d'orientationProbleme FCKeditor (code source image)
problème de requete sqlgestion des accents dans un body d'email
probleme session_start ()Création de forum : problème avec l'inscription
Plus de sujets relatifs à : [VB .NET]Problème gestion d'onglets


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR